Learning C ++ exceptions from scratch (1): Introduction to C language error handling methods and C ++ Exception Handling Methods (throw, try, catch)

Source: Internet
Author: User

I. C language error handling methods

1. Return Value (if... Else statement judgment error)
2. errno (called in Linux)
3. GOTO statement (partial jump in function)
4. setjmp, longjmp (do not use
Setjmp and
Longjmp in C ++ programs; these functions do not support C ++ object semantics .)

# DEFINE _ jblen 16

Typedef _ jbtype jmp_buf [_ jblen];


Saves the current state of the program.

int setjmp(   jmp_buf env );
Parameters

Env

Variable in which environment is stored.

Return Value

Returns 0 after saving the stack environment. If setjmp returns as a result of alongjmp call, it returns thevalue argument oflongjmp, or if thevalue
Argument oflongjmp is 0, setjmp returns 1. There is no error return.


Restores stack environment and execution locale.

void longjmp(    jmp_buf env,    int value  );
Parameters

Env

Variable in which environment is stored.

Value

Value to be returnedSetjmpCall.

C ++ code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# Include <stdio. h>
# Include <setjmp. h>

Jmp_buf Buf;

Double divide (double A, double B)
{
If (B = 0.0)
{
Longjmp (BUF, 1); // throw
}
Else
Return A/B;
}

Int main (void)
{
Int ret;
Ret = setjmp (BUF );
If (ret = 0) // try
{
Printf ("division... \ n ");
Printf ("% F \ n", divide (5.0, 0.0 ));
}
Else if (ret = 1) // catch
{
Printf ("divisiong by zero \ n ");
}
Return 0;
}


Analysis: first, setjump is successfully set and 0 is returned. Execute the divide function. If the divisor is 0, longjump jumps back to setjump and returns parameter 1. Therefore, the output continues.

Divisiong by zero, setjump, and longjump are already prototype of C ++ exceptions. Even if the divide function itself does not call longjump, it calls a function, in which longjump, you can also jump to the setjump, so that you do not need to judge the error through the function return values.

C language error handling is considered tightly coupled. Function users must write error handling code very close to function calls, which makes it clumsy and difficult to use.


Ii. How to Handle C ++ exceptions (throw, try, catch)

The compilation of error handling code is no longer tedious and not mixed with "normal" code. Programmers can focus on normal processes and then write Exception Handling Code in a region. If you call the same function multiple times, you only need to write an error handling code in one place.
Errors cannot be ignored.


C ++ code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# Include <iostream>
Using namespace STD;

Double divide (double A, double B)
{
If (B = 0.0)
{
Throw 1; // throw
}
Else
Return A/B;
}

Int main (void)
{
Try // try
{
Cout <"division..." <Endl;
Cout <divide (3.0, 1.0) <Endl;
Cout <divide (5.0, 0.0) <Endl;

}
Catch (INT) // catch
{
Cout <"divisiong by zero" <Endl;
}
Return 0;
}



That is, the throw can be caught no matter how far it is, but it must be noted that the type needs to be matched. The following article will discuss throwing a custom type exception.


Refer:

C ++ primer version 4
Valid tive C ++ 3rd
C ++ programming specifications

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.