Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Handling Multiple Exceptions | Exceptions
Java JUnit Library. Types of Testing
course content

Course Content

Java JUnit Library. Types of Testing

Java JUnit Library. Types of Testing

1. Testing in Development
2. Unit Tests
3. Exceptions

bookHandling Multiple Exceptions

As mentioned in the previous chapter, we can have multiple exceptions, and for each of such exceptions, we can have a separate catch block.

Multiple `catch` blocks

Let's take a look at a code example that parses a String into an int and then performs division. In this case, there can be two exceptions:

  • ArithmeticException if we attempt division by zero;
  • NumberFormatException if it's impossible to convert the String into an int.

The method is very simple but throws enough exceptions.

Let's use this method in the main class and handle both exceptions using a try-catch structure:

java

main

copy
1234567891011121314151617181920212223242526
package com.example; public class Main { public static void main(String[] args) { try { // Call the method that might throw multiple exceptions String input = "10"; int divisor = 0; double result = divideAndConvert(input, divisor); System.out.println("Result: " + result); } catch (ArithmeticException e) { System.out.println("ArithmeticException caught"); } catch (NumberFormatException e) { System.out.println("NumberFormatException caught"); } } public static double divideAndConvert(String numberStr, int divisor) { int number = Integer.parseInt(numberStr); // Can throw NumberFormatException if (divisor == 0) { throw new ArithmeticException("You can't divide by zero!"); } return (double) number / divisor; // Can throw ArithmeticException } }

As you can see, we handle multiple exceptions, one after the other, using two catch blocks. If we catch the first exception, in our case, ArithmeticException, we enter the first catch block and display the necessary information on the screen.

If we catch the second error, which is NumberFormatException in this case, we enter the next catch block, where we display the required information on the screen.

We also give each of the errors an alias, 'e', which allows us to access the object and understand what went wrong.

This is done using e.getMessage(). In code, it will look like this:

java

main

copy
1234567891011121314151617181920212223242526
package com.example; public class Main { public static void main(String[] args) { try { // Call the method that might throw multiple exceptions String input = "10"; int divisor = 0; double result = divideAndConvert(input, divisor); System.out.println("Result: " + result); } catch (ArithmeticException e) { System.out.println("ArithmeticException caught: " + e.getMessage()); } catch (NumberFormatException e) { System.out.println("NumberFormatException caught: " + e.getMessage()); } } public static double divideAndConvert(String numberStr, int divisor) { int number = Integer.parseInt(numberStr); // Can throw NumberFormatException if (divisor == 0) { throw new ArithmeticException("You can't divide by zero!"); } return (double) number / divisor; // Can throw ArithmeticException } }

Combining multiple `catch` blocks

You may notice that we perform the same operation in both catch blocks, which hints at the idea that we could combine them. Java allows us to do that by catching both exception objects in a single catch block.

For example:

java

main

copy
123456789101112131415161718192021222324
package com.example; public class Main { public static void main(String[] args) { try { // Call the method that might throw multiple exceptions String input = "10d"; int divisor = 2; double result = divideAndConvert(input, divisor); System.out.println("Result: " + result); } catch (ArithmeticException | NumberFormatException e) { System.out.println(e.getClass().getSimpleName() + " caught: " + e.getMessage()); } } public static double divideAndConvert(String numberStr, int divisor) { int number = Integer.parseInt(numberStr); // Can throw NumberFormatException if (divisor == 0) { throw new ArithmeticException("You can't divide by zero!"); } return (double) number / divisor; // Can throw ArithmeticException } }

We've listed multiple possible exceptions that we can catch using the bitwise OR (|). This way, in one catch block, we catch two possible exceptions simultaneously and display a common message for them.

We can slightly shorten this catch block by using the parent exception class object: RuntimeException.

Let's implement this in the code below:

java

main

copy
123456789101112131415161718192021222324
package com.example; public class Main { public static void main(String[] args) { try { // Call the method that might throw multiple exceptions String input = "10"; int divisor = 0; double result = divideAndConvert(input, divisor); System.out.println("Result: " + result); } catch (RuntimeException e) { System.out.println(e.getClass().getSimpleName() + " caught: " + e.getMessage()); } } public static double divideAndConvert(String numberStr, int divisor) { int number = Integer.parseInt(numberStr); // Can throw NumberFormatException if (divisor == 0) { throw new ArithmeticException("You can't divide by zero!"); } return (double) number / divisor; // Can throw ArithmeticException } }

We can conclude that exceptions also have their own inheritance hierarchy, and we can substitute a child class for a parent class, as well as use dependency inversion.

1. Which exceptions does the `divideAndConvert` method potentially throw?
2. In the provided example, what causes a `NumberFormatException`?
3. What is the purpose of combining multiple catch blocks?
4. How can you catch multiple exceptions in a single catch block?
5. If you catch `RuntimeException`, what types of exceptions are you handling?
Which exceptions does the `divideAndConvert` method potentially throw?

Which exceptions does the divideAndConvert method potentially throw?

Select the correct answer

In the provided example, what causes a `NumberFormatException`?

In the provided example, what causes a NumberFormatException?

Select the correct answer

What is the purpose of combining multiple catch blocks?

What is the purpose of combining multiple catch blocks?

Select the correct answer

How can you catch multiple exceptions in a single catch block?

How can you catch multiple exceptions in a single catch block?

Select the correct answer

If you catch `RuntimeException`, what types of exceptions are you handling?

If you catch RuntimeException, what types of exceptions are you handling?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 3. Chapter 3
We're sorry to hear that something went wrong. What happened?
some-alt