Java 7 – New Try Catch Blocks

85 / 100

Java 7 is still under Beta as time of writing and this time TechieDan would preview a whole new way of writing your try catch statement blocks.

java try catch

What is a try catch block?

It is good practice to be able to catch errors that are not handled by the user input so that the program would not throw an error with its own garbage error messages. Here is an example of what a garbage message would look like to a user screen.

Exception in thread “main” java.lang.NumberFormatException: For input string: “hello”
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at com.Test.TestSME.main(TestSME.java:10)

This was the program that created this garbage message using the command prompt.

public class TestSME {

public static void main(String... args) {
String result = null;
String a = "hello";
result = a;

System.out.println(Integer.parseInt(result));

System.exit(0);
}
}

From the code, there were no compilation errors. Errors will only be present at runtime.

So how does one handle it? Well, we can always use the try catch block.

public class TestSME {

public static void main(String... args) {
String result = null;
String a = "hello";
result = a;

try {
System.out.println("This is the result: " + Integer.parseInt(result));
} catch (Exception e) {
System.out.println("Cannot print result as result is not numeric, it is \"" + result + "\"");
}
System.exit(0);
}
}

Finally, no more garbage messages. If one were to run this program, it will produce this error message now.

Cannot print result as result is not numeric, it is “hello”.

Of course, something about this code is bad practice too. Most importantly, one should not catch Exception or throw plain Exception in a try catch handler.

Here is the reason why?

It’s very bad style to declare a method as throws Exception, because that forces the caller to always catch ALL Exceptions, including those he might have wanted to pass up to some more global error handler. Always use specific exception classes – in case there isn’t one handy, make up your own.

Here is a simple scenario then. By doing so, let us say that if the result was empty, it will also display the same error message and there is no meaning to the error message. So we want to catch the exception that was meant for that specific error.

Good practices come from writing good codes.

Of course, so how should we handle it?

try {
System.out.println("This is the result: " + Integer.parseInt(result));
} catch (NumberFormatException e) {
System.out.println("Cannot print result as result is not numeric, it is \"" + result + "\".");
} catch (NullPointerException e) {
System.out.println("result is null.");
}

Now this is multiple try catch blocks. This is good if there are multiple and different scenarios to catch errors which are specifically meant for that error type. Of course, the above would not really work as the NumberFormatException would eventually catch the NULL value too. It is just an example of how to write this in Java.

Of course, now let us look at Java 7. Java 7 has a different approach in terms of doing a catch block. It is easy to maintain and if one has to catch a few exceptions and give the same answer, it is best to use the Java 7 way.

A single catch block can handle more than one type of exception. This feature can reduce code duplication and lessen the temptation to catch an overly broad exception.

In the catch clause, specify the types of exceptions that block can handle, and separate each exception type with a vertical bar (|):

try {
System.out.println("This is the result: " + Integer.parseInt(result));
} catch (NumberFormatException | NullPointerException e) {
System.out.println("Cannot print result as result is not numeric, it is \"" + result + "\".");
}

Just by using the (|) bar, that is how easy it is for Java 7. I seriously love the flexibility of Java 7. What other exciting new features of Java 7?

One Response

  1. danielctw March 15, 2011
  2. Pingback: Proven Set Up JDK in Eclipse - TechieDan September 30, 2019

Leave a Reply