在Eclipse中更改未处理的异常自动生成的catch代码? [英] Change unhandled exception auto-generated catch code in Eclipse?

查看:356
本文介绍了在Eclipse中更改未处理的异常自动生成的catch代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我在Java中有未处理的异常,Eclipse会向我提出两个选择:(1)添加throws声明和(2)环绕与try / catch。



如果我选择(2)它添加一个代码

  try {
myfunction();
} catch(MyUnhandledException e){
// TODO自动生成的catch块
e.printStackTrace();
}

我想将其更改为

  try {
myfunction();
} catch(MyUnhandledException e){
throw new RuntimeException(e);
}

这是可能吗?



更新



为什么这么爱改变主题人物?



如果异常被捕获和打印,也不需要再抓住它。如果我忘记了错误处理异常,我喜欢我的应用程序崩溃。所以,我喜欢默认重新抛出。

解决方案

是的,你可以更改Eclipse添加的默认代码。 >


  1. 在首选项中,导航到 Java>代码样式>代码模板

  2. 代码下,选择抓住块正文

  3. 按编辑按钮更改代码。完成后,按
    确定按钮。

考虑在默认情况下添加TODO注释抓块。例如,默认值包括:

  // $ {todo}自动生成的catch块


If I have unhandled exception in Java, Eclipse proposes two options to me: (1) add throws declaration and (2) surround with try/catch.

If I choose (2) it adds a code

try {
   myfunction();
} catch (MyUnhandledException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

I want to change this to

try {
   myfunction();
} catch (MyUnhandledException e) {
    throw new RuntimeException(e);
}

Is this possible?

UPDATE

Why are so love to change the topic people???

If exception is catched and printed it is also no need to catch it anymore. I like my application to crash if I forget to handle an exception by mistake. So, I like to rethrow it by default.

解决方案

Yes, you can change the default code added by Eclipse.

  1. In Preferences, navigate to Java>Code Style>Code Templates.
  2. Under Code, select Catch block body.
  3. Press the Edit button to change the code. When finished, press the OK button.

Consider adding a TODO comment in the default catch block. For example, the default includes:

     // ${todo} Auto-generated catch block

这篇关于在Eclipse中更改未处理的异常自动生成的catch代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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