I am programming a Java Servlet application which needs certain environment variables and JNDI definitions to be present. These are defined in server.xml
file. Using default values is not possible. So the program needs to throw a runtime (unchecked) exception. But which one? If none of the subclasses of java.lang.RuntimeException
is suitable, I guess we will need to create a new type.

- 723
- 1
- 9
- 31

- 331
- 2
- 14
-
`MissingPropetiesException`, `AssertionError`, `WhatEverYouLikeException`? you want a *name* basically? – Eugene Jul 30 '18 at 09:48
-
Yes, a name, or rather - if possible - an existing RuntimeException childclass. – Mikko Koivunalho Jul 30 '18 at 11:03
-
1there are `430` as far as I see... but this is a bit crazy, IMO. Choose whatever name you feel fits best, and *I would* create a new one like `MissingStartupPropertiesException` – Eugene Jul 30 '18 at 11:06
2 Answers
You could use exceptions already defined, but I just usually implement my own, because I can always quickly recognize it when it is thrown. (It tells you more, just by having your project's classpath.)
public class MissingInitialContextException extends RuntimeException {
public MissingInitialContextException() {
}
public MissingInitialContextException(String message) {
super(message);
}
public MissingInitialContextException(String message, Throwable cause) {
super(message, cause);
}
public MissingInitialContextException(Throwable cause) {
super(cause);
}
public MissingInitialContextException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
}
You can auto generate classes like this in most IDE. (IDEA, after you created the class, ALT+ENTER and override methods from the RuntimeException class)
Why use custom exceptions.:
- You can use your IDE's find implementations and other search functions to locate usages
- In some frameworks, like in Spring Boot, you can decorate your exceptions with annotations and quickly define the response text and http error code.
- You can change the exception's implementation later.
- You can define automatic break on exception in your IDE for your custom exception, while if you would do this for a built in one, your debugger would stop in unexpected lines.
- You can avoid unintentionally catching other library's exceptions.

- 2,356
- 5
- 40
- 74
java.lang.NullPointerException
When the configuration of the application is not (yet) complete the variables will probably be null, a NullPointerException seems appropriate. This exception is pretty generic but can be customized with a specific detail message in the constructor.
From the API:
Applications should throw instances of this class to indicate other illegal uses of the null object.
- When accessing or modifying the field of a null object.
throw new NullPointerException("environment variable XX is missing");
-
1I would not use NullPointerException. I think they are always the programmer's fault. But in this case the guilty party is actually the Application Server config manager... – Mikko Koivunalho Jul 30 '18 at 11:07