Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Exception Definition
Exception Occurrence
Exception Handling
Exception Propagation
Exception
[UserException.java]
Hierarchical Structure of
Throwable Class
Object
Throwable
Error Exception
... RuntimeException
...
...
Definition of Exception
❑ Error Class
✒ Criticalerror which is not acceptable in normal
application program
❑ Exception Class
✒ Possible exception in normal application program
execution
✒ Possible to handle by programmer
System-Defined Exception
[DivByZero.java]
System-Defined Exception
❑ IndexOutOfBoundsException :
✒ When beyond the bound of index in the object which use
index, such as array, string, and vector
❑ ArrayStoreException :
✒ When assign object of incorrect type to element of array
❑ NegativeArraySizeException :
✒ When using a negative size of array
❑ NullPointerException :
✒ When refer to object as a null pointer
❑ SecurityException :
✒ When violate security. Caused by security manager
❑ IllegalMonitorStateException :
✒ When the thread which is not owner of monitor involves
wait or notify method
Programmer-Defined Exception
[ThrowStatement.java]
Exception Occurrence
java.lang.NullPointerException
at ThrowStatement.exp(ThrowStatement.java:4)
at ThrowStatement.main(ThrowStatement.java:8)
Exception Occurrence
❑ throws Statement
✒ When programmer-defined exception is raised, if
there is no exception handler, need to describe it in
the declaration part of method
[ThrowsClause.java]
Exception Handling
❑ try-catch-finally Statement
✒ Check and Handle the Exception
try {
// …
} catch (ExceptionType1 identifier) {
// …
} catch (ExceptionType2 identifier) {
// …
} finally {
// …
}
[ExceptionHandler.java]
Exception Handling
java.lang.ArithmeticException: / by zero
at Propagate.orange(Propagate.java:4)
at Propagate.apple(Propagate.java:8)
at Propagate.main(Propagate.java:11)
Exception Propagation
✒ System-Defined Exception
Do not need to announce the possibility of exception
occurrence
✒ Programmer-Defined Exception
When it is not managed in correspond method, the
exception type should be informed.
Use the throws clause
Exception Propagation
[MsgException.java]
Summary of Exception Handling
❑ Objectives
✒ Making safer program by providing special mechanism
❑ Situation of Exception Handling
✒ Error Correction and re-calling of the method
which occurred the exception
✒ Error Correction and continued execute without
re-calling of method
✒ Alternative way instead of giving up the
execution result
✒ After dealing with the exception, re-occur the
same/other exception to caller
✒ Exit the program when raising the exception