Why not use exceptions as regular flow of control?

后端 未结 24 1848
心在旅途
心在旅途 2020-11-21 07:30

To avoid all standard-answers I could have Googled on, I will provide an example you all can attack at will.

C# and Java (and too many others) have with plenty of ty

相关标签:
24条回答
  • 2020-11-21 07:54

    Lets assume you have a method that does some calculations. There are many input parameters it has to validate, then to return a number greater then 0.

    Using return values to signal validation error, it's simple: if method returned a number lesser then 0, an error occured. How to tell then which parameter didn't validate?

    I remember from my C days a lot of functions returned error codes like this:

    -1 - x lesser then MinX
    -2 - x greater then MaxX
    -3 - y lesser then MinY
    

    etc.

    Is it really less readable then throwing and catching an exception?

    0 讨论(0)
  • 2020-11-21 07:54

    But you won't always know what happens in the Method/s that you call. You won't know exactly where the exception was thrown. Without examining the exception object in greater detail....

    0 讨论(0)
  • 2020-11-21 07:56

    Typically there is nothing wrong, per se, with handling an exception at a low level. An exception IS a valid message that provides a lot of detail for why an operation cannot be performed. And if you can handle it, you ought to.

    In general if you know there is a high probability of failure that you can check for... you should do the check... i.e. if(obj != null) obj.method()

    In your case, i'm not familiar enough with the C# library to know if date time has an easy way to check whether a timestamp is out of bounds. If it does, just call if(.isvalid(ts)) otherwise your code is basically fine.

    So, basically it comes down to whichever way creates cleaner code... if the operation to guard against an expected exception is more complex than just handling the exception; than you have my permission to handle the exception instead of creating complex guards everywhere.

    0 讨论(0)
  • 2020-11-21 07:58

    How about performance? While load testing a .NET web app we topped out at 100 simulated users per web server until we fixed a commonly-occuring exception and that number increased to 500 users.

    0 讨论(0)
  • 2020-11-21 07:58

    I think that you can use Exceptions for flow control. There is, however, a flipside of this technique. Creating Exceptions is a costly thing, because they have to create a stack trace. So if you want to use Exceptions more often than for just signalling an exceptional situation you have to make sure that building the stack traces doesn't negatively influence your performance.

    The best way to cut down the cost of creating exceptions is to override the fillInStackTrace() method like this:

    public Throwable fillInStackTrace() { return this; }
    

    Such an exception will have no stacktraces filled in.

    0 讨论(0)
  • 2020-11-21 08:03

    Have you ever tried to debug a program raising five exceptions per second in the normal course of operation ?

    I have.

    The program was quite complex (it was a distributed calculation server), and a slight modification at one side of the program could easily break something in a totally different place.

    I wish I could just have launched the program and wait for exceptions to occur, but there were around 200 exceptions during the start-up in the normal course of operations

    My point : if you use exceptions for normal situations, how do you locate unusual (ie exceptional) situations ?

    Of course, there are other strong reasons not to use exceptions too much, especially performance-wise

    0 讨论(0)
提交回复
热议问题