In versions of java prior to java 7 each catch clause could handle only one type of exception.

With release of Java 7, oracle has done some good changes in exception handling mechanism also. Primarily these are improved catch block and redundant throws clause. Lets see how they got changed.

1. Improved catch block in Java 7

In this feature, now you can catch multiple exceptions in single catch block. Before Java 7, you was restricted to catch only one exception per catch block.

To specify the list of expected exceptions a pipe (‘|’) character is used.

Java program to catch multiple exceptions in single catch block.

try { //Do some processing which throws NullPointerException; throw new NullPointerException(); } //You can catch multiple exception added after 'pipe' character catch( NullPointerException npe | IndexOutOfBoundsException iobe ) { throw ex; }

If a catch block handles more than one exception type, then the catch parameter is implicitly final. In this example, the catch parameter ex is final and therefore you cannot assign any values to it within the catch block.

2. Redundant throws clause in Java 7

This feature liberate you from using throws clause in method declaration. See in below given example:

public class MultipleExceptionsInCatchBlock { public static void main(String[] args) { sampleMethod(); } public static void sampleMethod() //throws Throwable //No need to do this { try { //Do some processing which throws NullPointerException; I am sending directly throw new NullPointerException(); } //You can catch multiple exception added after 'pipe' character catch(NullPointerException | IndexOutOfBoundsException ex) { throw ex; } //Now method sampleMethod() do not need to have 'throws' clause catch(Throwable ex) { throw ex; } } }

Java runtime is intelligent enough to identify the exact exception class in caller method.

If you compile above code with Java 6 or earlier release, It will give compilation error and will ask you do to declare throws clause.

In my opinion, first improvement is definitely a good addition for Java developers. But, I really do not see the second addition so much relevant. Again, its my view.

Happy Learning !!

Article 6 of 6

Part of: Troubleshooting common Java errors and exceptions

Just how well do you know exception handling in Java? These 10 tough multiple-choice questions on checked and unchecked exceptions will test your error handling mettle.

Every time software runs, there's a potential for an error to occur that could grind the application to a halt.

The Java programming language provides a number of error-handling constructs that enable the developer to recover from and handle problems that occur at runtime. Some of those constructs include:

  • checked exceptions;
  • runtime exceptions;
  • system level errors; and
  • try, catch, finally blocks.

Think you've got a solid grasp on how to develop effective, error-proof Java code? Take this tough, multiple-choice, Java exception handling quiz and find out.

Dig Deeper on JSRs and APIs

  • In versions of java prior to java 7 each catch clause could handle only one type of exception.
    Java Exception handling best practices

    In versions of java prior to java 7 each catch clause could handle only one type of exception.

    By: Cameron McKenzie

  • In versions of java prior to java 7 each catch clause could handle only one type of exception.
    exception handling

    In versions of java prior to java 7 each catch clause could handle only one type of exception.

    By: Alexander Gillis

  • In versions of java prior to java 7 each catch clause could handle only one type of exception.
    What are checked vs. unchecked exceptions in Java?

    In versions of java prior to java 7 each catch clause could handle only one type of exception.

    By: Cameron McKenzie

  • In versions of java prior to java 7 each catch clause could handle only one type of exception.
    Either log or rethrow Java exceptions, but never do both

Part of: Troubleshooting common Java errors and exceptions

Article 6 of 6

When the code in a try block may throw more than one kind of exception?

When the code in a try block may throw more than one type of exception, you need to write a catch clause for each type of exception that could potentially be thrown. All of the exceptions that you will handle are instances of classes that extend this class.

What classes of exceptions may be caught by a catch clause?

A catch clause can catch any exception that may be assigned to the Throwable type.
When catching multiple exceptions that are related to one another through inheritance, you should handle the more specialized exception classes before the more general exception classes. To serialize an object and write it to the file, use this method of the ObjectOutputStream class.

How does JVM handle an exception?

The JVM is responsible for finding an exception handler to process the Exception object. It searches backward through the call stack until it finds a matching exception handler for that particular class of Exception object (in Java term, it is called " catch " the Exception ).