Multiple exceptions catching in one catch block Java

Posted By Kundan Ray Akela | 30-Dec-2014

We all know that Java 8 has been introduced with its great new features, but I wanted to share a feature of Java 7 that I have used very frequently in my coding. This feature is, handling multiple exceptions in one catch block.Yes, we can handle multiple exceptions in one catch block that will reduce the code duplication, and facilitate easier and more concise exception handling. Here is how you can convert your exception handling code prior from Java SE7 to Java SE7. 

public class ExceptionHandling
{
	public static void main( String[] args )
	{
   		try {
   			URL newUrl = new URL("http://www.yoursimpledate.server/");
   			BufferedReader reader = new 
				BufferedReader(newInputStreamReader(newUrl.openStream()));
   			String line = reader.readLine();
   			SimpleDateFormat formater = new SimpleDateFormat("MM/DD/YY");
   			Date date = formater.parse(line);
   		}
		// handle passing in the wrong type of URL.
   		catch(ParseException exception) {
			System.out.println(“Passing wrong type of Url::::”+exception);
		}
		// handle I/O problems.
		catch(IOException exception) {
			System.out.println(“IO exception:::::”+exception);
		}
		// handle date parse problems.
		catch(ParseException exception) {
   			System.out.println(“Date parse exception:::”+exception)	;	
		}
	}
}

The solution, if you wanted to have the same logic for two of the three cases above, say, the ParseException and the IOException, you can copy and paste the same code. But this is more for  inexperienced or lazy programmer that might think it would be okay to do the following:

public class ExceptionHandlingNovice
{
	public static void main( String[] args )
	{
   		try {
   			URL newUrl = new URL("http://www.yoursimpledate.server/");
   			BufferedReader reader = new 
				BufferedReader(new InputStreamReader(newUrl.openStream()));
   			String line = reader.readLine();
   			SimpleDateFormat formater = new SimpleDateFormat("MM/DD/YY");
   			Date date = formater.parse(line);
   		}
	// I am an inexperienced or lazy programmer here.
   		catch(Exception exception) {
			System.out.println(“Exception occured :::”+exception);
   		}
   	}
}

The biggest problem with the above code is that it could introduce unintended side effects. Any code in the try block could throw an exception that would be swallowed up with a blanket (Exception) catch clause. If an exception that is not a ParseException orIOException is thrown (for example, a SecurityException), the code would still catch it, but the upstream user would not be aware of what really happened. Swallowing up an exception like that makes problems difficult to debug.


In order to facilitate the programmer’s work, Java SE 7 now includes a multi-catch statement. This allows the programmer to combine a catch clause into a single block of code without the need to use a dangerous catch-all clause or copy entire blocks of code. 

public class ExceptionHandlingNew
{
	public static void main( String[] args )
	{
		try {
   			URL newUrl = new URL("http://www.yoursimpledate.server/");
   			BufferedReader reader = new BufferedReader(
   				new InputStreamReader(url.openStream()));
   			String line = reader.readLine();
   			SimpleDateFormat formater = new SimpleDateFormat("MM/DD/YY");
   			Date date = formater.parse(line);
   		}
	        // handle our problems here.
   		catch(ParseException | IOException exception) {
			System.out.pritnln(“Exception occured ”+exception);
   		}
   	}
}

I hope you will use multi-catch feature of Java SE7 for handling multiple exceptions.

Request for Proposal

Recaptcha is required.

Sending message..