Error is a term used to describe any issue that arises unexpectedly and results in incorrect output.
Some common examples;
-Division by zero occurs
-Accessing the elements of an array beyond its range.
-Invalid input
-Hard disk crash
-Heap memory exhaustion

Logical Error
:- Occur due to poor understanding of problem or solution procedure
Syntactic Error
:- Arises due to poor understanding of the language itself
Runtime Errors: occurs while the program is running if the environment detects an operation that is impossible to carry out.

-Exception are run-time anomalies or unusual conditions that a program may encounter while executing.

Exception handling separates error-handling code from normal code.

It can be used to enhance readability and clarifies the code.

An exception is classified in Two types
Synchronous Exception:- The exception which occurs during the program execution due to some fault in the input data is known as synchronous exceptions.

Eg: Errors such as out of range, overflow, underflow

Asynchronous Exception:– The exception caused by events or faults unrelated (external) to the program and beyond the control of the program is called asynchronous exception

Eg. Keyboard interrupts, hardware malfunction, disk failure.

Image result for exception class java

Exception Handling Mechanism
The exception handling mechanism provides a means to detect and report exception circumstances.
-Hit the exception
-Inform that an error has occurred (Throw the exception)
– Receive the error information (Catch the exception )
– Handle the exception

Exception Handling Mechanism is built upon three keywords:
Try- is used to preface a block of statements that may generate exceptions.
The program leaves the try block when an exception occurs and enters the catch statement of the catch block

Nested try Statement: If an inner try statement does not have a catch, then the next try statement’s cast handlers are inspected for a match.

Throw:- When an exception is detected, it is thrown using a throw statement in the try block.
Syntax: throw exception
-Generates the exception specified by exception.
-throw must be executed within a try block.
-throw can also be executed within a function called from a try block
-throwing an unhandled exception causes the standard library function terminate() to be invoked.
-By default, terminate() calls abort() to stop the program, but we can specify our own termination handler

the throw keyword is used to explicitly throw an exception/custom exception.
throw new Exception_name(“Error Message”);

Throw either checked or unchecked exception.
throw new ThrowableInstance

ThrowableInstance must be an object of type Throwable/ subclass Throwable.

Two ways to obtain Throwable objects:
-Using a parameter into a catch clause.
-Creating one with the new operator

public class bank
public static void main(String args[])
int a=10;
int b=100;
//ArithmeticException e = new ArithmeticException("Error Message");
//throw e;
//throw new ArithmeticException();
throw new ArithmeticException("Error Message");
System.out.println("No error message");

Catch:– A catch block defined by the keyword catch catches the exception which is thrown by the throw statement in the try block and handle it appropriately.
If the type of object thrown matches the arg type in the catch statement the catch block is executed.
Otherwise, the program is terminated using the abort() function.
Multiple catch block: If more than one exception can occur, then we use multiple catch blocks.
After one catch statement executes, the others are bypassed.

Stack Trace: The sequence of method calls that brought control to the point where the exception occurred.

Checked Exception
Inherited from the core Java class Exception.
They represent an exception that is frequently considered “non-fatal” to program execution.

A checked exception must be handled in your code or passed to parent for handling.

Unchecked Exception
Represents error conditions that are considered “fatal” to program execution.
The program automatically terminates with an appropriate message.

To pass the exception “up the chain”, we declare a throws clause in our method or class declaration.

public class Test
public static void main(String args[])
{ try
{int a[]=new int[2];
System.out.println("Accessing element three:"+a[3]);
catch(ArrayIndexOutOfBoundException e)
System.out.println("Exception thrown: "+e);

The finally keyword is used to create a block of code that follows a try block.
A final block of code always executes, whether or not an execution has occurred.
Finally block must be followed by a try or catch block.

Usually, a finally block allows you to run any cleanup-type statement that you want to execute, no matter what happens in the protected code.

{//code }
catch(ExceptionType e)
{ //catch block }
{ //the finally block executes }


-If a method is capable of causing an exception that it does not handle, it must specify this behavior so that callers of the method can guard themselves against that exception.

type method-name parameter-list) throws exception-list
{ // body of method }

It is not applicable for Error or RuntimeException, or any of their subclasses

cannot be used to multiple exceptions

//custom exception
public class My_Exception
public static void main (String args [ ])
int x = 10;
if (x < 20 || x >100) throw new NumberRangeException( );
catch (NumberRangeException e)
System.out.println (e);

Exception Handling with Method Overriding in Java
If the superclass method does not declare an exception, subclass overridden method cannot declare the checked exception but it can declare the unchecked exception


class Parent{
void msg()
class ExceptionChild extends Parent{ 
 void msg()throws IOException{
 System.out.println("Exception in Child class");
public static void main(String args[]){ 
   Parent p=new ExceptionChild(); 

If the superclass method declares an exception, subclass overridden method can declare same, subclass exception or no exception but cannot declare parent exception


class Parent{
void msg()throwsArithmeticException
class ExceptionChild2 extends Parent{
 void msg()throws Exception {System.out.println("child");} 
 public static void main(String args[]){
 Parent p=new ExceptionChild2(); 
    }catch(Exception e){}