"Ah. There you are. I've been looking for you."

"Did something happen?"

"No, but we're still studying."

"OK. I'm listening."

"I want to tell you a couple more things about exceptions:"

"In Java 7, the try-catch construct was extended slightly through the addition of multiple catch blocks. Look at this example:"

Java 5
try
{
  …
}
 catch (IOException ex)
{
 logger.log(ex);
 throw ex;
}
 catch (SQLException ex)
{
 logger.log(ex);
 throw ex;
}
Java 7
try
{
  …
}
 catch (IOException | SQLException ex)
{
 logger.log(ex);
 throw ex;
}

"So now can we write multiple exceptions separated by OR operators ('|' is binary OR)?"

"That's right. Isn't that convenient?"

"Hmm. But what will the type of the exception object inside the catch block?"

"After all, an IOException has its methods, and a SQLException has its methods."

"The exception type will be that of their common ancestor class."

"Ah. In other words, it will most likely be Exeption or RuntimeException. Then why not simply write catch(Exception e)?"

"Sometimes when handling errors individually, it's convenient to group them, writing some errors to a log, rethrowing others, and handling others in some other way."

"In other words, this scheme is recognized as solving the problem of duplicate catch blocks for handling different errors."

"Ah. I get it. Thanks, Ellie."

"That's not all. I want to tell you a little more about the finally block."

"As you probably already know, this block is always executed."

"And when I say always, I mean absolutely always."

"For example:"

Example using finally
try
{
 return 1;
}
 finally
{
 return 0;
}

"Here there's a return in the try block, and a return in the finally block. So this method's return value will be the number 0."

"The finally block will execute no matter what happens. And its return statement overwrites the other return value with its own value."

"I see."

"However, a method can either return a value or throw an exception."

"So, if a value is returned in a try block, but the finally block throws an exception, then the result will be an exception."

"What if an exception is thrown in the try block but the finally block has a return statement?"

"Then it is as if the method worked properly and the value in the return statement is returned.

Example Result
try
{
 return 1;
}
 finally
{
 return 0;
}
0
try
{
 return 1;
}
 finally
{
 throw new RuntimeException();
}
RuntimeException
try
{
 throw new RuntimeException();
}
 finally
{
 return 0;
}
0
try
{
 throw new RuntimeException();
}
 finally
{
 throw new IOException();
}
IOException

"The only reason the finally method might not be executed would be the program's immediate termination by a call to the System.exit(); method."

Example
try
{
 System.exit(0);
 return 1;
}
 finally
{
 return 0;
}

"I see."

"Keep in mind that all of these topics are usually asked about in interviews, so it would be good for you to understand and remember them."