Core Java Tutorials


Introduction

Java Hello World

Java Compilation

JDK New Features

JVM Architecture

Java OOPs Concepts

Java Package

Array

Exception Handling

String

Immutable Class

File Handling

Serialization

MultiThreading

Volatile

Concurrent Package

Collection

Collection Internal

Generics

Cloning

Reflection In Java

Annotation

Class Loader

Java Inner classes

Garbage Collector

JDBC

References In Java

Heap Dump & Thread Dump

Exception Handling In Java

Exception Handling In Java is the mechanism to handle runtime error. We need to handle runtime error i.e. exceptions to prevent abrupt termination of program. The term exception means error which comes at runtime in java program. In Java program exception can come because of many reasons like invalid number , accessing array beyond it's size, wrong sql query etc.

Advantages of Exception Handling

Exception handling allows us to control the normal flow of the program.
Exception chaining to handle exception at common place.
It also gives us the scope of organizing and differentiating between different error types using a separate block of codes. This is done with the help of try-catch blocks.

Category of Exception.

There are two types of exceptions
Checked exceptions
Unchecked exceptions

What is checked Exception

Exceptions which are forced by the compiler to handle it at compile time to the programmer, are known as checked Exception. Note:- here programmer writes only try and catch block to handle the exception but exception will come at runtime only.
Example :FileNotFoundException, IOException, SQLException

What is Unchecked Exception

The exceptions which are not forced by the compiler to handle it at compile time to the programmer, are called unchecked exception
Example :ArithmaticException,NullPointerException,ArrayIndexOutOfBoundException
exception-type-in-java


How to Handle Exception with try-catch try-catch-exception

  Exception will only comes at runtime , in case of checked exception compiler only force to programmer to handle it in order to make code robust.

How to Handle Exception with throws Keyword

public class HandleException {

	public static void main()throws Exception {
		
		File f=new File("a.txt");   
	 }
  }  
}

In the above program we are trying to load a file on run time . so there may be a chance to file not available so compiler will force us to keep try-catch or use throws keyword with method.Basically throws keyword use for to delegate the responsibility to the caller

How to write code with try-catch and finally Keyword

In above example's we have already seen that how to handle exception and write code with try-catch and with throws keyword now let's take an example with finally block.
as we know finally block is always executable block.it will execute in both case if exception occur or exception not occur. now look at following example

public class HandleException {

	public static void main() {
	
	 try {
	 	
	   int c=10/0; // ArithmaticException
	   System.out.println("hello i am in try block");
	 		
	 }catch(Exception ex) {
	   System.out.println("hello i am in catch block");
	   
	 }finally {
	 System.out.println("hello i am in finally block");
	 }
  }  
}

Output

hello i am in catch block
hello i am in finally block

In the example we are trying to divide a number from zero so here we will get a run time exception saying "ArithmaticExcption" so control will go in catch block but after catch block it will be go in finally block .
Let's take an another example with try block

public class HandleException {

	public static void main() {
	
	 try {
	   int c=10/2;
	   System.out.println("hello i am in try block");
	 		
	 }catch(Exception ex) {
	   System.out.println("hello i am in catch block");
	 }finally {
	 System.out.println("hello i am in finally block");
	 }
  }  
}

Output

hello i am in try block
hello i am in finally block

In above example we are trying to divide a number from a genuine value so there is no chance to occur any excption but after try block control will go in finally block .
Basically finally is use for cleanup activity. if any resource may have open then we can close in finally block before program terminates. Look at following example

public class HandleException {

	public static void main() {
	
	Connection con=null;
	 try {
	 	
	    con=DriverManager.registerDriver(" "," "," ");254
	    int a=10/0;//ArithmaticException
	    System.out.println("hello i am in try block");
	 	//con.close(); 	
	 }catch(Exception ex) {
	 
	   System.out.println("hello i am in catch block");
	   
	 }finally {
	 
	 	con.close();
	 }
  }  
}

In above example we are trying to make connection to database so may be a chance is there our connection may close, because at line no. 2 we will get an exception but we can close our resource in finally block