Java Exception Handling


Quiz

  • What is Exception in Java?
  • What are the Exception Handling Keywords in Java?
  • Explain Java Exception Hierarchy?
  • What are important methods of Java Exception Class?
  • Explain Java 7 ARM Feature and multi-catch block?
  • What is difference between Checked and Unchecked Exception in Java?
  • What is difference between throw and throws keyword in Java?
  • How to write custom exception in Java?
  • What is OutOfMemoryError in Java?
  • What are different scenarios causing “Exception in thread main”?
  • What is difference between final, finally and finalize in Java?
  • What happens when exception is thrown by main method?
  • Can we have an empty catch block?
  • Provide some Java Exception Handling Best Practices?
  • What is the problem with below programs and how do we fix it?

https://www.journaldev.com/2167/java-exception-interview-questions-and-answers

When the JVM throws an exception, it’s as if the JVM is shooting off a signal flare to notify an application that something has gone wrong, in order to give the application a chance to recover from the problem if we’ve equipped it to do so. Through a technique known as exception handling, we can design our applications so that they may anticipate and gracefully recover from such exceptions at run time.

There are many different exception types built into the Java language, each one defined by a different predefined Java class derived from a common ancestor class called Exception.

The try block

We enclose code that is likely to throw an exception inside of a pair of braces to form a code block, and precede the opening brace with the keyword try. This indicates our intention to catch—that is, to detect and respond to—any exceptions that might by thrown by the JVM while executing the code within that try block.

In order for the preceding code to compile, the try block must be immediately followed by at least one catch or finally block.

The catch block

catch (ExceptionType variableName) { 
    // Pseudocode. recovery code for an ExceptionType exception goes here        
}

One or more catch blocks can be associated with the same try block,

try { 
    code liable to throw exception(s) goes here ... 
} catch (ExceptionType1 variableName1) { 
    recovery code for ExceptionType1 goes here ... 
} catch (ExceptionType2 variableName2) { 
    recovery code for ExceptionType2 goes here ... 
}

There are many different exception types built into the Java language, each one defined by a different predefined Java class derived from a common ancestor class called Exception.

The finally block

A try block may optionally have a finally block associated with it. If provided, a finally block follows any catch blocks associated with that same try block. The code within a finally block is guaranteed to execute no matter what happens in the try/catch code that precedes it—that is, whether any of the following happen:

  • The try block executes to completion without throwing any exceptions whatsoever.
  • The try block throws an exception that is handled by one of the catch blocks.
  • The try block throws an exception that is not handled by any of the catch blocks.

Caching exceptions

The Exception Class Hierarchy

A catch clause for a given exception type X will catch that specific type of exception or any of its subtypes, by virtue of the “is a” nature of inheritance. For this reason, it’s important to list catch clauses in most specific to least specific order after a try block; that is, from lowest level subclass to highest superclass.

Catching the Generic Exception Type

Some programmers use the “lazy” approach of catching the most generic Exception type and then doing nothing to recover, just to silence the compiler. This is not a good practice! By doing so, we’re masking the fact that an exception has occurred. Our program may be in a serious state of dysfunction, perhaps coming to a screeching halt (!), but it will remain silent as to why because the preceding catch clause suppresses the typical stack trace that would otherwise typically be displayed.

try { 
    // Pseudocode. do anything!!! 
} catch (Exception e) { } // Empty braces => do NOTHING to recover!!!

Compiler Enforcement of Exception Handling

Generally speaking, the Java compiler will force us to enclose code that is liable to throw an exception in a try block with an appropriate catch block(s). Unreported exception java.io.FileNotFoundException; must be caught or declared to be thrown.

  1. Ideally, we’d enclose the code in question in a try block with an appropriate catch block (or blocks)

  2. Alternatively, we may add a throws clause to the header of the method in which the uncaught exception might arise.

public static void main(String[] args) throws FileNotFoundException {}

Taking Advantage of the Exception That We’ve “Caught”

  1. e.getMessage()
  2. e.printStackTrace()

Nesting of try/catch Blocks

A try statement may be nested inside either the try or catch block of another try statement. We frequently have a need to nest an inner try within an outer catch block, in particular, because the recovery code that we write within a catch block may pose a risk of throwing additional exceptions of its own.

User-Defined Exception Types

public class MissingValueException extends Exception { ... }

  • Override constructors.
  • Provide logic in Exception raising.

results matching ""

    No results matching ""