Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
229 views
in Technique[技术] by (71.8m points)

java - Change unhandled exception auto-generated catch code in Eclipse?

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...