What is the throw keyword responsible for in a C# error handling exercise -
i trying learn how debug , handle errors in c# code using vs2012 desktop. stepping-through below code using step into f11 technique.
i understand how execution of code jumps between different parts of code. have messages printed out console me identify step being executed. have split screen can see line of code i'm stepping , output messages in console @ same time.
on line 70 (marked in comments) - when nested index
passed throwexception()
not understand why there throw
keyword , functionality is. why pointer jump nested finally block goes way main , throws indexoutofbounds
exception. mean? see execution jump in code don't understand why says throw
. mean exception handled? how?
i read when throw
exception there no need add break;
statement because switch statement breaks when meets throw
keyword, not sure right path of thinking in example.
please, me understand meaning of throw
keyword on line 70.
thank in advance.
using system; using system.collections.generic; using system.linq; using system.text; using system.threading.tasks; namespace ch07ex02 { class program { static string[] etypes = { "none", "simple", "index", "nested index" }; static void main(string[] args) { foreach (string etype in etypes) { try { console.writeline("main() try block reached."); // line 19 console.writeline("throwexception(\"{0}\") called.", etype); throwexception(etype); console.writeline("main() try block continues."); // line 22 } catch (system.indexoutofrangeexception e) // line 24 { console.writeline("main() system.indexoutofrangeexception catch" + " block reached. message:\n\"{0}\"", e.message); } catch // line 30 { console.writeline("main() general catch block reached."); } { console.writeline("main() block reached."); } console.writeline(); } console.readkey(); } static void throwexception(string exceptiontype) { console.writeline("throwexception(\"{0}\") reached.", exceptiontype); switch (exceptiontype) { case "none": console.writeline("not throwing exception."); break; // line 50 case "simple": console.writeline("throwing system.exception."); throw new system.exception(); // line 53 case "index": console.writeline("throwing system.indexoutofrangeexception."); etypes[4] = "error"; // line 56 break; case "nested index": try // line 59 { console.writeline("throwexception(\"nested index\") " + "try block reached."); console.writeline("throwexception(\"index\") called."); throwexception("index"); // line 64 } catch // line 66 { console.writeline("throwexception(\"nested index\") general" + " catch block reached."); throw; // line 70 } { console.writeline("throwexception(\"nested index\") finally" + " block reached."); } break; } } } }
the above code compiles , runs no errors.
keyword throw
can used inside catch
clause rethrow whatever exception has been caught catch
block. lets "plug in" execution logic in process of handling exception without disrupting details of exception has been thrown.
in case, able log details console, , re-throw exception if never handled it. note different catching exception , wrapping in own, because details of original exception preserved.
Comments
Post a Comment