Tuesday, 30 May 2017

Tutorial 25: More on Handling Exceptions



So in the last tutorial, we learnt that the Exception class is the parent class of all exceptions. We also learnt that exceptions are part of Java’s error handling mechanism. We then went on to see two ways to handle exceptions, which are: throwing exceptions and using a try-catch block. In this tutorial, we learn more about exceptions.

Now, let’s assume that you have a program that you expect to return a specific result, like zero. If the program doesn’t return zero, then it means that there is a problem. So, you see in this case, Java doesn’t force us, like in the examples in the last tutorial to handle the exceptions. What we want to do instead, is to tell Java that we know that something in the program might go wrong, and we want to tell it what to do to that end.

We can start by testing whether the value returned by the operation is zero using an “if” block like this:

if (int count != 0) {
}

Now, we want to tell Java that if “count” is not equal to zero, then an exception could arise. Let’s assume that when “count” is not equal to zero, an IOException is thrown. This is how we tell Java to throw the exception:

if (int count != 0) {
            throw new IOException();
}

Now, we can specify a message for the exception within the brackets of the exception’s constructor like this:

if (int count != 0) {
            throw new IOException(“There is an error in the operation”);
}

Then, we can add an “else” statement to tell Java what to do if there is no error, i.e. if “count” is equal to zero. 

if (int count != 0) {
            throw new IOException(“There is an error in the operation”);
} else {
            System.out.println(“Everything is working great!”);
}

So, if “count” is not equal to zero, the function that contains the above if-else block also has to throw an exception. Let’s assume that the name of the function is testCount();

public void testCount() throws IOException {
            int count = 5;
if (count != 0) {
                        throw new IOException(“There is an error in the operation”);
} else {
                        System.out.println(“Everything is working great!”);
}          
}

Also, when we declare the function testCount() in a main function of a class, we have to handle the exception in that main method. We can either throw the exception and in case of an error, print out a stack trace. Alternatively, we could surround the method call with a try-catch block, which allows us to print user-friendly messages instead of a stack trace.

Throwing the Exception from the Main Method


public static void main (String[] args) throws IOException {
            testCount();
}


Figure 1: More on throwing exceptions


Surrounding the Method with a Try-Catch Block



public static void main (String[] args) {
try {   
testCount();
} catch (IOException e) {
            e.printStackTrace();
}
}



Figure 2: Handling an exception using a try-catch block


But remember, we specified a message that can be displayed in the constructor of the IOException. To display this message to the user, you can use the getMessage() method like this:

public static void main (String[] args) {
try {   
testCount();
} catch (IOException e) {
            System.out.println (e.getMessage());
}
}

Alternatively, we could just print out a message on the console using the System.out.println() like this:

public static void main (String[] args) {
try {   
testCount();
} catch (IOException e) {
            System.out.println(“An error has occurred”);
}
}



Figure 3: A try-catch block that produces user-friendly information 

 

No comments:

Post a Comment