bloopiebloopie
bloopiebloopie

Reputation: 319

JAVA: If an error is caught, is the code in the try block that apperes after the error executed when the error is handled?

1 try{
2    this line of code throws exceptions;
3    is this line of code ever executed?; 
4 catch(MadeUpException ex){1
5    something happens in response to the exception!;
6 }

Is the third line of code executed if the first line of code throws and exceptions and in handling the exception the program is not killed.

Upvotes: 0

Views: 372

Answers (3)

Ricard Nàcher Roig
Ricard Nàcher Roig

Reputation: 1320

In section 11.3. Run-Time Handling of an Exception of the JAVA8 specification says:

The control transfer that occurs when an exception is thrown causes abrupt completion of expressions (§15.6) and statements (§14.1) until a catch clause is encountered that can handle the exception; execution then continues by executing the block of that catch clause. The code that caused the exception is never resumed.

So, as others noticed before, line 3 is skipped. Notice also that if the thrown exception is not a MadeUpException then even line 5 will be skipped!.

Upvotes: 2

DarkV1
DarkV1

Reputation: 1776

The code will not run unless you fix the error in your catch block and retry. It stops immediately like a break; statement if an exception is thrown.

Upvotes: 1

2ARSJcdocuyVu7LfjUnB
2ARSJcdocuyVu7LfjUnB

Reputation: 435

No. The try block is interrupted as soon as an exception is thrown.

Upvotes: 1

Related Questions