Java - Exception Handling

Exception handling is a one of the powerful mechanism in java. It provides a mechanism to handle runtime errors to maintain normal flow of program.

 

What is Exception?

In java, an exception is an event, which occurs during the execution of a program that disrupts the normal flow of the program's instructions. Exception is a object which is throws at runtime.

 

Advantages of Exception

The main advantage of exception handing is maintaining the normal flow of program.

 

Syntax:


try{
	statement….
}catch(){
	
}finally{
}

 

Keywords use in Exception Handling

throw:

It is possible for your program to throw an exception explicitly, using the throw statement.

throw ThrowableInstance;

ThrowableInstance must be an object of type Throwable or a subclass of Throwable.

The flow of execution stop immediately after throw statement, any statement not executed after this statement within same try ..catch block.

 

throws:

If method is capable of causing an exception that it does not handle, it must specify this exception to handle by its caller method. This throws clause use with method declaration.

<return type>  <Method-name>(Parameter List) throws Exception List By comma separated{

method code

}

If throws Exception than it must be handle by caller methods.

 

try-catch:

We use try-catch block for exception handling in our application.try is the start of the block and catch is the end of block in exception handling. We can have multiple catch with try and try-catch block nested also. catch block required parameter of type Exception.

 

finally:

finally block is optional and can we use only with try-catch. finally block always executed whether exception occur or not. finally block always use for close the resource.



try{
}catch(){
}finally{
Code here.
}


 

Exception Hierarchy:

Throwable

--Exception

------IOException

------SQLException

------RuntimeException

------------NullPointerException

------------NumberFormatException

--Error

------VirtualMachineError

------AssertionError

 

Types of Exception:

There are three types of exception

  1. Error
  2. Checked Exception
  3. Unchecked Exception

 

Error:

Error are exceptional scenario that are out of scope for application and it’s not possible to recover. E.g Hardware Failure, Network failure.

 

Checked Exception:

Checked Exceptions are exceptional scenarios that we can anticipate in a program and try to recover from it, for example FileNotFoundException. Exception is the parent class of all checked exception. We should catch this type of exception and provide useful message to user.

 

Unchecked Exception or Runtime Exception:

Runtime exception are occurs by bad programming for example trying to divide 4 by 0. RuntimeException is parent class of all unchecked exception. Runtime exception can be avoided by better programming.

 

Example:


public class SiteMapGenerator {

	public static void main(String[] args) {
		String str = null;
		try{
			if(str.equals("Testing")){ /* check condition for null to avoid runtime exception */
				System.out.println("Return true");
			}
		}catch(NullPointerException ex){
			System.out.println("Null pointer exception");
		}finally{
			str = null;
		}
		
	}
}