Академический Документы
Профессиональный Документы
Культура Документы
Exceptions
Suzana/Java II-Chapt2
4th Ed Chapter N - 1
Objectives
Define exceptions
Use try, catch and finally
Describe exception categories
Identify common exceptions
Suzana/Java II-Chapt2
4th Ed Chapter N - 2
Exceptions
What is an exception ? An event that is raised by Java during the running of a program
and disrupt the normal flow of the program execution.
What is the problem if the exception is NOT handled? A program may terminate
abnormally . Eg: Program unexpectedly quits while running .. and you might not see the
desired output.. and also you do not know what is happening !! )
What is the solution? Java provides programmers with the capability to elegantly handle
those runtime errors. It is referred to exception handling
Suzana/Java II-Chapt2
4th Ed Chapter N - 3
4th Ed Chapter N - 4
4th Ed Chapter N - 5
try
{
catch
{
}
(Exception ex)
System.out.println(Incorrect output: an integer is required);
X : If exception occur on this line (input), the rest of the line in the try clause are skipped.
Control is now transferred to the catch clause
Y: After the exception is caught and processed, the control is transferred to the next
statement after the try-catch block
Suzana/Java II-Chapt2
4th Ed Chapter N - 6
Suzana/Java II-Chapt2
4th Ed Chapter N - 7
Suzana/Java II-Chapt2
4th Ed Chapter N - 8
Suzana/Java II-Chapt2
4th Ed Chapter N - 9
error ?
Suzana/Java II-Chapt2
4th Ed Chapter N
- 10
Suzana/Java II-Chapt2
4th Ed Chapter N
- 11
4th Ed Chapter N
- 12
Suzana/Java II-Chapt2
4th Ed Chapter N
- 13
method2( )
{
try
{ invoke method2;
}
catch (Exception ex )
{ process exception;
}
throws Exception
}
}
catch exception
Suzana/Java II-Chapt2
throw exception
4th Ed Chapter N
- 14
Suzana/Java II-Chapt2
4th Ed Chapter N
- 15
Suzana/Java II-Chapt2
4th Ed Chapter N
- 16
4th Ed Chapter N
- 17
4th Ed Chapter N
- 18
An exception is thrown
in method3
method2( )
{
try
{
invoke method3;
statement5;
}
catch (Exception3 ex3)
{ process ex3;
}
statement6;
}
4th Ed Chapter N
- 19
//
Eg:
public void printStackTrace( ) // prints the Throwable object and its trace
information on the console
Suzana/Java II-Chapt2
4th Ed Chapter N
- 20
Suzana/Java II-Chapt2
4th Ed Chapter N
- 21
Suzana/Java II-Chapt2
4th Ed Chapter N
- 22
Finally clause
Occasionaly, you may want some code to be executed
regardless exception occurs or is caught
Java hasfinally clause to achieve this objective.
The syntax is :
try
{
statements;
}
catch(TheException ex)
{
handling ex;
}
finally
{
finalStatements;
}
Suzana/Java II-Chapt2
4th Ed Chapter N
- 23
Code
example :
Suzana/Java II-Chapt2
4th Ed Chapter N
- 24
Code
example
(cont)
Suzana/Java II-Chapt2
4th Ed Chapter N
- 25
Common Exceptions
Arithmetic exception the result of divide-by-zero
operation for integers int i = 12/0;
NullPointerException an attempt to access the data
or method of an object using a variable that is not refer to
an object Date d = null;
System.out.println(d.toString);
4th Ed Chapter N
- 26
Suzana/Java II-Chapt2
4th Ed Chapter N
- 27
4th Ed Chapter N
- 28
2.
3.
4.
Write the catch block that cathes that exception and prints out the
exception object. Print also a message Quitting
5.
Recompile and run the program. Compare the result with the one on the
paper just now
6.
tulip
4th Ed Chapter N
- 29
4th Ed Chapter N
- 30