windows - C# exceptions and code breaking -
i have method called tryme
has try catch block , catches exceptions.
call him class, when exception occurs doesn't stop code execution.
example:
public void tryme() { try { somemethod(); } catch(exception exception){ messagebox.show(exception.message); } } //method calling actions caactions = new actions(); cactions.tryme(); /////////////////////////////////// //if exception handled should stop here. this.hide(); formactions formactions = new formactions();
the method definition in class file. method calling in windows form.
problem shows messagebox , code execution continues.
want stop code after exception catching , not hiding form. if okay should hide it.
maybe conceptions wrong?
the easiest fix change funcion return true/false depending on whether succeeded or not (i.e. hide form if tryme method didn't error):
public bool tryme() { try { somemethod(); return true; } catch (exception exception) { // log exception return false; } }
and call this:
if (cactions.tryme()) { this.hide(); }
another option re throw exception after showing message, , have calling code handle in try catch:
public void tryme() { try { somemethod(); } catch (exception exception) { // log exception? throw; } }
calling code:
try { cactions.tryme(); this.hide(); } catch (exception ex) { // error handling }
Comments
Post a Comment