The anomaly is the judgment of whether the situation is abnormal, so what is the difference between exception handling and if judgment statement? What are the benefits.
The advantage of the if-else approach is that it is more close and logical and has better performance than exception. The disadvantage of exception is that it is not suitable for OOP, it is not very semantic, it is not easy to error-error-tracking or has fewer error tips, and the type is relatively simple. The advantage of the exception approach is that business logic and exception handling are decoupled (the code is relatively clear), that a try handles the business, and that exceptions are handled in the catch. In the API design, you can design exception handler to handle the anomaly, which makes the hierarchy clear. At the same time, better OOP encapsulation and polymorphism. The disadvantage is that the performance is relatively poor. But for large projects, exception is the best way to do it.
The above is the cliché, below say my personal understanding.
In C + +, exceptions are in the form of objects, so exceptions have the advantages of OOP. In my opinion, the exception is better than if: The exception is neutral . That is, the exception throw and catch are separated, and the advantage is that you can handle an exception in a different way. For example, if you go for an interview, you have 3 interviewers. Take you as a class my,3 an interviewer is a class a,b,c. At this time you throw out a word, three interviewers received this sentence, but for this sentence, different interviewers have different understanding, a think that this sentence grammar problems, b feel the efficiency is too low, C feel the use of memory space is too large. This example illustrates the benefits of exception, no matter how many interviewers, can be handled according to their own way, embodies the advantages of OOP: encapsulation, polymorphism, suitable for large-scale projects, modular ...