try/catch语句中有多少个语句?

Sjo*_*erd 7 oop exception-handling exception try-catch

我应该在try中放入多个语句然后捕获所有可能的异常,还是应该只在try语句中放入一个语句?

例:

try {
    MaybeThrowIOException();
    MaybeThrowFooBarException();
    return true;
} catch (IOException e) {
    // ...
} catch (FooBarException e) {
   // ... 
}
Run Code Online (Sandbox Code Playgroud)

要么

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

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

return true;
Run Code Online (Sandbox Code Playgroud)

Sam*_*son 7

包裹您的关键部分,以保持您的信息清晰明了.