How multi-catch works - 1

"A few more interesting lessons. Oh, how I love teaching!"

"I'd like to tell you about how multiple catch blocks work. It's very simple: when exceptions occur in a try block, execution moves to the first catch block."

"If a type indicated in the parentheses of the catch block matches the type of the thrown exception, then execution starts inside that block. Otherwise, we move to the next catch block, where the same check is performed."

"If we run out of catch blocks and the exception hasn't been caught, it will be rethrown, and the current method will terminate abnormally."

"I see. The catch block that coincides with the exception type will be the one executed."

"Yes, right. However, in reality it's a bit more complicated. Classes can inherit other classes. If a Cow class inherits an Animal class, the Cow object may be referenced not only by a Cow variable, but also by an Animal variable."

"And?"

"Because all exceptions inherit Exception or RuntimeException (which also inherits Exception), they can still be caught using 'catch (Exception e)' or 'catch (RuntimeException e)'."

"And?"

"We can make two conclusions. First, you can use 'catch (Exception e)' to catch any exception. Second, the order of catch blocks matters."

"Here are some examples:"

"The ArithmeticException that occurs after we divide by 0 will be caught in the second catch block."

Code
try
{
    System.out.println("Before calling method1.");
    int a = 1 / 0;
    System.out.println("After calling method1. This will never be shown.");
}
catch (NullPointerException e)
{
    System.out.println("Null reference. Exception has been caught.");
}
catch (ArithmeticException e)
{
    System.out.println("Division by zero. Exception has been caught.");
}
catch (Exception e)
{
    System.out.println("Any other errors. Exception has been caught.");
}

"In the example below, the ArithmeticException will be caught in the first catch block, because all exceptions inherit Exception, i.e. Exception covers all exceptions."

Code
try
{
    System.out.println("Before calling method1.");
    int a = 1/0;
    System.out.println("After calling method1. This will never be shown.");
}
catch (Exception e)
{
    System.out.println("Any other errors. Exception has been caught.");
}
catch (NullPointerException e)
{
    System.out.println("Null reference. Exception has been caught.");
}
catch (ArithmeticException e)
{
    System.out.println("Divided by zero. Exception has been caught.");
}

"In the example below, ArithmeticException won't be caught. It will be rethrown to the calling method."

Code
try
{
    System.out.println("Before calling method1.");
    int a = 1/0;
    System.out.println("After calling method1. This will never be shown.");
}
catch (NullPointerException e)
{
    System.out.println("Null reference. Exception has been caught.");
}

"That cleared things up a bit. These exceptions are not the easiest topic."

"It only seems that way. They actually are one of the simplest things in Java."

"I don't know whether to be happy or sad about that…"

undefined
9
Task
New Java Syntax, level 9, lesson 7
Locked
Let's work with StringBuilder
You need to implement 2 methods using a StringBuilder: addTo(String, String[]) and replace(String, String, int, int). 1. The addTo(String, String[]) method adds to the string received as the first parameter, in order, all the strings in the array of strings received as the second parameter and retur
undefined
9
Task
New Java Syntax, level 9, lesson 7
Locked
Flipping strings
Using a StringBuilder in the reverseString(String) method, reverse the string that is received as a parameter.