Java run code only if no exception is thrown in try and catch block?

Here are two ways:

try {
    somethingThatMayThrowAnException();
    somethingElseAfterwards();
} catch (...) {
    ...
}

Or if you want your second block of code to be outside the try block:

boolean success = false;
try {
    somethingThatMayThrowAnException();
    success = true;
} catch (...) {
    ...
}
if (success) {
    somethingElseAfterwards();
}

You could also put the if statement in a finally block, but there is not enough information in your question to tell if that would be preferable or not.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)