Академический Документы
Профессиональный Документы
Культура Документы
Outline
11.1
11.2
11.3
11.4
11.5
11.6
11.7
11.8
Introduction
Exception Handling Overview
Example: DivideByZeroException
.NET Exception Hierarchy
finally Block
Exception Properties
Programmer-Defined Exception Classes
Handling Overflows with Operators checked and unchecked
11.1 Introduction
Exception handling
Create application that can handle or resolve exception
Catch
Represent types of exceptions the catch can handle
Finally
(Optional) codes present here will always execute
Exception handling
Stack unwinding
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
Outline
System;
System.Drawing;
System.Collections;
System.ComponentModel;
System.Windows.Forms;
System.Data;
DivideByZeroTest
.cs
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
Outline
DivideByZeroTest
.cs
if
DivideByZeroException
thrown if denominator is zero
outputLabel.Text = result.ToString();
} // end try
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
Outline
DivideByZeroTest
.cs
Catch handler for
DivideByZeroException
Outline
DivideByZeroTest
.cs
Program Output
When incorrect format
are entered into either
input fields
When attempting to
diving by zero
10
ApplicationException
Programmer use to create data types specific to their
application
Low chance of program stopping execution
SystemException
CLR can generate at any point during execution
Runtime exceptin
Example: IndexOutOfRangeException
11
Finally block
12
An exception object
Must be of either class Exception or one of its derived class
Customize the exception type thrown from methods
13
A default constructor
A constructor that receives a string argument
A constructor that takes a string argument and an Exception
argument
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
Outline
NegativeNumberEx
ception.cs
14
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
Outline
System;
System.Drawing;
System.Collections;
System.ComponentModel;
System.Windows.Forms;
System.Data;
15
SquareRootTest.c
s
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
Outline
16
SquareRootTest.c
s
SqaureRoot throws a
NegativeNumberException
A FormatException occurs if
NegativeNumberExceptions thrown
not a valid number from user
Try block invoke SqaureRoot
// catch any
try
{
double result =
SquareRoot( Double.Parse( inputTextBox.Text ) );
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
Outline
outputLabel.Text = result.ToString();
}
17
correct function
When attempting
to take a negative
square root