user6134582
user6134582

Reputation:

Fatal error VS Exception

I know the difference between Error[runtime error] and Exception.{Talking in reference to JAVA}. But are the fatal error and exception the same thing or they are different.Or there is no concept of fatal error in java? I searched google but it was quite confusing.

Upvotes: 4

Views: 9493

Answers (2)

Honza Zidek
Honza Zidek

Reputation: 20026

Read the Java tutorial on Exceptions.

For short explanation you may use this small guide. See the class diagram with the Java Exception hierarchy (red) and a few examples (blue) in each category.

enter image description here

Throwable: the common ancestor of anything which can be thrown (and caught) using the Java exception mechanism.

Error: you may think about Errors as about what you call "fatal exception". It usually does not make sense to even try any recovery of the application. OutOfMemoryError or StackOverflowError are such fatal errors that you can hardly do anything within the application.

Do not catch Errors, let the program crash and solve the issue outside.

RuntimeException: should be used for mostly programmer's errors (see the following example), such as not-handled null pointer de-reference, not fulfilling method contracts etc.

Although they are techniques where all the exceptions are wrapped into RuntimeExceptions and handled outside of the business code (typically with AOP), normally an occurence of a RuntimeException means that you should let the program crash and fix its source code.

/**
 * Does something with s.
 * @param s The input string
 */
public void badlyDesignedMethod(String s) {
    // Here the programmer omitted to check s against null
    int i = s.length();
    ...
}

/**
 * Does something with s.
 * @param s The input string; must not be null
 */
public void betterDesignedMethod(String s) {
    if (s == null) {
        throw new IllegalArgumentException("The parameter s must not be null!");
    }
    int i = s.length();
    ...
}

public void caller() {
    badlyDesignedMethod(null); // will throw NullPointerException
    // It is the fault of the programmer of badlyDesignedMethod()
    // as they have not specified that the parameter must not be null.

    betterDesignedMethod(null); // will throw IllegalArgumentException
    // Now it is the fault of the programmer of caller()
    // as they violated the contract of betterDesignedMethod()
    // which DOES tell them that the parameter must not be null
}

Both the Errors and RuntimeExceptions are unchecked, which means that a method does not have to declare that it throws them and the callers do not have to catch them, i.e. do not have surround the call with try-catch block.

Other exceptions (inherited from Exception and not from RuntimeException): exceptions which should be declared (throws...) and caught (try-catch), which are mostly intended carrying useful information from a method to its caller: database is not available, file cannot be opened etc.

These are exception which are meant to be handled by your code. They are "expected exceptions", comparing to the run-time exceptions which are "unexpected exceptions".

public void doSomethingWithFile(String fileName) throws IOException {
    if (/* file is not accessible */) {
        throw new IOException("File " + fileName + " is not accessible!");
    }
    ...
}
public void caller() {
    try {
        doSomethingWithFile("/home/honza/file.txt");
    }
    catch (IOException e) {
        // handle the fact that the file is not accessible
        return;
    }
    ...
}

Upvotes: 10

tmokha
tmokha

Reputation: 85

The word FATAL is generally used in context of a logger framework, e.g. log4j, SLF4J, logback and many more. Now on looking in a log file one can find messages like

2015-09-28 15:21:48,222 Thread-4 FATAL Unable to register shutdown hook because JVM is shutting down. java.lang.IllegalStateException: Cannot add new shutdown hook as this is not started. Current state: STOPPED
at org.apache.logging.log4j.core.util.DefaultShutdownCallbackRegistry.addShutdownCallback(DefaultShutdownCallbackRegistry.java:113)
at org.apache.logging.log4j.core.impl.Log4jContextFactory.addShutdownCallback(Log4jContextFactory.java:271)
at org.apache.logging.log4j.core.LoggerContext.setUpShutdownHook(LoggerContext.java:240)     

This indicate an operation caused a error or exception to occur and it, the error or exception, was logged by the developer using the logger framework at FATAL level. The message could have been logged at any level.

Log4J for instance offers many log levels: OFF,FATAL, ERROR, WARN, INFO, DEBUG, TRACE, ALL

http://logging.apache.org/log4j/2.x/log4j-api/apidocs/org/apache/logging/log4j/Level.html

Just to cause more confusion there is a log level called ERROR.

To conclude, FATAL exception Vs FATAL error: The FATAL is the log level at which the exception or error object was logged at and has no bearing on the exception or error that occurred, but when a developer logs at FATAL level it is an to catch your eye and cause a reaction.

As per Log4J's doc on when to log at FATAL level "A severe error that will prevent the application from continuing."

Upvotes: 1

Related Questions