0

I have a noobish question. If you have a nested try-catch block and an exception is thrown in the inner catch block, will the outer catch block catch it? Example:

try {             // All this code is random. It doesn't
     int x = 5;   // have anything to do with my actual program;     
     Scanner sin = new Scanner(new File("file.txt"));
     String temp = "Howdy Y'all";
     try {
          x = Integer.parseInt("5f");    // EXCEPTION!
     } catch {                               // Caught it.
          x = Integer.parseInt(temp);        // NEW EXCEPTION!
     } 
} catch {
// Did the NEW EXCEPTION get caught here?
}

I'm almost positive the answer is yes, but want to be sure before I finish the code.

Edited by leverin4: n/a

3
Contributors
3
Replies
4
Views
7 Years
Discussion Span
Last Post by leverin4
0

Yes, if the catch expression is appropriate to catch that particular exception type. The exception will be passed up the call stack until it is handled or causes termination.

0

Since Ezzaral already answered your question - doesn't your code have syntax errors? You didn't specify a type of Exception in the catch block i.e. catch(Exception e). PS, for things like this, usually the best way to find out is to run a small sample program and see what the output is, if you get errors, etc. Nothing wrong with posting it here, just saying.

Edited by BestJewSinceJC: n/a

0

Yes, if the catch expression is appropriate to catch that particular exception type. The exception will be passed up the call stack until it is handled or causes termination.

Thanks, that's what I figured.

Since Ezzaral already answered your question - doesn't your code have syntax errors? You didn't specify a type of Exception in the catch block i.e. catch(Exception e). PS, for things like this, usually the best way to find out is to run a small sample program and see what the output is, if you get errors, etc. Nothing wrong with posting it here, just saying.

Like I said in the comments in my code above, this wasn't real code. I just typed it up as an example, so I wasn't really paying attention to syntax.

Thanks for your input though, I was running late for work yesterday when I originally posted, so I figured I'd have a response from somebody when I got home. Usually I do what your 'PS' recommends. I was just in a hurry yesterday.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.