c# - Using Try-Catch-Finally to handle arithmetic exceptions -
i try 2 different things (both have strong possibility of failure) , reason use "finally" statement run "safety" in case first 2 tries both fail.
take following example (no not code using in project!).
int 0 = 0; int 1 = 1; try { // throws ' cannot divide 0 ' error int error = 1 / zero; } catch { // throws error again of course int somenum = 1 / zero; } { messagebox.show("i can never make here ..."); }
so, program following:
- attempt divide zero
- if step #1 fails, 'catch' statement run code (which should once again fail in example).
- if both steps #1 , #2 fail, program display messagebox in 'finally' statement.
am close one?
int 0 = 0; int 1 = 1; try { try { // throws ' cannot divide 0 ' error int error = 1 / zero; } catch (dividebyzeroexception) { // throws error again of course int somenum = 1 / zero; } } catch (dividebyzeroexception) { messagebox.show("i can never make here ..."); }
Comments
Post a Comment