一个try / catch语句中有多少个语句? [英] How many statements in a try/catch statement?

查看:252
本文介绍了一个try / catch语句中有多少个语句?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我应该把多个语句放在一个try中,然后捕获所有可能的异常,或者我应该在try语句中只放置一个语句?



示例:

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

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

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

return true;


解决方案

包装关键部分保持你的消息清楚,并指出。


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;

解决方案

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

这篇关于一个try / catch语句中有多少个语句?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆