Prev Next

Java / Exception

How do we handle more Than One Type of Exception using catch block in Java?

Using multiple catch blocks we may specify multiples exception to be handled in Java. For example, the below code handles both IOException and SQLException.

catch (IOException ioEx) {
     logger.log(ioEx);
     throw ioEx;
catch (SQLException sqlEx) {
     logger.log(sqlEx);
     throw sqlEx;
}

Although the above code handles multiple exceptions, we could notice that both blocks has same code and handling multiple exception sometimes lead to code duplication. Also in releases prior to Java 7, it is difficult to create a common method to eliminate the duplicated code because the exception variable has different types.

In Java SE 7 and later, multiple exception by a single catch block is introduced. This feature eliminates code duplication and reduce the possiblity to catch an overly broad exception.

The following example, which will execute in Java SE 7 and later, eliminates the duplicate code.

catch (IOException|SQLException exception) {
    logger.log(exception);
    throw exception;
}

The catch clause specifies the types of exceptions that each block can handle, and each exception type is separated with a pipe delimiter (|).

Note that if a catch block handles more than one exception type, then the catch parameter is implicitly final i.e. the exception variable value cannot be changed within the catch block.

❤Cash Back At Stores you Love !!!❤

Earn your $10 reward when you make your first purchase through Ebates by signing up with clicking below button.

Ebates Coupons and Cash Back

More Related questions...

How are the exceptions handled in java? Difference between Error and Exception in Java. What are checked and unchecked exceptions? How do we handle more Than One Type of Exception using catch block in Java? What happens if an exception is thrown from the finally or catch block in Java? Will the finally block be executed when the catch clause throws exception in Java? What is a user defined/custom exception in Java? How to create unchecked exception in Java? What will happen to the Exception object after exception handling in Java? How does finally block differ from finalize() method in Java? Does a finally block always run in Java? Does return statement allow finally block to execute in Java? Should a catch block always follow try block in Java for Exception handling? Explain finally block in Java. What is an Exception in Java? In Java 7, can we catch by grouping exceptions that are hierarchically related? What is try-with-resources statement in Java? Can we have multiple resources inside a try-with-resources in Java? What is the order the resources are closed using try-with-resources? Advantages of using try-with-resources in Java. What happens when the AutoCloseable resource is null in Java Try-With-Resource? Difference between Error and runtime exceptions in Java. Difference between throw and throws clause in Java. Can try block exist without any catch and finally block in Java? Will the resources be closed at finally/catch in try with resources? Order of closing resources in try with resources Java statement. What is exception chaining in Java? What is stack trace? What is the order of catch blocks when catching more than one exception? What are the improvements on Try-With-Resources in Java9? Can we have an empty catch block? Can we use FileNotFoundException and IOException in Java multi catch? Important methods of Java Exception Class. When do you subclass an exception? Can you catch OutOfMemoryError? Can you catch java.lang.Error? Give few examples of checked exceptions. Give few examples of unchecked exceptions. What is OutOfMemoryError in java? What is StackOverflowError in java? Explain exception handling when overriding a method? Can overridden method throw RuntimeException when original method throw ArithmeticException? Can we keep other statements in between try, catch and finally blocks? Can I write only try block without any catch and finally block? Explain Throwable class. Java Exception Class hierarchy. Difference between final, finally and finalize in Java. What is rethrowing an exception? Explain the rules of Exception Handling in terms of Method Overriding? Types of OutOfMemoryError in Java. Name some of the tools for probing Java Memory Leaks? Which exception advised not to be thrown from close method when implementing AutoCloseable? Which is the child interface of Autocloseable? What does close method throw: Closeable vs AutoCloseable? How do I rethrow an exception with custom message without losing stack trace?
Show more question and Answers...

Collections

Comments & Discussions