Sei sulla pagina 1di 20

Exception Handling

Exception Definition
Exception Occurrence
Exception Handling
Exception Propagation
Exception

❑ Error occurred in execution time


❑ Abnormal termination of program
❑ Wrong execution result

❑ Provide an exception handling mechanism in


language system
✒ Improve the reliability of application program
✒ Allow simple program code for exeception check
and handling into source
Exception Definition

❑ Treat exception as an object


❑ All exceptions are instances of a class extended
from Throwable class or its subclass.

❑ Generally, a programmer makes new exception


class to extend the Exception class which is
subclass of Throwable class.
Exception Definition

class UserErr extends Exception { }


class UserClass {
UserErr x = new UserErr();
// ...
if (val < 1) throw x;
}
Exception Definition

❑ We can pass the object which contains a message for


the exception in string form

class UserErr extends Exception {


UserErr(String s) super(s); // constructor
}
class UserClass {
// ...
if (val < 1) throw new UserErr("user exception throw message")
}

[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

❑ Raised implicitly by system because of illegal


execution of program
❑ When cannot continue program execution any
more
❑ Created by Java System automatically
❑ Exception extended from Error class and
RuntimeException class

[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

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


Exception Occurrence

❑ Raised implicitly by system


❑ Raised explicitly by programmer
✒ throw Statement Throwable class or
its sub class
throw ThrowableObject;

[ThrowStatement.java]
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.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

[modifiers] returntype methodName(params) throws e1, ... ,ek { }

[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

❑ 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
✒ Finallyclause is executed independent of exception
and catch
[SystemHandler.java] , [FinallyClause.java]
Exception Propagation

❑ Manage exceptions by collecting them in a specific


method 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 is
propagated to calling method

❑ All executions are ignored until finding the exception


handler
[Propagate.java]
Exception Propagation
public class Propagate {
void orange() {
ArithmeticException Occurred
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
}

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

❑ Explicit Description for possibility of Exception


Occurrence

✒ 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

class MyException extends Exception { }


public class ClassA {
// …
public void methodA() throws MyException {
// …
if (someErrCondition())
throw new MyException();
// …
}
}

[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

Potrebbero piacerti anche