Exception Handling In Java

What is Exception?

Exceptions are the unwanted situations or scenarios which occurs during the execution of the program. Due to this scenarios program terminated abnormally.

 

What is Exception Handling?

Exception handling is a way to handle the unwanted scenarios to avoid abnormal termination of the program, and provide the alternative way if any exception comes inside program.

 

Keywords use in exception Handling

  1. try – try is a block of statements which my throw exception.
  2. catch – catch block is use to handle the exception thrown from try block.
  3. throw – is use to throw exception manually. Mostly is use for Custom exception.
  4. throws – throws is use to populate the exception into a calling method.
  5. finally – finally is a block which gets executed always irrespective of try and catch execution.

 

 

Exception Hierarchy

 

Exception Hierarchy

 

 

Difference Between Exception And Error

ExceptionError
Exceptions are recoverableErrors are non-recoverable
Exceptions are mostly occurring due to logical problem into program.Errors are mostly occurring due to System issue.
By Handling exception, you can continue execution of the program.Error can be handled but you cannot continue the execution of program.

 

Exception Occurrence steps

  1. Identify the exception scenario.
  2. Find out the type of exception and create object of
  3. This exception object will be thrown from a statement level.

 

try keyword

  1. try keyword is use to write a statement which can throw exception.
  2. Try block always comes with the catch or finally or both.
  3. One try block is throwing more than one exception, then you can have more than one exception block for a single try.
  4. Syntax:

try {

//statement(s)

}

  1. Java 1.7 has introduce a try with resource block.
    1. This block is use to create a resources which must be close.
    2. These resources will be close automatically by java once execution come outside the try block.
    3. Try with resource can be use without catch or finally (which is not recommended), because internally this is a try-finally implementation.
    4. Syntax:

try (Resources/Object to close) {

//statement(s)

}

 

catch keyword

  1. Catch block is use to handle the exception thrown from try block statement.
  2. Inside catch block has to provide the type of exception which is to be catch by this block.
  3. If you are writing multiple catch for single try, then you have to follow the rule
    1. You have handle all child exception before parent exception
  4. Syntax:

catch(ExceptionType(Class)) {

//statement(s)

}

  1. Java 1.7 introduce catch with multiple exception.
    1. You can create a catch block which has multiple exceptions.
    2. This feature can be use to club the multiple exception in a single catch block which is having similar execution.
    3. You cannot write an exception which is having inheritance relation.
    4. Syntax:

catch(Exception1 | Exception2 | Exception3…  ex)

 

finally keyword

  1. Finally block is execute always, irrespective of the execution of try or catch.
  2. Finally block has to come with try block.
  3. Finally is mostly use for resource closing.
  4. Syntax:

finally {

statement(s)

}

 

throw keyword

  1. To throw any exception manually from the statement level throw keyword is used.
  2. Mostly Custom exceptions has to throw manually or you can also throw check or unchecked exceptions also.
  3. By Using Throw keyword you can throw and Object of exception.

 

throws keyword

  1. To populate the exception at method level.
  2. Throws keyword is use at method declaration level.
  3. By Throws keyword can throw exception outside method or at a calling method.
  4. Throws always followed with Exception class Name.
  5. You can declare more than one exception class for throws.

 

Types Of Exceptions

Unchecked Exception

  1. It is an exception for which handling can be skip.
  2. Here java will not restrict you to handle this exception.
  3. You can skip the exception handling for unchecked type of exception, but this exception occurs then program terminates abnormally.
  4. It is also known as runtime exception.
  5. Example: ArithmaticException, ArrayIndexOutOfBoundException, NullPointerException..

 

Check Exception

  1. It is an exception which has to handle at the time of coding else java compiler will not allow use to compile the program.
  2. Here java compiler will strictly check where checked exception is handle or not.
  3. There is very less or no chances of abnormal termination of the program.
  4. Examples: IOException, SqlException..

 

Steps to Create Custom Exception

  1. Create a Java class
  2. Extends that class with either an Exception class or any sub class of Exception class.
  3. In Custom Exception you can provide your custom exception execution.

 

 

Frequently Ask Question

  1. What is Exception Handling
  2. Difference between exception and error
  3. Difference between checked exception and unchecked Exception
  4. Difference between throw and throws
  5. Difference between finally and final keyword
  6. What is the use of finally keyword, and is there any situation where finally block will not execute?
  7. What is Custom Exception and how to create custom exception.
  8. Java 7 updations in exception handling.
  9. Use to try, catch keyword.
  10. Can we use try catch inside another catch block?

 

Join Telegram : Click Here

 

All Full Stack Java Study Material

 

Java the programing language

 

Job’s For Fresher

 

Share This Information To Your Friends and Your College Group’s, To Help Them !