What is an exception?

An Exception can be anything which interrupts the normal flow of the program. When an exception occurs program processing gets terminated and doesn’t continue further. In such cases we get a system generated error message. The good thing about exceptions is that they can be handled. We will cover the handling part later in this same tutorial.

Reasons for Exceptions

There can be several reasons for an exception. For example, following situations can cause an exception – Divide by zero, Opening a non-existing file, Network connection problem, Operands being manipulated are out of prescribed ranges, class file missing which was supposed to be loaded and so on.

Difference between error and exception

Errors indicate serious problems and abnormal conditions that most applications should not try to handle. Error defines problems that are not expected to be caught under normal circumstances by our program. For example memory error, hardware error, JVM error etc.
Exceptions are conditions within the code. A developer can handle such conditions and take necessary corrective actions. Few examples –

  • DivideByZero exception
  • NullPointerException
  • ArithmeticException
  • ArrayIndexOutOfBoundsException

Advantages of Exception Handling

One of the important purposes of exception handling in Java is to continue program execution after an exception is caught and handled. The execution of a Java program does not terminate when an exception occurs. Once the exception is resolved, program execution continues till completion. This also causes user to see non-friendly error messages.

What happens when we do not handle exceptions?

Look at the below block of code. Here we are dividing a number by zero and hence causing a run time exception.

public class Maths
{
    public static void main(String args[])
    {
    //Declare numbers 
    int number1, number2 , result;
    //Initialize numbers 
        number1 = 0;
        number2 = 42 ;
    //Divide the two numbers 
        result = number2/number1;
        System.out.println("Next Statement in the code block.");
    }
}

Since there is no exception handling in the code the end user will see error message like this. The below error message is not friendly to user.

The better way is to handle exception using Try Catch Block

public class Maths
{
    public static void main(String args[])
    {
    try
    {
    //Declare numbers 
    int number1, number2, result;
    //Initialize numbers 
            number1 = 0;
            number2 = 42;
    //Divide the two numbers 
            result = number2 / number1;
            
    //This line will not get executed
            System.out.println("Next Statement in the code block.");
    }
    catch (ArithmeticException e)
    { 
    // Handle the exception here
            System.out.println("Your code got a exception divide by zero");
    }
    //The program will continue
        System.out.println("Continue with the program");
    }
}

The output will look something like below. As you can see user will see much meaningful message and we also have an option to continue with the program execution.

3 thoughts on “Why Exception handling?

  1. Wonderful goods from you, man. I have remember your stuff prior to and you’re just extremely fantastic. I really like what you’ve acquired here, really like what you’re stating and the way in which during which you say it. You make it entertaining and you still take care of to stay it sensible. I cant wait to learn far more from you. That is really a wonderful site.

  2. Hi there, just become alert to your blog via Google, and found that it’s really informative. I’m going to watch out for brussels. I will be grateful if you happen to continue this in future. Lots of people can be benefited from your writing. Cheers!

Leave a Reply

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