Category Archives: Exceptions

  1. We will discuss frequently asked interview questions on exception class hierarchy in java.
  2. Simulate checked & unchecked exceptions in java (with examples).
  3. We will create custom or user defined checked & unchecked exceptions.
  4. We will also simulate error conditions like OutOfMemoryError.
  5. We will discuss AutoCloseable interface introduced in Java 7.
  6. We will shed light on some important facts about exception class hierarchy.

try with resources & AutoCloseable java 7 (example & class hierarchy)

The try-with-resources is try statement that used to declare one or more resources. e.g. resources like BufferedReader, Scanner, BufferedWriter can be used with try-with-resource statement. Any resource class implementing AutoCloseable interface, can be used with try-with-resource statement. Resources are closed automatically at the end of try statement. We will look into the resources implementing AutoCloseable interface… Read More »

Catch multiple exceptions before & after java 7 (examples)

What is exception (background)? Exception occurs in an application mostly due to programming mistakes. e.g. NullPointerException occurs, when we try to access uninitialized objects e.g. ArrayIndexOutOfBoundsException occurs, when we try to access the array index beyond its bounds. We catch the exceptions using catch blocks and recover from them. e.g. If ArrayIndexOutOfBoundsException occurs, in catch block we… Read More »

User defined custom Runtime/Unchecked exception example

What is user defined or custom exception in java? User defined exceptions is concrete class extending exceptions like Exception, RuntimeException etc. Create an unchecked exception by extending RuntimeException class in java In our application, sometimes we would like to pass custom information to exception. User defined exceptions are used to handle application specific inputs. e.g. If we unable to find… Read More »

Create custom or user defined unchecked exception in java (example)

Create a user defined exception in java. Create a custom exception, by extending unchecked exceptions. We will create CustomArithmeticException by extending ArithmeticException class We will create CustomNullPointerException by extending NullPointerException class. Exception class hierarchy in java Program – Custom user defined arithmetic & nullpointer exception 1.) CustomArithmeticException Class:  Create CustomArithmeticException class, by extending ArithmeticException exception class. package org.learn.exception; public… Read More »

Program – simulate or generate unchecked exceptions (java & examples)

1. What is unchecked exception in java? Unchecked exceptions are not checked at compiled time. The unchecked exceptions are not forced by compiler to implement. i.e. Its not mandatory to enclose the code with try-catch block. Its purely at programmer’s discretion to catch or throws the unchecked exceptions. We have already discussed about the checked and unchecked… Read More »

Difference b/w checked & unchecked exception (class hierarchy & example)

Difference between checked and unchecked exceptions in java. Discuss the exception class hierarchy showing checked & unchecked exception in java. Discuss the examples of checked & unchecked exception. What is exception ? Exception is an event that occurs during the execution of program, the program breaks it normal flow of execution. Types of exceptions in… Read More »

What is difference between error & exception in java (class hierarchy/ example)

We will discuss the difference between error & exception in java. We will demonstrate exception class hierarchy in java. We have shown checked & unchecked exception class hierarchy in Fig 1.. We will discuss the examples of error & exceptions in java. 1. Exception class hierarchy in java We have shown some of the concrete classes of exceptions… Read More »