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;
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.
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.
maximum one catch block will be executed. No, we can write multiple catch block but only one is executed at a time.
1. How many except statements can a try-except block have? Answer: d Explanation: There has to be at least one except statement.
Wrap your critical parts to keep your message clear and to the point.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With