The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

jarkko.r (45) [Avatar] Offline
#1
On page 398, in Q7-2, option c: "If a method throws a checked exception, it must be either handled by the method or specified in its throws clause". It sounds like the same method would both throw an exception and handle it as well, which is not possible.
LazLondon (13) [Avatar] Offline
#2
Re: Method cannot both throw and handle the same exception (p. 398)
Hi All,

Correct me if I was wrong but the same method CAN throw an exception AND handle it as well.

INTERESTINGLY if an exception is thrown in a try block which is inside a catch block (IOException in the example below), then it must be caught on the same level depth, otherwise the exception wont be handled by that method, the exception cannot be caught using the outer catch block.(In the book Twist in the Tale 7.2 the exception can be caught using an outer catch block because the exception is thrown in a try block which is in a try block).

import java.io.*;

public class Exceptions {

public static void main(String[] args){
try {
exceptions();
} catch (Exception e) {
System.out.println(e.toString() + " is handled by main().");
}
}

static void exceptions() throws Exception{
try{
throw new FileNotFoundException("FIRST");
}
catch(FileNotFoundException fe){
System.out.println(fe.toString() + " is handled by exceptions().");
try{
throw new FileNotFoundException("SECOND");
}
catch(FileNotFoundException e){
System.out.println(e.toString() + " is handled by exceptions() - nested.");
}
try{
throw new IOException();
}
finally{}
}
catch(Exception e){
System.out.println(e.toString() + " is handled by exceptions().");
}
}
}
jarkko.r (45) [Avatar] Offline
#3
Re: Method cannot both throw and handle the same exception (p. 398)
Ah, yes. This is true, now I get what was meant. So this is no error.
mala.gupta (245) [Avatar] Offline
#4
Re: Method cannot both throw and handle the same exception (p. 398)
LazLondon,

Thanks for a detailed reply. Much appreciated!

With respect,
Mala