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 7 Years Ago by leverin4: n/a

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.

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 7 Years Ago by BestJewSinceJC: n/a

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.