Java Exceptions , Errors , Exception Handling in Java

By Saruque Ahamed Mollick

In this article we are going to learn Java Exceptions, Errors , How to handle those exceptions, different types of exceptions with some easy and understandable examples.

What is an exception?

Every application or program has its normal flow , but sometimes the normal flow of the program is disrupted. An exception is nothing but a reason behind the disruption of the program or application.
So for exceptions or exceptional events occurrence the program is going to be terminated abnormally.
Now you might think that this is ridiculous for the programmer as when the exception occurs we usually get an error message which is system generated. But nothing to worry as these exceptions can be handled. We will learn those process step by step later in this article.

When exception usually occurs?

We can divide an program into two parts. Part one is the compilation part and 2nd part is running or execution of the program.
When an exception occurs during compilation of the program it is knows as compile time exception and when an exception occurs during the execution of the program it is known as runtime exception .

Reasons of exception in java :

There is not only a particular reason of java exception. There are many several reasons. I would like to give several possible reasons of java exception

  • suppose you are trying to open a file which does not exist on the path , then an exception will be occurred. (known as FileNotFoundException)
  • Network connection problem
  • An invalid input data given
  • JVM Runs out of memory
  • Class file missing which was supposed to be loaded

and there is a lot of other reasons too.

Some of these exceptions are occurred by user , some exceptions are made by the programmer and it is noticeable that in many cases we found exceptions for hardware problem.
so based on this reasons the exceptions are divided into two categories.

  1. Checked exception or compile time exception
  2. Runtime exception  or execution time exception

Types of Exceptions with examples:

  • Checked exceptions or compile time exception:

If an exception is occurred during the compilation time then it is called a checked exception or compile time exception. So in easy words we can say that all exceptions except runtime exceptions are known as checked exception as the compiler checks the exception during the compilation time to notice whether the programmer has handled them or not. In case if it is found that the exception/exceptions is/are not handled in the program then it will show a compilation error ( I mean exception )

  Example of checked exception:

  • FileNotFoundException
  • ClassNotFoundException
  • IllegalAccessException
  • EOFException
  • NoSuchFieldException   and lots of ……

Lets see an example of checked exception  with easy coding

suppose you are trying to read a file by using FileReader, but the file does not exist in the specified path. In this situation  FileNotFoundException will occur.

import java.io.File;
import java.io.FileReader;

public class File_missing_exception {

   public static void main(String[] args) {		
      File file = new File("D://myfile.txt");
      FileReader fr = new FileReader(file); 
   }
}

try to compile the above code , you will find out the below message

C:\>javac File_missing_exception.java
File_missing_exception.java:8: error: unreported exception FileNotFoundException;
  •  Unchecked Exceptions:

Runtime exceptions are known as unchecked exception as the compiler does not check if the programmer handled the exception or not. If you don’t use API properly you may face runtime exception.
Though its our responsibility to handle the exceptions as much as we can in the program.

lets take an example. you have declared an array of size 3 and you are trying to reach at the 4th element, you can say its not possible as the size of the element is 3. so if we run this program it will show us an message.

public class Array_exception{
   
   public static void main(String[] args) {
      int element[] = {1, 2, 3};
      System.out.println(element[4]);
   }
}

This exception is known as ArrayIndexOutOfBoundsException.

some other examples

  1. if we try to divide a number by zero it will show us an error message. these type of exceptions are known as ArithmeticException.
int a=544454/0;//ArithmeticException

Java Errors

Errors are basically those serious problems which should not be handled by the application. example : memory errors, JVM errors.

generally you can do nothing  in your code to control errors.

Java Exceptions Handling:

Since I have discussed about exceptions now you all know that exceptions are not good to be in a program. So we have to handle those exceptions carefully.
there are some keywords (special keywords) to handle those exceptions that might be occurred in a program.

  1. try
  2. catch
  3. finally
  4. throw
  5. throws

java exception handling is nothing but a mechanism to resolve the exceptions that might be occurred.

we will learn about these blocks and keywords in my next article

Leave a Reply

Your email address will not be published. Required fields are marked *