Exceptions in Java

In this article, we are going to discuss Exceptions in Java.  And following topics.

  • What is the meaning of exception handling?
  • Default exception handling in java
  • Hierarchy of Java Exception classes
  • Different Between Exception and Error
  • Types of Java Exceptions
  • Checked Vs Unchecked Exceptions
  • Fully checked Vs Partially checked

 

Introduction:

Exception: An exception is an unwanted or unexpected event, that disturbs the normal flow of the program i.e at run time is called an exception.

 

It is highly recommended to handle exceptions so that the program terminates normally. The main objective of exception handling is graceful (normal) termination of the program.

 

What is the meaning of exception handling?

Exception handling doesn’t mean we are repairing an exception. We have to define an alternative way to continue the rest of the program normally without any disturbance. This way of defining alternative is nothing but exception handling.

 

Example: Suppose our programming requirement is to read data from a remote file locating in India. At runtime, if the India file is not available then our program should not be terminated abnormally.

 

We have to provide a default local file to continue the rest of the program normally when the file is not found at runtime. This way of defining alternative is nothing but exception handling.

Example:

statement 1;
statement 2;
statement 3;
statement 4;
statement 5;//exception occurs
try
{
read data from London file
}
catch(FileNotFoundException e)
{
use a local file and continue the rest of the program normally
}
statement 6;
statement 7;
statement 8;
statement 9;
statement 10;

Suppose there are 10 statements in your program and there occurs an exception at statement 5 like FileNotFoundException, then the rest of the code will not be executed and the program terminated abnormally. If we provide a default local file to perform exception handling at statement 5, then the rest of the statement will be executed. That is why we use exception handling in Java.

Default exception handling in java:

  1. If an exception raised inside any method then that method is responsible to create an Exception object with the following information. Name of the exception.
  2. After creating that Exception object, the method handovers that object to the JVM.
  3. JVM checks whether the method contains any exception handling code or not. If the method won’t contain any handling code then JVM terminates that method abnormally and removes the corresponding entry from the stack.
  4. JVM identifies the caller method and checks whether the caller method contains any handling code or not. If the caller method also does not contain handling code then JVM terminates that caller method also abnormally and removes the corresponding entry from the stack.
  5. This process will be continued until main() method and if the main() method also doesn’t contain any exception handling code then JVM terminates main() method also and removes the corresponding entry from the stack.
  6. Then JVM handovers the responsibility of exception handling to the default exception handler.
  7. Default exception handler just prints exception information to the console in the following format and terminates the program abnormally.

 

Hierarchy of Java Exception classes:

Exception

The Throwable class is the root class of Java Exception hierarchy, which is inherited by two subclasses: Exception and Error. Throwable class present in java.lang package.

 

Different Between Exception and Error:

Exception:

Most of the case exceptions are caused by our program and these are recoverable by the programmer.

Ex: If FileNotFoundException occurs then we can use local file and we can continue rest of the program execution normally.

 

Error:

Most of the cases errors are not caused by our program these are due to lack of system resources and these are non-recoverable by programmers.

Ex: If OutOfMemoryError occurs being a programmer we can’t do anything the program will be terminated abnormally. System Admin or Server Admin is responsible to raise/increase heap memory.

Exception in java

 

Types of Java Exceptions:

There are mainly two types of exceptions: checked and unchecked.

  1. Checked Exception
  2. Unchecked Exception

So if we are talking about an error then an error is considered as the unchecked exception.

Checked Vs Unchecked Exceptions:

Checked Exception:

The exceptions which are checked by the compiler whether programmer handling or not, for smooth execution of the program are called checked exceptions. Checked exceptions are checked at compile-time but it is always occurring at runtime.

Example:
IOException, SQLException, etc.

 

Unchecked Exceptions:

The exceptions which are not checked by the compiler whether programmer handing or not, are called unchecked exceptions.  Unchecked exceptions are not checked at compile-time, but they are checked at runtime.

Example:
ArithmeticException, NullPointerException, ArrayIndexOutOfBoundsException etc.

 

Fully checked Vs Partially checked :

Fully checked:

A checked exception is said to be fully checked if and only if all its child classes are also checked.

Example:
1) IOException
2) InterruptedException

 

Partially checked:

A checked exception is said to be partially checked if and only if some of its child classes are unchecked.

Example:
Exception.

NOTES:-

  1. RuntimeException and its child classes, Error, and its child classes are unchecked and all the remaining are considered as checked exceptions.
  2. Whether exception is checked or unchecked compulsory it should occur at runtime only and there is no chance of occurring any exception at compile time.
  3. The only possible partially checked exceptions in java are  Throwable and Exception. 

 

Read More topics related to the collection.

 

Read more topics related to java

 

Hope this was helpful for you. If you have any questions please feel free to leave a comment. Thank you for reading.

Leave a Reply

Your email address will not be published.