Do I have to break after throwing exception?

前端 未结 6 1662
予麋鹿
予麋鹿 2021-01-03 20:58

I\'m writing a custom class in C# and I\'m throwing a couple exceptions if people give the wrong inputs in some of the methods. If the exception is thrown, will any of the c

相关标签:
6条回答
  • 2021-01-03 21:04

    I recommend stepping through your program with a debugger then you'll see for yourself what is going on. Very useful for learning!

    0 讨论(0)
  • 2021-01-03 21:04

    As an aside to your actual question: you might want to rethink using exceptions to provide validation info back to the user.

    Raising exceptions is expensive resource-wise and slow. If you have a number of validation rules that you need to apply then write specific code for these - you should probably only rely on exception handling for things you don't anticipate.

    0 讨论(0)
  • 2021-01-03 21:08

    I came here looking for an answer to the original post and almost missed a very valuable answer posted by Eric Lippert. Here's his answer posted in the comments:

    Split this up into three questions.

    (1) Will any of the code in the method after the throw be executed?
    YES. If the exception was inside a try then code inside matching catch blocks or finally block will be executed. If there is no try block then NO. Control branches to the nearest enclosing finally, catch or (in vb) exception filter block up the stack.

    (2) Do I have to put a break after the throw?
    NO, never do that. The end point of the throw statement is not reachable; a throw is treated as a goto by the compiler. A statement immediately following a throw is not reachable and will never execute.

    (3) Does a throw always quit the method?
    NO. If the throw is in a try and the try has a matching catch block then the catch block can "eat" the exception. Only if there is no catch block does the exception do a non-local goto up the call stack.

    If you have more questions about this, I recommend reading the C# specification; all this behavior is clearly documented.

    Finally, it sounds like you are throwing "boneheaded" exceptions, as in "hey boneheaded caller, I told you to never give me that data". That's great because it prevents bugs in callers. But if you do that, you should make sure that the caller has some way of knowing what you expect! If the caller cannot figure out whether you're going to throw or not based on your documentation, then you haven't made a boneheaded exception, you've made a vexing exception. See http://blogs.msdn.com/ericlippert/archive/2008/09/10/vexing-exceptions.aspx for details.

    0 讨论(0)
  • 2021-01-03 21:15

    Throw will move up the stack, thus exiting the method.

    0 讨论(0)
  • 2021-01-03 21:23

    When you throw an exception, the next code to get executed is any catch block that covers that throw within the method (if any) then, the finally block (if any). You can have a try, a try-catch, a try-catch-finally or a try-finally. Then, if the exception is not handled, re-thrown by a catch block or not caught at all, control is returned to the caller. For example, you will get "Yes1, Yes2, Yes3" from this code ...

    try
    {
        Console.WriteLine("Yes1");
        throw (new Exception());
        Console.WriteLine("No1");
    
    }
    catch
    {
        Console.WriteLine("Yes2");
        throw;
        Console.WriteLine("No2");
    }
    finally
    {
        Console.WriteLine("Yes3");
    }
    
    Console.WriteLine("No3");
    
    0 讨论(0)
  • 2021-01-03 21:24

    If you've wrapped your code in a Try...Catch...Finally block, then the code under Finally will always execute. For example:

    Try
      ' do some stuff here
      ' Examine user input
      If user input isn't valid
          Throw new exception
    Catch
       Throw ' Just re-throws the same exception
    Finally
       ' This code will execute, no matter what - exception or not
    End Try
    
    0 讨论(0)
提交回复
热议问题