Java exception handling with easy explanation

Exception handling in java is a very important topic.Exception is an error which disrupt the normal flow of execution of the program.

Java provides the robust mechanism to handle this type of error known as Exception handling.

What is Exception handling ?


Exception handling is a mechanism which is used to handle run time errors such as IOException,SQLException,ArrayIndexOutOfBoundsException etc.

Example:

Suppose you have 100 number of lines of code & one of them code exception occurs then in this case we can handle this exception without disturbing the flow of program.

Types of java Exception

There are two  types of exception in java

1.Checked exception

2. UnChecked exception

1.Checked Exception

When the exception occur at the compile then this exception is known as Checked Exception.

 Example:IOException,ClassNotFoundException

2.Unchecked Exception

When the exception occur  at the run time then this exception is known as  UnChecked Exception.ArrayIndexOutOfBoundsException,NullPointerException

Java Exception handling keywords

Java provides different types of keywords in which we can handle the exception.

1.try-catch:

“try-catch” is used to handle the exception. try block contains business logic where exception may occur & catch block is used  to catch the exception such as ArrayIndexOutOfBounds,IOException etc.

2.finally:

“finally” block always execute whether exception occur or not.

3.throw

“throw” is used to throw the exception.

4.throws

“throws” is used to declare the exception.

Example of Exception handling

Public class ExceptionHandling
{
Public static void main(String[ ] arg)
{
try
{
Int a={1,2,3,4};
System.out.println(“index= ” +a[5]);
}
catch(ArithmeticException e)
System.out.println(“first exception handling program”);
}
}

Output:

ArrayOutOfBoundsException exception

First exception handling program

Java Exception handling- Table of contents

1.try block

Try block contains the business logic where exception may occur.it always followed by the catch block.

Syntax of try block

try
{
//business logic where exception may occur
}

catch block

catch block is a place where we  handle the exception.it always followed by the try block.A single try block may be multiple exception.

syntax of try-catch block

try
{
//business logic where exception may occur
}
catch(Exception e)
{
//handle exception here
}

Example:try-catch block

If an exception occurs in the try block  then control flow is passed to the catch block.

public class ExceptionHandling
{
public static void main(String[ ] args)
{
try
{
Int sum=10/0;
System.out.println(sum);
}
catch(ArithmeticException e)
{
//it will execute if exception occur
System.out.println(“try-catch block example with divide by zero”);
}
}
}

Output:

Try-catch block example with divide by zero

2.finally block in java

finally block always execute whether exception occur or not.

So inside the finally block we can keep the important code.

Syntax of finally block

try
{
//business logic where exception  may occur
}
catch(Exception e)
{
//handle the exception
}
finally
{
//it code always execute
}

Example of finally block

public class ExceptionHandling
{
Public static void main(String[ ] args)
{
try
{
int x=10/2;
System.out.println(x);
}
catch(NullPointerException e)
{
System.out.println(e);
}
Finally
{
System.out.println(“This is a finally block”);
}
}
}

Output:

This is a finally block

3.Throw and Throws keyword in java

Example of throw & throws

void Test( ) throws ArithmeticException,NullPointerException
{
//Here exception may occur
throw new ArithmeticException( );
}

Difference between throw & throws

ThrowThrows

1.it is used to throw exception explicitly.


1.it is used to declare exception one or more separated by commas.


2.it is used within the method


2.It is used with the method signature


3.Only one exception can throw at a time.


3.Multiple exception can through with the help of throw.


4.Checked exception can not be propagated only  through throw.

4.For propagating checked exception,throws keyword must have followed by class name.

4.final ,finally & finalize in java

Final keyword

final is reserved keyword in java we can not use as an identifier.

It can be used with the variables,methods and also class.

1.final variable

The final variable value  can not changed once initialized

Example

class Test
{
Public static void main(String[ ] args)
{

//This is non-final variable
Int x=10;
//This is the final variable 
final int y=5;
y=y+10 //Compile time error
}
}

2.final class

When any class declare final then we can not extend that class.

Example

final class Test
{
public static void main(String[ ] args)
{
//
}
}
class Test1 extends Test //Class Test can not be extended
{
Public static void main(String[ ] args)
{
}
}

3.final method

When any method declared final then this method can’t be override by the sub-class.

Example

public class Test
{
final  void run ( )
{
}
Public static void main(String[ ] args)
{
}
}

public class Test1 extends Test
{
//This method can not override due to final keyword
public void run( ) 
{
}
public static void main(String[ ] args)
{
}
}

Finally block

This block is used  with the exception handling.

It blocks always execute whether exception occur or not.

Example

public class Exception
{
Public static void main(String[ ] args)
{
try
{
Int x=10/0;
System.out.println(x);
}
catch(ArithmeticException e)
{
System,out,println(e);
}
Finally
{
System.out.println(“This finally block example”);
}
}
}

Output:

This is finally block example

Finalize

Finalize method is used for the purpose of object cleaning before object is garbage collected.

Example:

public class FinalizeProgram
{
void finalize( )
{
System.out.println(“garbage collection example”);
}
public static void main(String[ ] args)
{
FinalizeProgram obj=new FinalizeProgram( );
obj=null;
System.gc( ); 
}
}

Output

garbage collection example

Hope,I have explained all the topic very easily and step by step.If you have any doubt and need improvement this post you can comment.It would be grateful for me.

sumankumar80

Hello Friends,I have written and developed this website because you can learn technology easily ,prepare for the placement and also for the any type of examination . By :Suman Kumar

Leave a Reply

Your email address will not be published. Required fields are marked *