Why not use exceptions as regular flow of control?

后端 未结 24 1885
心在旅途
心在旅途 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?

提交回复
热议问题