i'm trying figure out in c# winforms if anywhere possible stop code execution calling function , not return.
the following code possible in php
if($something == null) $this->response->error(0); // code never executed if condition true echo 'hello'; and library response, has like:
public class response { public function error($index) { $response = array(); switch ($index) { case 0: $response = array('msg' => 'fields missing..'); break; } // trick here exit(json_encode($response)); } } so, in c# project , within form call response library way:
private void button1_click(object sender, eventargs e) { libraries.response response = new libraries.response(); if(textbox1.text == "") response.error(0); // code continues executed if condition true button2.performclick(); } the response triggers messagebox has nothing make code execution stop.
class response { public void error(int index) { string msg = ""; switch (index) { case 0: msg = "fields missing.."; break; } messagebox.show(msg, "my app", messageboxbuttons.ok, messageboxicon.error); } } now, know can use return below code shows stop code execution, wondering if there's else can put in library responsethat trick?
if(textbox1.text == "") { response.error(0); return; }
in .net stop execution of code in exceptional situations, can throw exception. way lines of code after throw statement not execute , exception bubble stack until first try-catch matches specific exception type , code of catch block execute. if no suitable try-catch block found in call stack, process terminated , message displayed user.
exceptions should not used change flow of program part of ordinary execution. exceptions should used report , handle error conditions.
to throw exception can use throw statement. example:
public string getobjecttypename(object something) { if(something==null) throw new exception("some exception message"); return something.gettype().name; } for more information take @ these resources:
Comments
Post a Comment