Exception Handling in Java

Level : Intermediate
Mentor: Shailendra Chauhan
Duration : 00:04:00

What is an Exception in Java?

An event that interrupts the program's normal flow. It is an object that is thrown at runtime. When an exception occurs, the JVM generates an exception object to determine the type of exception that happened.

Types of Exceptions in Java

In Java, exception handling is classified into three types:

  • Checked or Compile-time Exceptions in Java
  • Unchecked or Runtime Exceptions in Java
  • Errors in Java

Checked or Compile-time Exceptions in Java

Checked or compiled-time exceptions arise when something goes wrong in your code but is potentially recoverable. Checked at compilation time. Some checked Exceptions include:

  • SQLException
  • IOException
  • ClassNotFoundException
  • InvocationTargetException
  • FileNotfound Exception

Unchecked or Runtime Exceptions in Java

Unchecked exceptions are classes that inherit from RuntimeException. They are not tested at compile time, but rather during runtime. Some unchecked exceptions include:

  • ArithmeticException
  • NullPointerException
  • ArrayIndexOutOfBoundsException
  • NumberFormatException
  • InputMismatchException

Errors in Java

Exceptional conditions that the application cannot normally predict or recover from. They are also ignored throughout the compilation process. Here are some examples of errors:

  • OutOfMemoryError
  • VirtualMachineErrors
  • AssertionError

Java Exception Keywords

Java supports 5 keywords for handling exceptions.

  1. try
  2. catch
  3. throw
  4. throws
  5. finally

What is an Exception Class in Java?

In Java, an Exception Class is a class that represents unusual conditions that may arise during program execution, breaking the normal flow. It usually extends a Java exception hierarchy subclass, such as the Exception class.


Exception Handling in Java

Exception handling in Java covers runtime failures such as ClassNotFoundException and IOException, ensuring that the application's execution flow remains consistent. It manages exceptions using try, catch, and finally blocks to prevent them from disrupting the program's normal function.


Finally Block in Java

The Finally block is used to include essential code, such as cleanup code. Executes whether an exception is thrown or not and whether it is handled. Finally includes every important statement, whether an exception happens or not.


Try Block in Java

The Try block encloses statements that may throw multiple exceptions. Use the throw keyword to trigger explicit exceptions. There could be several catch blocks, a finally block, or both. As a result, any combination of try-catch, try-finally, or try-catch-finally blocks is valid.

Exception Hierarchy in Java


Advantages of Exception Handling in Java

  • Smooth program execution with post-exception handling avoids sudden termination.
  • Enables easy error detection during execution.
  • In Java, exceptions are objects that can be described in detail using toString().
  • The hierarchical arrangement of exception classes facilitates categorization.
Self-paced Membership
  • 22+ Video Courses
  • 800+ Hands-On Labs
  • 400+ Quick Notes
  • 55+ Skill Tests
  • 45+ Interview Q&A Courses
  • 10+ Real-world Projects
  • Career Coaching Sessions
  • Email Support
Upto 60% OFF
Know More
Still have some questions? Let's discuss.
CONTACT US
Accept cookies & close this