09. exceptions
TRANSCRIPT
-
8/11/2019 09. Exceptions
1/21
Le Hong HaiUET-VNUH
Exceptions
-
8/11/2019 09. Exceptions
2/21
What is an Exception?
Event that occurs when somethingunexpected happens null.someMethod();
(new int[1])[1] = 0;
int i = string;
File not exists
..
2
-
8/11/2019 09. Exceptions
3/21
Exception Handling
Older programming languages such as C/C++have some drawbacks in exception handing
The programmers are not made to aware of theexceptional conditions
3
-
8/11/2019 09. Exceptions
4/21
Example
if(file exists) {
open file;
while(there is more records to be processed) {
if (no IO errors) {
process the file record} else{
handle the errors }
} if(file is opened)
close the file;
} else{report the file does not exist;
}
4
-
8/11/2019 09. Exceptions
5/21
Handing Exception in Java
Java overcomes these drawbacks by building the
exception handling into the language rather than
leaving it to the discretion of the programmers Exception handling codes are separated from the
main logic [Via the try-catch-finally construct]
5
-
8/11/2019 09. Exceptions
6/21
-
8/11/2019 09. Exceptions
7/21
Exception Handling Process
When an exception occurs inside a Java method, themethod creates an Exception object and passes(throw) the Exception object to the JRE
The JRE is responsible for finding an exception
handlerto process the Exception object. It searchesbackward through the call stack until it finds amatching exception handler for that particular classof Exception
If the JRE cannot find a matching exception handler inall the methods in the call stack, it terminates theprogram
7
-
8/11/2019 09. Exceptions
8/21
Exception Handling Process
8
-
8/11/2019 09. Exceptions
9/21
Exception message
To tell the code using your method thatsomething went wrongException in thread "main"
java.lang.ArrayIndexOutOfBoundsException: 5 at
RuntimeException.main (RuntimeException.java:8)
9
-
8/11/2019 09. Exceptions
10/21
Try-catch-finallytry{
// main logic, uses methods that may throw
Exceptions ...
} catch(Exception1 ex) {
// error handler for Exception1 ...
} catch(Exception2 ex) {
// error handler for Exception2 ... ...
} finally{
// finally is optional // clean up codes,
always executed regardless of exceptions ...
}
10
The finally-block is meant for cleanup code such as closing the file,
database connection regardless of whether the try block succeeds.
The finally block is always executed (unless the catch-block pre-
maturely terminated the current method).
-
8/11/2019 09. Exceptions
11/21
-
8/11/2019 09. Exceptions
12/21
Exception Classes
The base class for all Exception objects isjava.lang.Throwable, together with its twosubclassesjava.lang.Exceptionandjava.lang.Error
The Errorclass describes internal system errors. If such
an error occurs, there is little that you can do and theprogram will be terminated by the Java runtime
The Exceptionclass describes the error caused by yourprogram (e.g. FileNotFoundException IOException). Theseerrors could be caught and handled by your program
12
-
8/11/2019 09. Exceptions
13/21
-
8/11/2019 09. Exceptions
14/21
-
8/11/2019 09. Exceptions
15/21
-
8/11/2019 09. Exceptions
16/21
Example using custom exceptionpublic class MyMagicExceptionTest {
public static void magic(int number) throws
MyMagicException {
if (number == 8) {
throw (new MyMagicException("you hit the magic
number"));}
System.out.println("hello");
}
public static void main(String[] args) {
try {magic(9); // does not trigger exception
magic(8); // trigger exception
} catch (MyMagicException ex) {
// exception handler
ex.printStackTrace();} 16
-
8/11/2019 09. Exceptions
17/21
-
8/11/2019 09. Exceptions
18/21
-
8/11/2019 09. Exceptions
19/21
Example
test assumptions
Value > 0
String is an Integer
19
-
8/11/2019 09. Exceptions
20/21
Enable Assertion
Assertion can be disabled to ensure that they arenot a performance liability in the productionenvironment
It is disabled by default. To enable use theruntime command-line option
enableassertions (or ea)
> java -eaAssertionSwitchTest // enableassertion
20
-
8/11/2019 09. Exceptions
21/21
Enable Assertion in Eclipse
21