Sunteți pe pagina 1din 9

Exception

Exception Handling
Basic Exception Handling
try, catch, and finally
User-Defined Exceptions

11/17/2015

*Property of STI

K0095

1 _________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________

an event that occurs during the


execution of a program that disrupts the
normal flow of instructions
Two (2) types:
o Checked exception
o Unchecked exception

11/17/2015

*Property of STI

11/17/2015

*Property of STI

K0095

3 _________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________

2 __________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________

Sample Exceptions
When 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

K0095

Sample Exceptions
When a program attempts to divide a
value by 0
When a program tries to access an array
with an illegal index

11/17/2015

*Property of STI

K0095

4 __________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________

Exception Handling
The process used to change the normal
flow of code execution if a specified
exception occurs

11/17/2015

*Property of STI

K0095

5 _________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________

Checked Exception
Occurs during compilation
Cannot be ignored and should be
resolved to be able to execute the
program

11/17/2015

*Property of STI

K0095

6 __________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________

Checked Exceptions

Unchecked Exception
Occurs during execution
Also known as runtime exception
Ignored at the time of compilation

11/17/2015

*Property of STI

K0095

7 _________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________

11/17/2015

*Property of STI

K0095

8 __________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________

Unchecked Exceptions

11/17/2015

*Property of STI

K0095

9 _________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________

Unchecked Exceptions

11/17/2015

*Property of STI

K0095

10 _________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________

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/17/2015

*Property of STI

K0095

11

11 ________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________

10

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
}
11/17/2015

*Property of STI

K0095

12

12 _________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________

try-catch Structure

11/17/2015

*Property of STI

K0095

13

Example

11/17/2015

*Property of STI

13 ________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________

K0095

14 _________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________

Output

11/17/2015

*Property of STI

K0095

15

15 ________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________

14

Example

11/17/2015

*Property of STI

K0095

16

16 _________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________

Output

Question: What exception type will occur?


11/17/2015

*Property of STI

K0095

17

11/17/2015

*Property of STI

17 ________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________

11/17/2015

K0095

19

19 ________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________

18

18 _________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________

Answer

*Property of STI

K0095

Appropriate catch Block

11/17/2015

*Property of STI

K0095

20

20 _________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________

No. The output will still be:

Question: Will the messages be displayed when


the user enters zero as divisor?
11/17/2015

*Property of STI

K0095

21

21 ________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________

11/17/2015

*Property of STI

K0095

22 _________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________

Example

11/17/2015

*Property of STI

K0095

23

23 ________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________

22

The output will be:

11/17/2015

*Property of STI

K0095

24

24 _________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________

The output will be:

What will be the output?


11/17/2015

*Property of STI

K0095

25

11/17/2015

*Property of STI

25 ________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________

K0095

26

26 _________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________

finally
contains statements which are executed
whether or not an exception is thrown
Format:
finally
{
//statement or statements that
will be executed
}
11/17/2015

*Property of STI

K0095

27

27 ________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________

11/17/2015

*Property of STI

K0095

28

28 _________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________

User-Defined Exceptions

CustomerAccount Class (Part 1)

You can create a user-defined exception by


extending the Exception class.

11/17/2015

*Property of STI

K0095

29

29 ________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________

11/17/2015

*Property of STI

K0095

30 _________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________

CustomerAccount Class (Part 2)

11/17/2015

*Property of STI

K0095

31

31 ________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________

30

Sample Output

11/17/2015

*Property of STI

K0095

32

32 _________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________

Modifying the Exception Class


(Part 1)

11/17/2015

*Property of STI

K0095

33

33 ________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________

Modifying the Exception Class


(Part 2)

11/17/2015

*Property of STI

K0095

34

34 _________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________

S-ar putea să vă placă și