Java: What are the reasons for throwing an Exception rather than a specific exception?

Are there any reasons you would like to do this:

void foo() throws Exception { // Do something potentially exceptional } 

Instead of throwing an existing or custom exception?

+4
source share
10 answers

There are two cases that I can think of - the first such case I can think of is to implement finalize() , you need to throw Throwable :

 @Override protected void finalize() throws Throwable { super.finalize(); } 

... although keep in mind that some argue that using finalize should be discouraged on its own.

A potential second case is to use a (poorly written) library whose method can throw Exception , in which case if you do not want to deal with this particular method, the only option is to drop it onto the stack.

Personally, although if it were me, I would most likely wrap it in a RuntimeException, and then:

 public void doSomething() { try { int x = libraryThing.badMethod(); //Library method that throws "Exception" } catch(Exception ex) { throw new RuntimeException("Couldn't do something", ex); } } 

The second argument in the RuntimeException constructor RuntimeException important in this case, because if thrown, it will save the original exception in the stack trace as ("Caused by: x"). Of course, if you can find a more specific subclass of RuntimeException that you can guarantee is relevant in this context (e.g. IllegalArgumentException ), then using this would be better.

In terms of normal code, however, no - I would argue that it is almost always an anti-pattern (and usually it is simply caused by laziness!)

As a side point, throwing a RuntimeException not so bad - it is still very non-specific, but at least it does not make the caller explicitly catch everything.

+5
source

I would not do that. It provides a minimum of information about what happened.

I think that at present, it is best to use preferred exceptions (this is the C # path). The foo () method will catch checked exceptions and wrap them in a RuntimeException.

I would either state the exceptions, wrap them in a special business-specific exception, or wrap a RuntimeException.

+3
source

It allows the method to throw arbitrary exceptions.

This can be found in the context of the context where arbitrary code works in methods with well-known signatures. Is this "good" in this context ... meh. I would rather see exceptions for the environment or the runtime.

Other than that, it's usually an anti-pattern, IMO.

+3
source

I often do this in my testing methods.

 @Test public void testSOmething() throws Exception { 

This is my standard signature for unit tests that are not specifically tested to see if an exception is thrown (which is most tests).

Outside of these tests, I don't care what exception my tests may be, because throwing an exception in these cases is a failure of the method being tested.

I never do this in production code.

+3
source

You can declare throws Exception if the actual list is very long and not interesting. for example, when calling methods through reflections, this can lead to a fair number of Exceptions.

+2
source

Perhaps you are using the Java Callable interface! This is about summing up. If you provide some add-in where another user’s code can be executed, but you don’t want them to be forced to catch all checked exceptions inside. It could be argued that this is a good design for your own library, but the danger of poor design of checked exceptions in the first place (but then we will have a Holy War, not a SO issue).

+2
source

in general, this means either poor code design or poor base library design. If you find that you are throwing a “throw exception” for no good reason, consider throwing a RuntimeException instead. Especially in the library code.

+1
source

As a rule, I consider that a throws exception is acceptable for methods declared in the interface that must be implemented by someone else and can have very different implementations (you will not want to compress possible implementations in what they can throw).

I find this also acceptable when there are many exceptions that can be selected from a method.

I would not consider this an anti-pattern, but the idiom was often used out of laziness.

0
source

Based on business rules, state your own exception

 public void doSomething() { try { int x = 10/0; //Library method that throws "Exception" } catch(Exception ex) { throw new Exception("this doesn;t work.there is exception", ex); } } 

This overrides the Exception method;

 class Exception { Exception() { } Exception(String msg) { this.msg=msg; } } 
0
source

This is often done in test / fast and dirty code, and it is very reasonable.

Sometimes this happens when the throws list is long and tedious - semi-sensible

And some developers / projects do this for everything, because they have a different philosophy about "checked" exceptions, which is fine (I also deal with this mind) if they do not intend to have a significant code exchange with the "rest of the world".

0
source

Source: https://habr.com/ru/post/1432975/


All Articles