Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it ever OK to throw a java.lang.Error?

I have a plugin module that goes into a web application. If the module does not load correctly, it does not make sense for the web application to go on, and the web application should probably not load at all, we would prefer this module to initialize correctly always. If I were to throw a runtime exception, it would get into the logs, and just get ignored since the application will continue anyway, and the end users would never know... I know that errors are meant to be thrown only under exceptional conditions, and they generally have to do with situations that the system cannot recover from, but what would you do in such a situation?

like image 847
Abhijeet Kashnia Avatar asked Apr 22 '10 13:04

Abhijeet Kashnia


People also ask

Can we throw an error in Java?

Any code can throw an exception: your code, code from a package written by someone else such as the packages that come with the Java platform, or the Java runtime environment. Regardless of what throws the exception, it's always thrown with the throw statement.

How do you handle Java Lang error?

Catching Errors lang. Error class in Java doesn't inherit from java. lang. Exception, we must declare the Error base class – or the specific Error subclass we'd like to capture – in the catch statement in order to catch it.

Is it OK to catch exception in Java?

The general principal is to catch the most specific type you can. catch(Exception) is a bad practice because it catches all RuntimeException (unchecked exception) too.

Does throwing an error stop execution Java?

When an exception is thrown the method stops execution right after the "throw" statement. Any statements following the "throw" statement are not executed.


2 Answers

Not sure how exactly but OSGi got a dependency management of bundles (=similar to plugins). One bundle don't load until another bundle is ready. Maybe you can use this same mechanism (or simply use OSGi itself ;)) to wait for one plugin/application until another plugin is ready. Or you shutdown the application during startup if it cannot find/load your plugin correctly.

like image 37
Progman Avatar answered Sep 23 '22 07:09

Progman


The only Error which I've regularly used in business code is ExceptionInInitializerError. You have no other choice in static initializer blocks.

But even if you throw that inside a webapplication, the webapplication would still continue listening on HTTP requests. Your best bet is to do the module loading or initialization inside a Filter listening on an url-pattern of /* and let the Filter block the HTTP requests accordingly. E.g.

private boolean allModulesAreLoaded;

@Override
public void init(FilterConfig config) {
    try {
        // Load modules.
        allModulesAreLoaded = true;
    } catch (Exception e) {
        // Handle.
    }
}

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException {
    if (allModulesAreLoaded) {
        chain.doFilter(request, response);
    } else {
        throw new ServletException("Not all modules are loaded.");
    }
}

This would yield a HTTP 500 error with the given message.

like image 155
BalusC Avatar answered Sep 23 '22 07:09

BalusC