The problem of exception handling and if judgment in C + +

Source: Internet
Author: User

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 ...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.