简体   繁体   中英

What kind of Exception do you throw?

I have a method which searches for a variable in the Environment first, and if it isn't found there it searches config.properties . If neither yield a result, what Exception do you throw?

NotFoundException sounds perfect, except it's used for when a resource isn't found on a server.

So what, then, RuntimeException(String msg) and clarify in the message?

However, upon reading Unchecked Exceptions — The Controversy I read that if a client can recover from the exception (they could elect to set a default value or something else), I should use a Checked Exception. Going through a list of exceptions I can't find a suitable one, so then I assume I should throw Exception ?

Did I answer my own question?

Only in the rarest cases would I throw a predefined exception from my application code. Usually I define my own exception, much like @Edwin Dalorzo suggests. This gives you more fine-grained control in the higher layers of your application to decide what to do about it. The least I do is have two different types, one for things that are fatal (ie failures I can not handle inside the application) and recoverable failures (ie failures the application can deal with). Example: If your application can not run without that configuration variable, it is a fatal case that you let bubble up to the outer layer of your application (ie the Controller in web application or web service). If some higher layer could work without that variable, it is not fatal. The main difference is: Should the application itself deal with it or only the outer-most layer?

Consider creating an exception that extends RuntimeException. For example, public class BlamException extends RuntimeException... (name it something meaningful like "BadConfigurationException").

Much of the RuntimeException "controversy" is just goofballs claiming that the program can recover from an unexpected, invalid state which is usually (perhaps just often) not the case. In the case of a missing configuration value, there is likely no good default value.

If you are going to allow default values, then implement a default value for the missing configuration value and log a message stating something like "Warning: Configuration value blam is missing. Using default value: kapow".

100% you should not use a Checked Exception. A Checked Exception requires the program to catch and "handle" the exception. In your case, the "handle" part is "prevent the application from starting", which will happen if you throw a RuntimeException.

How about NoSuchElementException :

Thrown by various accessor methods to indicate that the element being requested does not exist.

I have a method that searches for a variable in the Environment first, and if it isn't found there it searches config.properties. If neither yield a result

Doesn't this sound just perfect for defining your own Exception ? Let us take a situation here:

You are developing an application for bank and you want to verify if the user says A who wants to send money to another user say B
So before transferring money, we would check whether A has that much amount, and if he/she has then we will carry forward the transaction.
What is A doesn't have that much balance? As a responsible developer, you will notify A . Here there are many ways to deal with it. But what I would prefer here is Defining my own exception. InsufficentBalanceException(int amount) .

You could use a unchecked exception by writing a wrapper class which would extend RuntimeException class with a custom message in case you're dealing with resources.

Overall you need to propagate the Exception to application level, because configuration does matter while starting you application.

Example

ErrorClass.java

public type doSomething(){
...
...
try{
   ...
} catch(FileNotFoundException e){
throw new MyCustomFileNotFoundException("config file not found", e); // no destructive wrapping here
}
}

MyCustomFileNotFoundException.java

public class MyCustomFileNotFoundException extends RuntimeException{
    public MyCustomFileNotFoundException(String msg, Throwable cause){
    super(msg, cause);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM