Exception

Exception Handling

  • An exception is nothing but an event and it is a abnormal condition, which occurs during the program execution (i.e.) it alters normal flow of excecution of the program.
  • Simply exception is nothing but Runtime Error.
  • Error is nothing but unexpected result in the program. whenever we terminate the program the error will occur.
  • Exception Handling means to handle the exceptions by the programmer himself. But it is not meant that resolving the exception, here we are creating an alternative way to continue the rest of the program normally
  • Exceptions that are checked at compile time by the java compiler are called checked exceptions. And the exceptions that are checked by jvm are called unchecked exceptions.
  • If there is an exception in the program the data which we have written wiil be corrupted so the programmer should take care of this by using Exception handling.
  • In Exception handling we have 5 keywords i.e., ‘try , catch , throw , throws , finally’.

try :

try block is a set of statements in that statements we might get an exception that exception can throw to the catch block. (i.e.) the programmer has to write the statements which may get an Exception in Try block. By this even there is an exception the program will not get terminated.

Syntax:
		
try { statement1; statement2; here we might get a exception statement3; }

catch:

catch block is comes immediately after a try block without catch block try block is not there if in case it is there we get an compiletime error. In between we do not use any statement or code. Try should have atleast one catch block. For one try block we can use multiple catch blocks.

catch block can handling the exception if in case catch block is not handling the exception it throws the exception to the JVM.

Syntax:
		
try { statement1; statement2; Exception statement3; } catch (Exception e) { System.out.println(e); }

finally:

finally block is always executed before it reaches the final destination. One try block contains only one finally block.

At which time finally block cannot be executed?

  • When we call system.exit() in the try block then JVM stops the programming
  • In finally block also it contains any exception in the code at that time finally block cannot be executed.

If the code written in try block got an exception then it will be handled by the catch block and prints the statement which is written in catch block. And also prints the code whatever is there in finally block if it is there.

Syntax:
		
try { statement1; statement2; statement3; } catch (Exception e) { System.out.println(e); } finally { statement1; statement2; statement3; }
Ex:
		
public class Exception { public static void main(String[] args) { try { int a[] = {1,2,3,4}; System.out.println(a[4]); } catch (ArrayIndexOutOFBoundException e) { System.out.println("hii"); } finally{ System.out.println("geek"); } } }

The output for the above program is as following:
hii
geek
Here in the above program the code which is written the try block is properly handled by the catch block so the catch block also prints in the output.

But in case the code which is written in try block does not handled by the exception in catch blok then statements which we have written in catch block will not include in the output.

Ex:
		
public class Exception { public static void main(String[] args) { try { System.out.println(a[4]); } catch (ArithmeticException e) { System.out.println("hii"); } finally{ System.out.println("geek"); } } }

Here ArithmeticException is not the proper exception handled for array type.
So the ouput which we will get is by the following:
geek

Exception Hierarchy:

ExceptionHierarchy

Here Throwable is the parent class and these all are in Java.lang package. We have two types of Exceptions are there i.e., Checked Exceptions, Unchecked Exceptions.

Checked Exceptions:

Compile time exceptions are the checked exceptions these all are checked by the java compiler. All subclasses of exceptions excluding runtime exceptions called checked exceptions. These exceptions are handled by the programmer.

UnChecked Exceptions:

All the subclasses of runtime exception are called unchecked exceptions. JVM checks if an exception occurs in the program when the programmer does not provide a try catch block. Unchecked exceptions are handled by the JVM.

We have two types of Exceptions available that are

  1. Built-in Exceptions
  2. User-defined Exceptions.
Built-in Ecxeptions:

These are the Exceptions that are already defined in java and those are used to related exceptions.

1. Arithmetic Exception:

Whenever any value is dividing with Zero we get Arithmetic Exception.It is a subclass of runtime Exception.

		
public class Arithmetic { public static void main(String[] args) { int a = 0,b=3,c; c= b/a; System.out.println(c); } } In the above example we get Arithmetic Exception because of we cannot handle the exception. public class Arithmetic { public static void main(String[] args) { int a = 0,b=3,c; try { c= b/a; System.out.println(c); } catch (ArithmeticException e) { System.out.println(e); } } }

In the above example we handle the exception by using try & catch blocks.

2. Array Index out of bound Exception:

When we occur this Exception is when we try to access the element that does not exists i.e., index number is more than the Array size this exception will get.

		
public class ArrayException { public static void main(String[] args) { int x[] = {10,20,30,40}; System.out.println(x[5]); } }

In the above example we get Array index out of bound exception because of we take array size is 4 but we retrieve the value of index is 5 so we didn’t get it gives Array out of bound Exception.

		
public class ArrayException { public static void main(String[] args) { try { int x[] = {10,20,30,40}; System.out.println(x[5]); } catch (ArrayIndexOutOfBoundsException e) { System.out.println(e); } } }

In the above example we handle the Exception by using try&catch blocks at line number 5 we might get an exception.

3. Number Format Exception:

When a string value is not parsed successfully at run time.

4. NullPointerException:-

This type of Exception wil occur when refers to null object members.

5. StringIndexOutOfBoundException:-

This type of exception will occur when the size of the string is greater than the index or if it is negative.

6. IO Exception:-

This Exception will occur when the input-output operation failed or interrupted.

7. RunTimeException:-

This type of Ecxeption occurs during runtime.

8. NosuchMethodException:-

This type oh Exception will occur when we retrieve the method which is not available or found.

9. NoSuchFieldException:-

This Exception will occur whenever we will retrieve the fields or variables which are not available in present class.

10. fileNotFoundException:-

This exception we will get whenever we want to the retrieve the file which is not accessible.

UserDefinedExceptions:

These User-defined exceptions wil be usefull in case if the built-in exceptions are not allowed to explain the situation of the occurred exception.
We have the parent Exception class as it is parent class of all exceptions and we can create subclass to the Exception.

Throw:

Sometimes we have to create expilicitly the Exception object manually and let it catch and to handover to the JVM.
It is done by using the throw keyword.
Main purpose of this throw key word is nothing but to handover the object to JVM manually.

Throws :

Sometimes even if the programmer does not handle the RunTimeExceptions then the compiler will not give any error related to runtime exceptions.
But we have to follow the rule that the programmer should handle the exceptions if he does not want to handle the exceptions, should throw by using the throws keyword.

HTML Comment Box is loading comments...