Why not use exceptions as regular flow of control?

后端 未结 24 2302
心在旅途
心在旅途 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 08:12

    I feel that there is nothing wrong with your example. On the contrary, it would be a sin to ignore the exception thrown by the called function.

    In the JVM, throwing an exception is not that expensive, only creating the exception with new xyzException(...), because the latter involves a stack walk. So if you have some exceptions created in advance, you may throw them many times without costs. Of course, this way you can't pass data along with the exception, but I think that is a bad thing to do anyway.

提交回复
热议问题