63rd Lesson C Language exception handling

Source: Internet
Author: User

1. concept of anomalies

(1) The program may produce abnormal during Operation

(2) The difference between the exception (Exception) and the Bug

Exceptions are execution branches that can be expected when a program is run

Bug is the program is wrong, is not expected to run the way

2. Comparison of exceptions and bugs

(1) Exception: If the runtime generates a condition other than 0 , the external file that needs to be opened does not exist , and the array is accessed out of bounds

(2) Bug: If the use of wild pointers , heap array After the end of use is not released , Select Sort cannot handle the length of 0 array

3. C Language Classic processing method: If-else

(1) Sample program

void func (...) {    if( Judging If an exception is generated )    {        normal code logic    }      else   {        exception case code logic    }}

"Programming Experiment" division operation exception Handling

(2) Defects

The ①divide function has 3 parameters, which makes it difficult to understand its purpose .

②divide function call must be judged by the result of valid : True indicates normal results, false indicates an exception occurred

4. How to optimize C language exception handling

(1) optimized by setjmp () and longjmp ( )

① include header file # include <setjmp.h> or <csetjmp>

②int setjmp(jmp_buf env): The current context is saved in the JUM_BUF structure for later longjmp () use when restoring state information. If you call setjmp () directly, the return value is 0, and if you call setjmp () by calling longjmp (), the return value is not 0. SETJMP () can only be called in certain situations, such as in an if statement, a switch statement, and a conditional test part of a looping statement, as well as some simple relational expressions.

③void longjmp(jmp_buf env, int val): used to recover state information saved to Env by the last Call to setjmp (). When it finishes executing, the program execution stream jumps to the setjmp () line , and the return value of setjmp () At this point is Val, based on the re-execution of setjmp ().

Optimization of exception handling in "programming experiment" division operation

(2) Defects

① introduction of SETJMP () and longjmp () must involve the use of global variables

Violent jumps cause code readability to be reduced

Nature or If-else exception handling method

5. Common problems in C language exception handling

(1) A large number of code handling exceptions that can be mixed into the program logic

(2) normal logic and exception handling code are mixed together, resulting in rapid expansion of code and difficult to maintain.

Analysis of exception handling code for "Case Analysis"

6. Summary

(1) Inevitable anomalies in the program

(2) Exceptions are run-time issues that can be foreseen in the development phase

(3) C language through the classic If-else

(4) There are better exception handling methods in C + +

63rd Lesson C Language exception handling

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.