Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How many statements in a try/catch statement?

Should I put multiple statements in a try and then catch all possible exceptions, or should I put only one statement in the try statement?

Example:

try {
    MaybeThrowIOException();
    MaybeThrowFooBarException();
    return true;
} catch (IOException e) {
    // ...
} catch (FooBarException e) {
   // ... 
}

Or

try {
    MaybeThrowIOException();
} catch (IOException e) {
    // ...
}

try {
    MaybeThrowFooBarException();
} catch (FooBarException e) {
   // ... 
}

return true;
like image 881
Sjoerd Avatar asked Jan 18 '10 14:01

Sjoerd


People also ask

How many Catch statements can a single try statement have?

Yes, we can define one try block with multiple catch blocks in Java. Every try should and must be associated with at least one catch block.

Can you have 2 Catch statements?

Yes you can have multiple catch blocks with try statement. You start with catching specific exceptions and then in the last block you may catch base Exception . Only one of the catch block will handle your exception.

How many catch statement you can have?

maximum one catch block will be executed. No, we can write multiple catch block but only one is executed at a time.

How many statements can a try block have?

1. How many except statements can a try-except block have? Answer: d Explanation: There has to be at least one except statement.


1 Answers

Wrap your critical parts to keep your message clear and to the point.

like image 138
Sampson Avatar answered Oct 29 '22 01:10

Sampson