【发布时间】:2012-05-27 07:37:05
【问题描述】:
如果发生异常,我想执行一些代码。但是该代码也可以生成异常。但我从未见过有人在另一个 try/catch 中执行 try/catch。
我在做什么不好的做法,也许有更好的方法:
Uri uri = Uri.parse("some url");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
try
{
startActivity(intent);
}
catch (ActivityNotFoundException anfe)
{
// Make some alert to me
// Now try to redirect them to the web version:
Uri weburi = Uri.parse("some url");
try
{
Intent webintent = new Intent(Intent.ACTION_VIEW, weburi);
startActivity(webintent);
}
catch ( Exception e )
{
// Make some alert to me
}
}
这似乎有点尴尬。是不是有什么问题?
【问题讨论】:
-
你可以考虑把catch块里的代码放在自己的方法里。
-
@HunterMcMillen 好点。 :)
标签: java exception exception-handling try-catch