Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.


1 Answers

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;
    }
    ...
}
like image 112
Honza Zidek Avatar answered Oct 23 '25 01:10

Honza Zidek



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!