Upload
roland-baylon-ii
View
214
Download
0
Embed Size (px)
Citation preview
8/19/2019 06 LCD Slide Handout 1
1/9
11/17/2015 1K0095*Property of STI
Exception Handling
Basic Exception Handling
try , catch, and finally
User-Defined Exceptions
1 _________________________
___________________________
___________________________
___________________________ ___________________________
___________________________
___________________________
___________________________
___________________________
11/17/2015 2K0095*Property of STI
Exception
an event that occurs during the
execution of a program that disrupts the
normal flow of instructions
Two (2) types:
oChecked exception
oUnchecked exception
2 __________________________
___________________________
___________________________
___________________________ ___________________________
___________________________
___________________________
___________________________
___________________________
11/17/2015 3K0095*Property of STI
Sample ExceptionsWhen a program issues a command to
read a file from a disk but the file does
not exist there
When a program attempts to write a
data to a disk but the disk is full
When a program asks for a user input
but the user enters an invalid data type
3 _________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
11/17/2015 4K0095*Property of STI
Sample ExceptionsWhen a program attempts to divide a
value by 0
When a program tries to access an array
with an illegal index
4 __________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
8/19/2019 06 LCD Slide Handout 1
2/9
11/17/2015 5K0095*Property of STI
Exception Handling
The process used to change the normal
flow of code execution if a specified
exception occurs
5 _________________________
___________________________
___________________________
___________________________ ___________________________
___________________________
___________________________
___________________________
___________________________
11/17/2015 6K0095*Property of STI
Checked Exception
Occurs during compilation
Cannot be ignored and should be
resolved to be able to execute the
program
6 __________________________
___________________________
___________________________
___________________________ ___________________________
___________________________
___________________________
___________________________
___________________________
11/17/2015 7K0095*Property of STI
Checked Exceptions
7 _________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
11/17/2015 8K0095*Property of STI
Unchecked ExceptionOccurs during execution
Also known as runtime exception
Ignored at the time of compilation
8 __________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
8/19/2019 06 LCD Slide Handout 1
3/9
11/17/2015 9K0095*Property of STI
Unchecked Exceptions
9 _________________________
___________________________
___________________________
___________________________ ___________________________
___________________________
___________________________
___________________________
___________________________
11/17/2015 10K0095*Property of STI
Unchecked Exceptions
10 _________________________
___________________________
___________________________
___________________________ ___________________________
___________________________
___________________________
___________________________
___________________________
11/17/2015 11K0095*Property of STI
try Block a block of code that might throw an exception
that can be handled by a matching catch block
Format:
try
{
//statement or statements that might
generate an exception
}
11 ________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
11/17/2015 12K0095*Property of STI
catch Block a segment of code that can handle an
exception that might be thrown by the try
block that precedes it
Format:
catch (Exception exceptionName)
{
//actions to take if exception occurs
}
12 _________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
8/19/2019 06 LCD Slide Handout 1
4/9
11/17/2015 13K0095*Property of STI
try-catch Structure
13 ________________________
___________________________
___________________________
___________________________ ___________________________
___________________________
___________________________
___________________________
___________________________
11/17/2015 14K0095*Property of STI
Example
14 _________________________
___________________________
___________________________
___________________________ ___________________________
___________________________
___________________________
___________________________
___________________________
11/17/2015 15K0095*Property of STI
Output
15 ________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
11/17/2015 16K0095*Property of STI
Example
16 _________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
8/19/2019 06 LCD Slide Handout 1
5/9
11/17/2015 17K0095*Property of STI
Output
17 ________________________
___________________________
___________________________
___________________________ ___________________________
___________________________
___________________________
___________________________
___________________________
11/17/2015 18K0095*Property of STI
Question: What exception type will occur?
18 _________________________
___________________________
___________________________
___________________________ ___________________________
___________________________
___________________________
___________________________
___________________________
11/17/2015 19K0095*Property of STI
Answer
19 ________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
11/17/2015 20K0095*Property of STI
Appropriate catch Block
20 _________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
8/19/2019 06 LCD Slide Handout 1
6/9
11/17/2015 21K0095*Property of STI
Question: Will the messages be displayed when
the user enters zero as divisor?
21 ________________________
___________________________
___________________________
___________________________ ___________________________
___________________________
___________________________
___________________________
___________________________
11/17/2015 22K0095*Property of STI
No. The output will still be:
22 _________________________
___________________________
___________________________
___________________________ ___________________________
___________________________
___________________________
___________________________
___________________________
11/17/2015 23K0095*Property of STI
Example
23 ________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
11/17/2015 24K0095*Property of STI
The output will be:
24 _________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
8/19/2019 06 LCD Slide Handout 1
7/9
11/17/2015 25K0095*Property of STI
What will be the output?
25 ________________________
___________________________
___________________________
___________________________ ___________________________
___________________________
___________________________
___________________________
___________________________
11/17/2015 26K0095*Property of STI
The output will be:
26 _________________________
___________________________
___________________________
___________________________ ___________________________
___________________________
___________________________
___________________________
___________________________
11/17/2015 27K0095*Property of STI
finally contains statements which are executed
whether or not an exception is thrown
Format:
finally
{
//statement or statements that
will be executed
}
27 ________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
11/17/2015 28K0095*Property of STI
28 _________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
8/19/2019 06 LCD Slide Handout 1
8/9
11/17/2015 29K0095*Property of STI
User-Defined Exceptions
You can create a user-defined exception by
extending the Exception class.
29 ________________________
___________________________
___________________________
___________________________ ___________________________
___________________________
___________________________
___________________________
___________________________
11/17/2015 30K0095*Property of STI
CustomerAccount Class (Part 1)
30 _________________________
___________________________
___________________________
___________________________ ___________________________
___________________________
___________________________
___________________________
___________________________
11/17/2015 31K0095*Property of STI
CustomerAccount Class (Part 2)
31 ________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
11/17/2015 32K0095*Property of STI
Sample Output
32 _________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
8/19/2019 06 LCD Slide Handout 1
9/9
11/17/2015 33K0095*Property of STI
Modifying the Exception Class
(Part 1)
33 ________________________
___________________________
___________________________
___________________________ ___________________________
___________________________
___________________________
___________________________
___________________________
11/17/2015 34K0095*Property of STI
Modifying the Exception Class
(Part 2)
34 _________________________
___________________________
___________________________
___________________________ ___________________________
___________________________
___________________________
___________________________
___________________________