Java Exception

Java Exception

Exception Handling in Java is a powerful mechanism. Exception handling is done to handle the run time error of the program and to maintain normal flow.

Exception refers to an abnormal event, which creates a kind of intrapt between tasks. This destroys the normal flow of the work.


    It is an object which is thrown at runtime execution.


#What is Java Exception

An exception is a problem that arises during the execution of a program. An exception can occur for many different reasons, including the following:


  1. By entering invalide data,

  2. Want to access files that doesn’t exist in the local machine or server,

  3. A network connection has been lost between the server and client,

  4. JVM runs out of Memory.



Some of these exceptions are caused by user error, others by programmer error, and others by physical resources that have failed in some manner.


#What is Exception Handling

Exception Handling is a powerful technique through which ClassNotFoundException, IOException, SQLException, RemoteException, etc. This type of error is handled by JAVA. It can be done very nicely in Java.

To understand how exception handling works in Java, you need to understand the three categories of exceptions:

 

  1.    Checked Exception

  2.    Unchecked Exception

  3.    Error 


#Checked Exception: It is an exception that is typically a user error or a problem that cannot be foreseen by the programmer.


All classes that inherit the Throwable class directly, except the RuntimeException and error classes, are called Checked Exceptions.


For example, if a file is to be opened, but the file cannot be found, an exception occurs. These exceptions cannot simply be ignored at the time of compilation.It means, In compile time, these exceptions are checked wheather it occurs or not.




#Unchecked Exception: An Unchecked exception is an exception that occurs at the time of execution, these are also called as Runtime Exceptions.


Classes that directly inherit RuntimeException are called Unchecked Exception.


Example: ArithmeticException, NullPointerException, ArrayIndexOutOfBoundsException etc.In runtime, this exception is checked, means wheather it occurs or not.



For example, if you have declared an array of size 10 in your program, and trying to call the 11th element of the array then an ArrayIndexOutOfBoundsExceptionexception occurs.


#Error:These are not exceptions at all, but problems that arise beyond the control of the user or the programmer. If there is an error in the code, it is not possible to fix it in runtime and compile time.


Errors are typically ignored in your code because you can rarely do anything about an error.


E.g. OutOfMemoryError, VirtualMachineError, AssertionError etc


#Advantage of Exception Handling

Maintaining the normal flow of the program is the main task of Exception Handling.

let's consider the given examples:


statement 1;

statement 2;

statement 3;

statement 4;

statement 5; // exception occurs

statement 6;

statement 7;

statement 8;

statement 9;

statement 10;

Suppose, there are 10 statements here. If there is an exception error in statement 5 then the remaining 8 to 10 generation statements will no longer be executed. If we do Exception Handling here then the rest of the statements will be executed without any hassle.


#Hierarchy of Java Exception classes


The root class of the Java Exception hierarchy is java.lang.Throwable class. Exception and error inherit the Throwable class.

java exception


#The sub-classes of Exception are:

  1.     IOException
  2.     SQLException
  3.     ClassNotFoundException
  4.     RunTimeException

#The sub-classes of RunTimeException are:

  1.     ArithmeticException
  2.     NullPointException
  3.     NumberFormateException
  4.     IndexOutOfBoundsException


#The sub-classes of IndexOutOfBoundsException are:

  1.     ArrayIndexOutOfBoundsException
  2.     StringIndexOutOfBoundsException



#The sub-classes of Error are:

  1.     StackOverFlowException
  2.     VirtualMachineException
  3.     OutOfMemoryException


Let's drive into deeper,

#Catching Exceptions:

try and catch keywords is used to display exception. A try/catch block is placed around the code that might generate an exception. Code within a try/catch block is referred to as protected code, and the syntax for using try/catch looks like the following: 

try

{

   //Protected code

}catch(ExceptionName e1)

{

   //Catch block

}

 

Every try block should be immediately followed either by a catch block or finally

block.

 

#Multiple catch Blocks:  

 

A try block can be followed by multiple catch blocks. The syntax for multiple

catch blocks 

looks like the following:

 

 

try

{

   //Protected code

}catch(ExceptionType1 e1)

{

   //Catch block

}catch(ExceptionType2 e2)

{

   //Catch block

}catch(ExceptionType3 e3)

{

   //Catch block

}

 

 

 

The previous statements demonstrate three catch blocks, but you can have any 

 

number of them after a single try.

 

#The finally block  

 

The finally block follows a try block or a catch block. A finally block of code

 always executes,Using a finally block allows you to run any cleanup-type

 statements that you want to execute, no matter what happens in the protected

 code.


A finally block appears at the end of the catch blocks and has the following syntax:

 

try

{

   //Protected code

}catch(ExceptionType1 e1)

{

   //Catch block

}catch(ExceptionType2 e2)

{

   //Catch block

}catch(ExceptionType3 e3)

{

   //Catch block

}finally{

   //The finally block always executes.

}


#Note the following: 


  • A catch clause cannot exist without a try statement.

  • It is not compulsory to have finally clauses whenever a try/catch block is present.

  • The try block cannot be present without either catch clause or finally clause.

  • Any code cannot be present in between the try, catch, finally blocks.

 

 

Next, we will disscuss about several types of exception with detailed examples, stay tuned!



You may like these posts