except hand
Post on 05-Apr-2018
217 Views
Preview:
TRANSCRIPT
-
8/2/2019 Except Hand
1/20
Exception Handling
Exception Definition
Exception Occurrence
Exception Handling
Exception Propagation
-
8/2/2019 Except Hand
2/20
Exception Error occurred in execution time
Abnormal termination of program
Wrong execution result
Provide an exception handling mechanism inlanguage system
Improve the reliability of application program
Allow simple program code for exeception checkand handling into source
-
8/2/2019 Except Hand
3/20
Exception Definition
Treat exception as an object
All exceptions are instances of a class extendedfrom Throwable class or its subclass.
Generally, a programmer makes new exceptionclass to extend the Exception class which issubclass of Throwable class.
-
8/2/2019 Except Hand
4/20
Exception Definition
class UserErr extends Exception { }class UserClass {
UserErr x = new UserErr();
// ...if (val < 1) throw x;
}
-
8/2/2019 Except Hand
5/20
Exception Definition
We can pass the object which contains a message for
the exception in string form
[UserException.java]
class UserErr extends Exception {UserErr(String s) super(s); // constructor
}class UserClass {
// ...
if (val < 1) throw new UserErr("user exception throw message");}
-
8/2/2019 Except Hand
6/20
Hierarchical Structure of
Throwable Class
Object
Throwable
Error Exception
RuntimeException
...
... ...
-
8/2/2019 Except Hand
7/20
Definition of Exception
Error Class
Critical error which is not acceptable in normalapplication program
Exception Class
Possible exception in normal application programexecution
Possible to handle by programmer
-
8/2/2019 Except Hand
8/20
System-Defined Exception
Raised implicitly by system because of illegalexecution of program
When cannot continue program execution any
more
Created by Java System automatically
Exception extended from Error class and
RuntimeException class
[DivByZero.java]
-
8/2/2019 Except Hand
9/20
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
-
8/2/2019 Except Hand
10/20
Programmer-Defined Exception
Exceptions raised by programmer
Check by compiler whether the exception
handler for exception occurred exists or not If there is no handler, it is error
Sub class of Exception class
-
8/2/2019 Except Hand
11/20
Exception Occurrence
Raised implicitly by system
Raised explicitly by programmer
throw Statement
[ThrowStatement.java]
throw ThrowableObject;
Throwable class or
its sub class
-
8/2/2019 Except Hand
12/20
Exception Occurrence
class ThrowStatement extends Exception {public static void exp(int ptr) {
if (ptr == 0)throw new NullPointerException();
}public static void main(String[] args) {
int i = 0;ThrowStatement.exp(i);
}}
java.lang.NullPointerExceptionat ThrowStatement.exp(ThrowStatement.java:4)at ThrowStatement.main(ThrowStatement.java:8)
-
8/2/2019 Except Hand
13/20
Exception Occurrence
throws Statement
When programmer-defined exception is raised, ifthere is no exception handler, need to describe it in
the declaration part of method
[ThrowsClause.java]
[modifiers] returntype methodName(params) throws e1, ... ,ek { }
-
8/2/2019 Except Hand
14/20
Exception Handling
try-catch-finally Statement
Check and Handle the Exception
[ExceptionHandler.java]
try {
//
} catch (ExceptionType1 identifier) {
//
} catch (ExceptionType2 identifier) {
//
} finally {//
}
-
8/2/2019 Except Hand
15/20
Exception Handling
Default Exception Handler
When system-defined exception occurred, if
programmer does not deal with it, it would be
processed by default exception handler
Simple function to output error message and exit
Execution Order of Exception Handler
Finally clause is executed independent of exceptionand catch
[SystemHandler.java] , [FinallyClause.java]
-
8/2/2019 Except Hand
16/20
Exception Propagation
Manage exceptions by collecting them in a specificmethod to propagate the exceptions to calling method
To prevent scattering of exception handling
Exception Propagation Order
If there is no catch block to deal with the exception, it ispropagated to calling method
All executions are ignored until finding the exceptionhandler
[Propagate.java]
-
8/2/2019 Except Hand
17/20
Exception Propagation
public class Propagate {void orange() {
int m = 25, i = 0;i = m / i;
}void apple() {
orange();}public static void main(String[] args) {
Propagate p = new Propagate();p.apple();
}}
Output by Default Exception
Handler
ArithmeticException Occurred
java.lang.ArithmeticException: / by zeroat Propagate.orange(Propagate.java:4)at Propagate.apple(Propagate.java:8)
at Propagate.main(Propagate.java:11)
-
8/2/2019 Except Hand
18/20
Exception Propagation
Explicit Description for possibility of ExceptionOccurrence
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
-
8/2/2019 Except Hand
19/20
Exception Propagation
[MsgException.java]
class MyException extends Exception { }
public class ClassA {
//
public void methodA() throws MyException {
// if (someErrCondition())
throw new MyException();
//
}
}
-
8/2/2019 Except Hand
20/20
Summary of Exception Handling
Objectives
Making safer program by providing special mechanism
Situation of Exception Handling
Error Correction and re-calling of the method whichoccurred 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/otherexception to caller
Exit the program when raising the exception
top related