Sie sind auf Seite 1von 3

Java Exceptions Tutorial

Exception are used in Java to handle errors or any other exceptional event that occurs in the normal flow of program.
There are several way Exception can occur in Java.
1.

Data provided is not in expected format(eg. int instead of String).

2.

DB can not connected.

3.

Network connection Lost.

4.

Object is null.

Java Exception Hierarchy


Every Exception in Java is sub type of Exception class which in turn is the subclass of Throwable. And as we know
everything in Java derived from Object class Throwable also derived from class Object. Exception and Error are two
different classes that derived from Throwable. Errors represent situation which doesnt occur because of
Programming error but that might happen at the time of program execution and these are abnormal behavior which
java program can not handle and shouldnt bother to handle. JVM running out of memory is a type of Error which
can occur at runtime.

Checked vs UnChecked Exception


Checked Exception
1.

Checked exceptions are subclasss of Exception excluding RuntimeException and its subclasses.

2.

Checked Exceptions force programmers to deal with the exception that may be thrown.

3.

When a checked exception occurs in a method, the method must either catch the exception and take the
appropriate action,or pass the exception on to its caller.

Example

Unchecked Exception
1.

Unchecked exceptions are RuntimeException and any of its subclasses.

2.

Compiler doesnt force the programmers to either catch unchecked exception or declare it in a throws clause.

3.

Programmers may not even know that the exception could be thrown.

4.

Checked exceptions must be caught at compile time.


Runtime exceptions do not need to be. Arithmetic exception.

5.

Example
ArrayIndexOutOfBounds Exception.

Java Exception Handling


Now we know that exception can occur in Java program at any time(or Any Location). So we need to know how to
handle these exceptions. Handling exception is required attribute in developing robust application. Handling
Exception means transferring execution of program to appropriate handler when an exception occurs. We can handle
exception by using try catch block.
try: try is used to define block of code where exception can occur.
catch: catch is used to match specific type of exception. There could be more than one catch clause for one try block.
finally: finally determine block of code which will always execute after try block. Even in case of Exception.
try {
throw new IOException();
} catch (IOException e) {
// Handle only IO Exception
// This block will get executed only in case of IOException
}catch (Exception e) {
// Handle only all other type of exception
// This block will get executed in case of all exception except IOException
}finally{
System.out.println("This block will get executed no matter exception occur or not");

Note*: try clause can not stand without catch or finally block. Any of them is required.

Exception Propagation
It is not required to handle all exception thrown by try block in catch block (As it is not required block). In case catch
block doesnt handle exception thrown by try block, it will be propagated to method where this method was called
from. In case previous method which called this method also doesnt handle it, exception will propagated to previous
method in method stack and it will keep propagating in the same way unless some method handle it. In case none of
method handle it in call stack, exception will reach to bottom and it will be handled by JVM.

Cheat-sheet

Checked & unchecked exception are two type of Exception.

Checked exception are those exception which are subtype of Exception class but excluding classes which
extends Runtime exception.

Subtype of Error and Runtime Exceptions comes under unchecked Exception.

Finally block will always be invoked in all condition.

System.exit() is the only way when finally block will not get executed coz in that case JVM will shut down.

Custom Exception can also be created by extending Exception class.

Catch block should be order in the form of most specific to most general. Otherwise compiler will complain
for unreachable code.

Das könnte Ihnen auch gefallen