Rethrowing exceptions:
-
I'm having difficulty understanding how to explain the following code. Please help...we have to dissect it and explain the operation of the code once it is executed. In advance thank you... using System; // Rethrowing exceptions: class MyClass { public static void Main() { MyClass x = new MyClass(); try { string s = null; x.MyFn(s); } catch (Exception e) { Console.WriteLine("{0} Exception caught.", e); } } public void MyFn(string s) { if (s == null) throw(new ArgumentNullException()); } } -ntfirebird- -- modified at 22:24 Thursday 29th September, 2005
-
I'm having difficulty understanding how to explain the following code. Please help...we have to dissect it and explain the operation of the code once it is executed. In advance thank you... using System; // Rethrowing exceptions: class MyClass { public static void Main() { MyClass x = new MyClass(); try { string s = null; x.MyFn(s); } catch (Exception e) { Console.WriteLine("{0} Exception caught.", e); } } public void MyFn(string s) { if (s == null) throw(new ArgumentNullException()); } } -ntfirebird- -- modified at 22:24 Thursday 29th September, 2005
What's wrong with this code? It workes well. When the program call the MyFn(s) and execute the throw state then it throw out a exception, in the Main() which make the MyFn(s) call most nearly, the try block catch the exception and goes to deal the exception code 'Console.WriteLine(...)'. === Game is power! ===
-
What's wrong with this code? It workes well. When the program call the MyFn(s) and execute the throw state then it throw out a exception, in the Main() which make the MyFn(s) call most nearly, the try block catch the exception and goes to deal the exception code 'Console.WriteLine(...)'. === Game is power! ===
You missed the point of his question. He was not wondering why the code would NOT work, he is wondering why the code DOES work. My guess would be he's taking a .NET class and this is an exersize for the student to explain rethrowing expetions. However, the code in question does not REthrow and exception, it merely throws one and catches it. I should resist the temptation to help you cheat, but here goes anyway. The "try" is saying "run the code between the next two braces, and if an exception is thrown during any of it, then "catch" that exception and run the code in the catch block. If no exception occurs, don't run any of the code in the catch block. Your function MyFn() throws an execption if the parms are null, and the catch block in Main will catch it and run the code in the catch block.