Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it ok to instantiate an exception without throwing it?

Tags:

java

Suppose I have a MyException class that subclasses Exception. I'm using this class to include contextual information when errors occur in my code.

I typically use it to wrap one of the "standard" exception classes. For example, if an error happens during input validation, I will do something like

if (invalidInput())
  throw new MyException(new IllegalArgumentException(), arg1, arg2, ...);

But my IDE (Intellij IDEA) warns me that instantiating an unchecked exception (IllegalArgumentException in this example) without throwing it is bad, but doesn't tell me why.

So how sinful is it to instantiate an exception without throwing it? To which circle of hell will I go?

like image 334
lindelof Avatar asked Dec 02 '09 11:12

lindelof


People also ask

Can we throw an exception without throws?

Without using throwsWhen an exception is cached in a catch block, you can re-throw it using the throw keyword (which is used to throw the exception objects). If you re-throw the exception, just like in the case of throws clause this exception now, will be generated at in the method that calls the current one.

Can exceptions be instantiated?

The InstantiationException is a runtime exception in Java that occurs when an application attempts to create an instance of a class using the Class. newInstance() method, but the specified class object cannot be instantiated.

Why do we need to throw an exception?

In Java, exceptions allows us to write good quality codes where the errors are checked at the compile time instead of runtime and we can create custom exceptions making the code recovery and debugging easier.

Is it a good approach to throw an exception?

The short answer is NO. You would throw an exception if the application can't continue executing with the bad data. In your example, the logic is to display an error message on the front end and Option 2 is the cleaner method for achieving this requirement.


1 Answers

You might be better off throwing an instance of IllegalArgumentException, in this case that's what it's for:

if (invalidInput())
         new IllegalArgumentException("Invalid argument " + x + ", expected ...");

Or otherwise extending, IllegalArgumentException instead of Exception, it if you want to enhance it with custom properties.

public class MyIllegalArgumentException extends IllegalArgumentException {  
    public MyIllegalArgumentException(Object arg...) {    ....  } 
}

Both cases provide a leaner, more meaningful, class model.

Update: given your comment about wanting to supply contextual info with the thrown exception - you can do this by supplying your custom exception object as the Throwable argument to the standard exceptions contructor i.e. flip it round so: instead of wrapping the relevant standard exception in your exception, you should wrap your exception in the relevant standard exception.

if (invalidInput())
         new IllegalArgumentException("Invalid argument " + x + ", expected ...", new MyContextException(a,b,c));

(where a,b & c are the various bits of context you want to transmit). This way you (re)use a meaningful & appropriate, exception at all points in the code, but you transmit the contextual information that you may want to use further up the stack when handling/logging the exception.

like image 188
Joel Avatar answered Nov 09 '22 14:11

Joel