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

Contenido del Curso

Java JUnit Library. Types of Testing

Handling Multiple ExceptionsHandling 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.java

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.java

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.java

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.java

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?

Selecciona la respuesta correcta

In the provided example, what causes a NumberFormatException?

Selecciona la respuesta correcta

What is the purpose of combining multiple catch blocks?

Selecciona la respuesta correcta

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

Selecciona la respuesta correcta

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

Selecciona la respuesta correcta

¿Todo estuvo claro?

Sección 3. Capítulo 3
course content

Contenido del Curso

Java JUnit Library. Types of Testing

Handling Multiple ExceptionsHandling 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.java

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.java

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.java

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.java

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?

Selecciona la respuesta correcta

In the provided example, what causes a NumberFormatException?

Selecciona la respuesta correcta

What is the purpose of combining multiple catch blocks?

Selecciona la respuesta correcta

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

Selecciona la respuesta correcta

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

Selecciona la respuesta correcta

¿Todo estuvo claro?

Sección 3. Capítulo 3
some-alt