C ++ exceptions from scratch (III): exceptions and inheritance, exceptions and pointers, exception specifications

Source: Internet
Author: User

I. Exceptions and inheritance

If the exception type is C ++ and the class has its base class, put the error handler of the derived class in front of it, and the error handler of the base class in the back


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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# Include <iostream>
# Include <string>

Using namespace STD;

Class myexception
{
Public:
Myexception (const char * message)
: Message _ (Message)
{
Cout <"myexception..." <Endl;
}
Myexception (const myexception & other): Message _ (other. Message _)
{
Cout <"Copy myexception..." <Endl;
}
Virtual ~ Myexception ()
{
Cout <"~ Myexception... "<Endl;
}

Const char * What () const
{
Return message _. c_str ();
}
PRIVATE:
String message _;
};

Class myexceptiond: Public myexception
{
Public:
Myexceptiond (const char * message)
: Myexception (Message)
{
Cout <"myexceptiond..." <Endl;
}
Myexceptiond (const myexceptiond & other)
: Myexception (other)
{
Cout <"Copy myexceptiond..." <Endl;
}
~ Myexceptiond ()
{
Cout <"~ Myexceptiond... "<Endl;
}
};

Int main (void)
{
Try
{
Myexceptiond E ("test exception ");
Throw E;
}
Catch (myexceptiond & E)
{
Cout <"catch myexceptiond..." <Endl;
Cout <E. What () <Endl;
}
Catch (myexception & E)
{
Cout <"catch myexception..." <Endl;
Cout <E. What () <Endl;
}

Return 0;
}



The exception of a derived class can be caught by the base class, and the exception handler can match the previous one. If the base class is located at the beginning, it is not referenced, for example, catch (myexception
E); then it will be caught by this, and the object slicing problem will occur during the construction of E.

Ii. Exceptions and pointers

It is usually a bad idea to throw a pointer, because the throw pointer requires that there be an object pointed to by the pointer wherever the corresponding processing code exists (note that when Throw throws, the pointer itself is copied, will not copy the content pointed to by the pointer)


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
Int main (void)
{
Try
{
// Myexceptiond E ("test exception ");
// Throw & E;
Throw new myexceptiond ("test exception ");
}
/* Catch (void * E)
{
Cout <"catch void *..." <Endl;
Cout <(myexceptiond *) e)-> what () <Endl;
Delete (myexceptiond *) E;
}*/
Catch (myexceptiond * E)
{
Cout <"catch myexceptiond..." <Endl;
Cout <e-> what () <Endl;
Delete E;
}
Catch (myexception & E)
{
Cout <"catch myexception..." <Endl;
Cout <E. What () <Endl;
}

Return 0;
}



Here, the myexception and myexeptiond classes are shown above. Now the myexeptiond object is allocated on the stack, so it has not been released during catch, and can be accessed

E-> what (); but you need to delete E at the end of the catch; assume that throw new myexceptiond ("test exception ");

Myexceptiond E ("test exception ");
Throw & E;

That is, the pointer to the local object is thrown. Because the myexeptiond object has been destructed during catch, e-> what () cannot be accessed; that is, E is a null pointer.

Another point is that any type of pointer can be captured by the void * pointer, if the catch (void * E );
The comment is opened, and the exception will be caught first because it is in the front.


Iii. Exception specifications

1. The purpose of the exception Specification Description is to let the function user know what exceptions the function may throw.
You can list all the exception types that this function may throw in the function declaration.
For example:

Void fun () Throw (A, B, C, D );

2. If no exception interface is declared, this function can throw any type of exception.
3. Do not throw any type exception function declaration as follows:

Void fun () Throw ();


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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
Void fun (int n) Throw (INT, myexception, myexceptiond)
{
If (n = 1)
{
Throw 1;
}
Else if (n = 2)
{
Throw myexception ("test exception ");
}
Else if (n = 3)
{
Throw myexceptiond ("test exceptiond ");
}

}

Void fun2 () Throw ()
{

}

Int main (void)
{
Try
{
Fun (2 );
}

Catch (int n)
{
Cout <"catch int..." <Endl;
Cout <"n =" <n <Endl;
}
Catch (myexceptiond & E)
{
Cout <"catch myexceptiond..." <Endl;
Cout <E. What () <Endl;
}
Catch (myexception & E)
{
Cout <"catch myexception..." <Endl;
Cout <E. What () <Endl;
}

Return 0;
}



In fact, compilation generates a warning:

Warning c4290: Ignore the C ++ exception specification, but the indicator function is not _ declspec (nothrow)

That is to say, the VC ++ compiler does not yet support exception specification descriptions. For example, void fun (int n) Throw (INT, myexception, myexceptiond );
An exception of the double type is not declared, but throw 1.0 in the function; it will still succeed in the external catch (double.


IV. C ++ standard library exception levels


For example, if a dynamic_cast execution error occurs, a bad_cast exception occurs. If a new memory allocation error occurs, a bad_alloc exception occurs. In fact, these exception classes are inherited from the exception.

Class, but the internal implementation does not have valid code, just to identify the type of exception generated by the current program.


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.