Why not use exceptions as regular flow of control?

后端 未结 24 2371
心在旅途
心在旅途 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:05

    One aesthetic reason:

    A try always comes with a catch, whereas an if doesn't have to come with an else.

    if (PerformCheckSucceeded())
       DoSomething();
    

    With try/catch, it becomes much more verbose.

    try
    {
       PerformCheckSucceeded();
       DoSomething();
    }
    catch
    {
    }
    

    That's 6 lines of code too many.

提交回复
热议问题