Exception handling..????
-
hello ALL, I have a doubt. if i want to continue normally after catching the expecteed exception is it possible?? Ex:- try { int intDivVal; for (int i =o ;i<5;i++) { int Divval=10/i; } } catch(dividebyzeroexception ex) { messagebox.show(ex.message.tostring()); } i want to continue after showing the message. Thanks in advance
Bharath.S Ron
-
hello ALL, I have a doubt. if i want to continue normally after catching the expecteed exception is it possible?? Ex:- try { int intDivVal; for (int i =o ;i<5;i++) { int Divval=10/i; } } catch(dividebyzeroexception ex) { messagebox.show(ex.message.tostring()); } i want to continue after showing the message. Thanks in advance
Bharath.S Ron
yep your code there will carry right on. If you continue you have to think about anything that might have broken during an exception though i.e. was something being set to a value or a stream being opened. Carrying on can lead to losing data or more expcetions. If you want to do something with the exception and then let the exception carry on then you can catch(dividebyzeroexception ex) { messagebox.show(ex.message.tostring()); throw ex; }
-
hello ALL, I have a doubt. if i want to continue normally after catching the expecteed exception is it possible?? Ex:- try { int intDivVal; for (int i =o ;i<5;i++) { int Divval=10/i; } } catch(dividebyzeroexception ex) { messagebox.show(ex.message.tostring()); } i want to continue after showing the message. Thanks in advance
Bharath.S Ron
hello It will continue as you wrote it. Alternatively, you can use the
finally
statement to make sure some code is executed in all cases (try
succeeded orcatch
executed) Talal-- If this is a post that has been helpful to you, please vote for it. Thank you! "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." --Rich Cook
-
hello ALL, I have a doubt. if i want to continue normally after catching the expecteed exception is it possible?? Ex:- try { int intDivVal; for (int i =o ;i<5;i++) { int Divval=10/i; } } catch(dividebyzeroexception ex) { messagebox.show(ex.message.tostring()); } i want to continue after showing the message. Thanks in advance
Bharath.S Ron
Yeah, I understand you want to continue your loop after the exception is thrown... In your code the try & catch block contains the loop...
try { int intDivVal; for (int i =o ;i<5;i++) { int Divval=10/i; } } catch(dividebyzeroexception ex) { messagebox.show(ex.message.tostring()); }
However, the loop should contain a try & catch block in order to continue the looptry { int intDivVal; for (int i =o ;i<5;i++) { try { int Divval=10/i; } catch (Exception ex) { MessageBox.Show("Oops, 10 / " + i + " failed!"); } } } catch(dividebyzeroexception ex) { messagebox.show(ex.message.tostring()); }
.: I love it when a plan comes together :. http://www.zonderpunt.nl
-
yep your code there will carry right on. If you continue you have to think about anything that might have broken during an exception though i.e. was something being set to a value or a stream being opened. Carrying on can lead to losing data or more expcetions. If you want to do something with the exception and then let the exception carry on then you can catch(dividebyzeroexception ex) { messagebox.show(ex.message.tostring()); throw ex; }
originSH wrote:
catch(dividebyzeroexception ex) { messagebox.show(ex.message.tostring()); throw ex; }
You actually want to just do a
throw
here rather thanthrow ex
. Thethrow ex
causes the original stack trace information to be lost whilethrow
preserves it.Scott.
—In just two days, tomorrow will be yesterday. [Forum Guidelines] [Articles] [Blog]
-
originSH wrote:
catch(dividebyzeroexception ex) { messagebox.show(ex.message.tostring()); throw ex; }
You actually want to just do a
throw
here rather thanthrow ex
. Thethrow ex
causes the original stack trace information to be lost whilethrow
preserves it.Scott.
—In just two days, tomorrow will be yesterday. [Forum Guidelines] [Articles] [Blog]
-
Yep.
throw
isrethrow
in the IL.Scott.
—In just two days, tomorrow will be yesterday. [Forum Guidelines] [Articles] [Blog]
-
hello ALL, I have a doubt. if i want to continue normally after catching the expecteed exception is it possible?? Ex:- try { int intDivVal; for (int i =o ;i<5;i++) { int Divval=10/i; } } catch(dividebyzeroexception ex) { messagebox.show(ex.message.tostring()); } i want to continue after showing the message. Thanks in advance
Bharath.S Ron
You should think about continuation that makes sense. You may return to an input for example, or substitute the illegal result with something that makes sense for you application. Just ignoring the exception is generally not a very good idea.
Rudolf Heijink