An exception is a runtime error or warning condition, which can be predefined or user-defined. Predefined exceptions are raised implicitly (automatically) by the runtime system. User-defined exceptions must be raised explicitly by RAISE statements. To handle raised exceptions, you write separate routines called exception handlers.

** creating an exception object and throws to the runtime system is called as ‘Throwing an exception’.
** Exceptions can be handled. But an error can’t. Exceptions generally handled by the exception handler.
Exception Handler – the runtime system searches for ‘call stack’ for a method which contains a block of code that can be handled that particular exception. This block of code known as ‘exception handler’.
There are two types of exceptions are there –
a. Checked exceptions – this exception generated during compilation time. Checked exceptions are also known as compile-time exceptions as these exceptions are checked by the compiler during the compilation process to confirm whether the exception is handled by the programmer or not. If not, then the system displays a compilation error. For example, SQLException, IOException, InvocationTargetException, and ClassNotFoundException.
b. Unchecked exceptions – this exception generated during run time.The unchecked exceptions are those exceptions that occur during the execution of the program. Hence they are also referred to as Runtime exceptions. These exceptions are generally ignored during the compilation process. They are not checked while compiling the program. In java, exception and error occurs for Throwable class.

Let’sunderstand, what the difference between exception and error is.
Exception | Error |
can be solved | can’t be solved |
generate on runtime/compile time | generate on runtime |
Creates for programming error. | Creates for many reason like system error , out of memory etc. |
Exceptions can be handled. But an error can’t.Generally for handling an exception we need to use try-catch block. We will discuss about both of these below along with uses of finally block.
General form of an exception-handling block try { // block of code to monitor for errors } catch (ExceptionType1 exOb) { // exception handler for ExceptionType1 } catch (ExceptionType2 exOb) { // exception handler for ExceptionType2 } // … finally { // block of code to be executed after try block ends } |
Let’s discuss about keywords of exception handling –
* try block – try block contains program statements that are to be to monitored for exceptions, If an exception occurs within the try block, it is thrown
•catch block –contain statements that catch this exception and handle it in some rational manner, System-generated exceptions are automatically thrown by the Java runtime system.
•throw –is used to manually throw an exception
•throws –clause is used to specify any exception that is thrown out of a method
•finally –block contains code that absolutely must be executed after a try block completes
Exceptions in java while programming is basically bifurcated into two categories such as:
Build-In Exceptions: These are the types of exception which can be caught using already existing java libraries. It is also known as Unchecked Exception or Runtime Exception.
Some of the built-in exceptions are –
Arithmetic Exception
ClassNotFoundException
IOException
ArrayIndexOutOfBoundsException
FileNotFoundException
User-Defined Exceptions: These are the types of exception which can be caught using some of the customized exception created by the user and the user should have the ability to handle these exceptions. These exceptions can also be called as Checked Exception or Compile time exception.
Let’s see uses of try-catch block for exception handling –
Using try and catch
• Exception handling benefits
– allows you to fix the error
– prevents the program from automatically terminating
class code1 { public static void main(String args[]) { int d, a; try { // monitor a block of code. d = 0; a = 42 / d; System.out.println(“This will not be printed.”); } catch (ArithmeticException e) { // catch divide-by-zero error System.out.println(“Division by zero.”); } System.out.println(“After catch statement.”); } } |

Let’s see another example. When you don’t know what type of exception will occur, then you can use ‘Exception’ class.
public class code2 { public static void main(String[] args) { intarr[]={10,20,30}; System.out.println(“array size is “+arr.length); try{ arr[3]=67; } catch(Exception e){ System.out.println(e); } } } |

Discussion:‘e’ is just a variable. (‘e’ stands for exception, but you can rename it anything you like, however, the data type has to remain ‘Exception’) The ‘e’ variable stores an exception-type object in this case.
Your ‘catch’ block will literally ‘catch” an exception object that was ‘thrown’ at some point during a ‘try’ block and store it in the ‘e’ variable (a.k.a parameter)
Inside your catch block, you can choose to throw ‘e’ again and some higher-level process can catch the exception object (like yours can/did). If there are no other catch blocks at a higher level when it is thrown, the exception will cause the program to halt.
Multiple catch Clauses –
Use when more than one exception could be raised by a single piece of code
•When an exception is thrown, each catch statement is inspected in order, and the first one whose type matches that of the exception is executed
–All others are bypassed
–execution continues after the try / catch block
•NOTE: exception subclasses must come before any of their superclasses because a catch statement that uses a superclass will catch exceptions of that type plus any of its subclasses
class mulCatch { public static void main(String args[]) { try { int a = args.length; System.out.println(“a = ” + a); int b = 42 / a; int c[] = { 1 }; c[42] = 99; } catch(ArithmeticException e) { System.out.println(“Divide by 0: ” + e); } catch(ArrayIndexOutOfBoundsException e) { System.out.println(“Array index oob: ” + e); } System.out.println(“After try/catch blocks.”); } } |

Nested try block
When a try catch block is present in another try block then it is called the nested try catch block. Each time a try block does not have a catch handler for a particular exception, then the catch blocks of parent try block are inspected for that exception, if match is found that that catch block executes.
Syntax –
try { statement 1; statement 2; //try-catch block inside another try block try { statement 3; statement 4; } catch(Exception e1) { //Exception Message of ‘e1’ } } //Catch of Main(parent) try block catch(Exception e2) { //Exception Message ‘e2’ } |
Let’s understand through an example…
public class nestedTry { public static void main(String[] args) { intarr[]={10,20,30,50}; System.out.println(“arr length “+arr.length); try{ arr[1]=100; System.out.println(“inside try-main block”); try{ arr[4]=56; } // arr[4] -> invalid catch(ArrayIndexOutOfBoundsException e1){ System.out.println(e1); } } catch(Exception e2){ System.out.println(e2); } } } |
