Exception

Exceptions are “exceptional condition.”
Exceptions allows the separation of error codes from the normal flow of program. Primary logic flows differently from the exception.
Exceptions handling allows developers to easily detect errors without writing special code to test return values.
throw keyword When exception event occurs, an exception is said to have thrown.
The code handling the event is an exception handler.

It catches the exception.

try- catch The try and catch code block is used to tell the compiler to look for errors.
It tells what code to execute when a certain errors happens.
The guarded region is the try code block.
try {

}

catch( MyFirstException) {

}

catch( MySecondException) {

}

Exam Tip If a catch block is used it must be prececed by he try block.
try- catch-finally The finally block allows for cleanup code to be executed. Closing of files, release network sockets, etc.
try {

}

catch( MyFirstException) {

}

catch( MySecondException) {

}

finally {

}

Exam Tip The finally block is executed regardless of whether an exception was thrown.
Exam Tip Finally block is not required.
Exam Tip Even if there is a return statement in the try block, finally get’s executed.
Exam Tip If there was an exception thrown, the finally block executes immediately after the proper catch block completes.
Exam Tip The try block must have either a catch or finally block. Catch before finally. Catch is not required.
No catch clause If a catch clause is not provided, that method using the try block is “ducking” the exception. The error propagates up the chain.
Call Stack Method call are placed on a method stack, call stack.
The stack trace can be printed to retrieve the method calls.
If an exception is not caught by the immediate it bubbles from the bottom of the stack up the call stack.
This is called exception propagation.
If the exception is not caught, the bubble will burst. The program will crash. A stack dump is produced on the screen.
The Thread that created the error or exception dies.
If the catch clauses do no defined the exception generated, the exception is propagated up the call stack. Exception matching is created.
Throwable The base class Exception is derived from Throwable class.

Throwable is derived from Object.

The following method allows the retrieval of information about exception:
printStrackTrace()

 

 

Prints a stack trace for the exception. It unwinds the stack. The result is printed upside down.
printStrackTrace( printStream p) Accepts a printStream object.
getMessage() Gets the message passed to the exception constructor.
toString() Gets the message passed to the exception constructor.
Throwable has two subclasses: Error and Exception.
Error Errors are unusual situations that not caused by program error. A program cannot recover from an error. Errors cannot be handled. It’s fatal.
Exception java.lang.Exception is the base class of all exceptions.
When an exception is thrown, an object of particular type of exception is instantiated.
That exception object is handed to the exception handler as an argument to the catch clause.
new Exception()
new Exception( String s) Message for the exception is passed.
Exception class derive:

RuntimeException and,

Slew of Exception classes (handling bugs)

Exception Hierarchy Exception class catches more than one type of exception in a single catch clause.
This is a catchall exception handler.
try {

}

catch (Exception e) {

e.printStackTrace();

}

Exam Tip A derived exception must be caught before the base exception. The compiler will treat this as an error if not done.
try {

}

catch (Sub…Exception e) {

}

catch (Exception e) {

e.printStackTrace();

}

Runtime/Checked Exceptions Runtime exceptions does not have to be caught. But, can be defined in an exception clause.
Runtime are unchecked exceptions. Exception derived from RuntimeException.
Checked exception are all other exception from the base Exception class.
Checked must be caught at:
The immediate place of declaration
Up the chain of method definition
Checked exceptions must have the try-catch block, or rethrow the exception.
method throws catchable Exception If a method throws a catchable exception its signature must declare that it does by using the throws keyword followed by the exception:
throws void myFunction() throws MyExceptin1, MyExceptin2 {

}

By specifying that a method throws an exception doe not mean it has to handle thrown exception in its code.
A method calling this method will either:

handle the exception by specifying a catch clause, or

indicate that it too throws that exception.

A method can flag that it throws a RuntimeException exception.
RuntimeException or Error object or subclasses are unchecked and are not required to be caught or declared has an exception.
Overriding Exception When a method is overriding, the subclass method may not specify different exception types then the base method did.
  • Same base Exception may be thrown in the subclass methods.
  • Subclass of the base Exception may be thrown in the subclass methods.
  • No Exception may be thrown in the subclass methods.

Any derived subclass of this subclass may not throw any exception. Specially the exception from the ancestral base class.

Throwing an Exception throw keyword
The throw keyword is used to throw a new exception:

throw new RuntimeException();

ReThrowing an Exception Rethrowing an exception can be achieved by using the throw exception. The method must indicate that it throws that exception.
catch (IOException e) {

e.printStackTrace();

throw e;

}

printStackTrace() The printStackTrace will unwind from the point of origin, even if the higher level methods prints the stack.
The stack trace information does not change.
fillInStackTrace() To hide the original location where the exception was created, use fillInStackTrace() method. The exception is thrown from the current location.
fillInStackTrace() returns an object of type Throwable.
Exam Tip If you rethrow the fillInStackTrace() returned object. The method must show that it throws a Throwable object.
public void foo() throws Throwable {…}
The Throwable catch clause may be used:

catch (Throwable e) {

}

However it is best to cast the fillInStackTrace() object returned.
Creating Own Exception The Exception class can be subclassed to create specialized exceptions.
try {

throw new MyException();

}

finally {

System.out.println(“Hello exception”);

}

Exam Tip If thrown exception is not caught, the finally clause is executed, then the thrown exception is printed and propagated up the call chain.
try {

System.out.println(“Line 1”);

}

catch (MyException e) {

System.out.println(“Line 2”);

e.printStackTrace();

}

finally {

System.out.println(“Finally line 3”);

}

Exam Tip If thrown exception is caught, the exception block is executed, then the finally clause is executed. The finally clause is not executed before the printStackTrace() method.

 

e.printStackTrace() prints first.

Exam Tip Becarful with the try-catch block.

The exception thrown might not be in the catch clause.

try {

factor = fload.valueOf( s ).floatVAlue();

}

catch (NumberFormatExcption e)

{}

 

If s is null NullPointerException is thrown.