Listen to this article

What happens when there is any problem in our life? We try to fix it…:) similarly, when there is a problem in our Java program we fix it by means of what are called ‘Exceptions’!! 🙂

During the course of working with our programs, we will encounter different bugs in our program. These bugs are also called as ‘Exceptions’ and they can be fixed in different ways in Java. The following lists the ways in which ‘Exceptions’ can be handled:

  • By means of a ‘try-catch’ block
  • By stating that the method declares the exceptions

This is the first way of dealing with ‘Exceptions’.

Handling exceptions via the ‘try-catch’ block:

try{
   //some statements here that throw the 'FileNotFoundException'
}

catch(FileNotFoundException e){
//some statements here
}
 

You can see the above program and understand how the ‘try-catch’ block works.

This is the second way of dealing with Exceptions.

Point to remember: The current version of Java is Java 18 which was released on March 22,2022

Handling exceptions by declaring them:

We can also declare that a method declares the exceptions by the following code:

public void except() throws IOException

This declares that the method except() ‘throws’ the IOException and now the Java compiler will not complain any more and normal functioning of the program continues.

Types of Exceptions:

There are three types of Exceptions in Java. They are checked exceptions, Errors and RuntimeExceptions.

  1. Checked exceptions:

Programs which are expected to recover from bugs are known as ‘Checked exceptions’. They are handled by means of the ‘try-catch’ block or they are declared by the ‘throws’ keyword.

All exceptions are ‘checked exceptions’ except those classes which are under java.lang.RuntimeException or java.lang.Error.

Examples of checked exceptions include:

  • FileNotFoundException
  • EOFException
  • MalformedURLException
  • SQLException

2. Error

Programs which cannot recover from bugs and those which are external to the application are ‘Errors’. These belong to the java.lang.Error class and they are serious errors. These cannot be resolved by ‘try-catch’ construct or by declaring them.

One example of ‘Errors’ might be running out of memory or the inability to read a file because of a system problem

3. Runtime Exception

Programs are not expected to recover from ‘Runtime Exception’ as well. They need not be handled by the ‘try-catch’ block or by declaring them. We will have to fix the problem that is causing the Runtime Exception.

Some ‘Runtime Exceptions’ are trying to access an ‘array'(we will deal with ‘Arrays’ in a later chapter) which is out of bounds, encountering a number which is divided by zero.

All sub-classes of java.lang.RuntimeException are Runtime exceptions. Examples include:

  • ArithmeticException
  • ArrayIndexOutOfBoundsException
  • NullPointerException
  • IllegalArgumentException

Exceptions in Java is a vast topic by itself. I have just touched on the minimal points. In conclusion, here are the salient features of ‘Exceptions’.

  • There are three types of Exceptions – checked exceptions, Errors and Runtime Exception
  • Only checked exceptions are handled
  • Checked exceptions are handled via the ‘try-catch’ block or by declaring them
  • You can learn more about Exceptions by going through the API at this link

Come back tomorrow for a personal post! 🙂

I’m participating in #BlogchatterA2Z by Blogchatter 

(Visited 53 times, 1 visits today)

Related Posts

One thought on “Exceptions

Leave a Reply