Change unhandled exception auto-generated catch code in Eclipse?
Asked Answered
M

3

29

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.

Metachromatism answered 7/11, 2012 at 18:5 Comment(4)
Just pick option (1) if that's what you want to do.Clearness
Note: read the question. The explicit goal is to choose option 2, but with a default catch block that does not hide the exception, in case the coder forgets to handle the exception. This is reasonable. Heck, I'm considering doing it myself.Wareroom
Old topic.. but you'd be better off updating the default catch block to something that won't compile, which will 'force' you to remember to handle the exception at compile time.Meave
You should have asked "I want to change the default generated catch block in eclipse". Consider rephrasing if nobody seems to understand your question. And thanks for the idea :)Expiry
W
37

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
Wareroom answered 7/11, 2012 at 18:10 Comment(0)
F
1

Personally, I use a generic idiom irrespective of the actual checked exception type, you might make Eclipse use that as a template instead:

try {
 ...
} 
catch (RuntimeException e) { throw e; } 
catch (Exception e) { throw new RuntimeException(e); }

The point is to wrap the whole code block instead of individually each line that may throw an exception. The block may throw any number of checked and unchecked exceptions, and this will allow the unchecked exceptions to pass through unharmed, and the checked exceptions will be wrapped.

Flacon answered 7/11, 2012 at 18:10 Comment(0)
F
0

If you are re-throwing your exception from the catch clause, then you would have to handle in the method that invoked your current method. But if you wrap your exception in RuntimeException, you won't need to handle it. But why would you do that?

I mean why not just: -

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

Because, in your code, basically you are wrapping a might be checked exception in an unchecked one. If I assume your MyUnhandledException as checked exception.

And also note that, if you are following this approach, you would still need to declare it to be thrown in your throws clause.

If you just want to do the way you are doing, then also it will work fine. You can change the Eclipse setting as per @Andy's answer.

But, it would be better to look at your design. Why is the method overrided throwing an exception not declared in your overriden method. Probably there is something wrong, that should be corrected.

Fund answered 7/11, 2012 at 18:8 Comment(6)
If I would able to throw MyUnhandledException it wasn't an error then. It is obvious from the question that MyUnhandledException is not runtime and not described in method declaration -- this is the only case exception causes compiler error. So I need to rethrow exception in runtime wrapper not to change declaration. This can be required if method is overrided.Metachromatism
@SuzanCioc. Well, in that case, it will work. There is no problem in that. But still it would be better to take a look at your method you are overriding. If the overrided method throw exception not declared in the overriden method, then there is certainly something wrong.Fund
There are no checked exceptions in C# at all and nothing goes wrong there. So, ANY exception can be just wrapped into RuntimeException and it won't cause any additional troubles except exception itself.Metachromatism
@SuzanCioc. Where does C# comes from? Your question is tagged Java.Fund
@RohitJain Susan is giving a counterexample of a language blessed with the absence of checked exceptions. I envy C# users.Flacon
@MarkoTopolnik.. Oh. Me too :( I have no idea about C#.Fund

© 2022 - 2024 — McMap. All rights reserved.