Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Rethrowing exceptions:

Rethrowing exceptions:

Scheduled Pinned Locked Moved C#
helptutorial
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    ntfirebird
    wrote on last edited by
    #1

    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

    L 1 Reply Last reply
    0
    • N ntfirebird

      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

      L Offline
      L Offline
      Libra
      wrote on last edited by
      #2

      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! ===

      D 1 Reply Last reply
      0
      • L Libra

        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! ===

        D Offline
        D Offline
        Dave Everett
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups