Explore Courses
Liverpool Business SchoolLiverpool Business SchoolMBA by Liverpool Business School
  • 18 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityMBA (Master of Business Administration)
  • 15 Months
Popular
O.P.Jindal Global UniversityO.P.Jindal Global UniversityMaster of Business Administration (MBA)
  • 12 Months
New
Birla Institute of Management Technology Birla Institute of Management Technology Post Graduate Diploma in Management (BIMTECH)
  • 24 Months
Liverpool John Moores UniversityLiverpool John Moores UniversityMS in Data Science
  • 18 Months
Popular
IIIT BangaloreIIIT BangalorePost Graduate Programme in Data Science & AI (Executive)
  • 12 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityDBA in Emerging Technologies with concentration in Generative AI
  • 3 Years
upGradupGradData Science Bootcamp with AI
  • 6 Months
New
University of MarylandIIIT BangalorePost Graduate Certificate in Data Science & AI (Executive)
  • 8-8.5 Months
upGradupGradData Science Bootcamp with AI
  • 6 months
Popular
upGrad KnowledgeHutupGrad KnowledgeHutData Engineer Bootcamp
  • Self-Paced
upGradupGradCertificate Course in Business Analytics & Consulting in association with PwC India
  • 06 Months
OP Jindal Global UniversityOP Jindal Global UniversityMaster of Design in User Experience Design
  • 12 Months
Popular
WoolfWoolfMaster of Science in Computer Science
  • 18 Months
New
Jindal Global UniversityJindal Global UniversityMaster of Design in User Experience
  • 12 Months
New
Rushford, GenevaRushford Business SchoolDBA Doctorate in Technology (Computer Science)
  • 36 Months
IIIT BangaloreIIIT BangaloreCloud Computing and DevOps Program (Executive)
  • 8 Months
New
upGrad KnowledgeHutupGrad KnowledgeHutAWS Solutions Architect Certification
  • 32 Hours
upGradupGradFull Stack Software Development Bootcamp
  • 6 Months
Popular
upGradupGradUI/UX Bootcamp
  • 3 Months
upGradupGradCloud Computing Bootcamp
  • 7.5 Months
Golden Gate University Golden Gate University Doctor of Business Administration in Digital Leadership
  • 36 Months
New
Jindal Global UniversityJindal Global UniversityMaster of Design in User Experience
  • 12 Months
New
Golden Gate University Golden Gate University Doctor of Business Administration (DBA)
  • 36 Months
Bestseller
Ecole Supérieure de Gestion et Commerce International ParisEcole Supérieure de Gestion et Commerce International ParisDoctorate of Business Administration (DBA)
  • 36 Months
Rushford, GenevaRushford Business SchoolDoctorate of Business Administration (DBA)
  • 36 Months
KnowledgeHut upGradKnowledgeHut upGradSAFe® 6.0 Certified ScrumMaster (SSM) Training
  • Self-Paced
KnowledgeHut upGradKnowledgeHut upGradPMP® certification
  • Self-Paced
IIM KozhikodeIIM KozhikodeProfessional Certification in HR Management and Analytics
  • 6 Months
Bestseller
Duke CEDuke CEPost Graduate Certificate in Product Management
  • 4-8 Months
Bestseller
upGrad KnowledgeHutupGrad KnowledgeHutLeading SAFe® 6.0 Certification
  • 16 Hours
Popular
upGrad KnowledgeHutupGrad KnowledgeHutCertified ScrumMaster®(CSM) Training
  • 16 Hours
Bestseller
PwCupGrad CampusCertification Program in Financial Modelling & Analysis in association with PwC India
  • 4 Months
upGrad KnowledgeHutupGrad KnowledgeHutSAFe® 6.0 POPM Certification
  • 16 Hours
O.P.Jindal Global UniversityO.P.Jindal Global UniversityMaster of Science in Artificial Intelligence and Data Science
  • 12 Months
Bestseller
Liverpool John Moores University Liverpool John Moores University MS in Machine Learning & AI
  • 18 Months
Popular
Golden Gate UniversityGolden Gate UniversityDBA in Emerging Technologies with concentration in Generative AI
  • 3 Years
IIIT BangaloreIIIT BangaloreExecutive Post Graduate Programme in Machine Learning & AI
  • 13 Months
Bestseller
IIITBIIITBExecutive Program in Generative AI for Leaders
  • 4 Months
upGradupGradAdvanced Certificate Program in GenerativeAI
  • 4 Months
New
IIIT BangaloreIIIT BangalorePost Graduate Certificate in Machine Learning & Deep Learning (Executive)
  • 8 Months
Bestseller
Jindal Global UniversityJindal Global UniversityMaster of Design in User Experience
  • 12 Months
New
Liverpool Business SchoolLiverpool Business SchoolMBA with Marketing Concentration
  • 18 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityMBA with Marketing Concentration
  • 15 Months
Popular
MICAMICAAdvanced Certificate in Digital Marketing and Communication
  • 6 Months
Bestseller
MICAMICAAdvanced Certificate in Brand Communication Management
  • 5 Months
Popular
upGradupGradDigital Marketing Accelerator Program
  • 05 Months
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Corporate & Financial Law
  • 12 Months
Bestseller
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in AI and Emerging Technologies (Blended Learning Program)
  • 12 Months
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Intellectual Property & Technology Law
  • 12 Months
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Dispute Resolution
  • 12 Months
upGradupGradContract Law Certificate Program
  • Self paced
New
ESGCI, ParisESGCI, ParisDoctorate of Business Administration (DBA) from ESGCI, Paris
  • 36 Months
Golden Gate University Golden Gate University Doctor of Business Administration From Golden Gate University, San Francisco
  • 36 Months
Rushford Business SchoolRushford Business SchoolDoctor of Business Administration from Rushford Business School, Switzerland)
  • 36 Months
Edgewood CollegeEdgewood CollegeDoctorate of Business Administration from Edgewood College
  • 24 Months
Golden Gate UniversityGolden Gate UniversityDBA in Emerging Technologies with Concentration in Generative AI
  • 36 Months
Golden Gate University Golden Gate University DBA in Digital Leadership from Golden Gate University, San Francisco
  • 36 Months
Liverpool Business SchoolLiverpool Business SchoolMBA by Liverpool Business School
  • 18 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityMBA (Master of Business Administration)
  • 15 Months
Popular
O.P.Jindal Global UniversityO.P.Jindal Global UniversityMaster of Business Administration (MBA)
  • 12 Months
New
Deakin Business School and Institute of Management Technology, GhaziabadDeakin Business School and IMT, GhaziabadMBA (Master of Business Administration)
  • 12 Months
Liverpool John Moores UniversityLiverpool John Moores UniversityMS in Data Science
  • 18 Months
Bestseller
O.P.Jindal Global UniversityO.P.Jindal Global UniversityMaster of Science in Artificial Intelligence and Data Science
  • 12 Months
Bestseller
IIIT BangaloreIIIT BangalorePost Graduate Programme in Data Science (Executive)
  • 12 Months
Bestseller
O.P.Jindal Global UniversityO.P.Jindal Global UniversityO.P.Jindal Global University
  • 12 Months
WoolfWoolfMaster of Science in Computer Science
  • 18 Months
New
Liverpool John Moores University Liverpool John Moores University MS in Machine Learning & AI
  • 18 Months
Popular
Golden Gate UniversityGolden Gate UniversityDBA in Emerging Technologies with concentration in Generative AI
  • 3 Years
Rushford, GenevaRushford Business SchoolDoctorate of Business Administration (AI/ML)
  • 36 Months
Ecole Supérieure de Gestion et Commerce International ParisEcole Supérieure de Gestion et Commerce International ParisDBA Specialisation in AI & ML
  • 36 Months
Golden Gate University Golden Gate University Doctor of Business Administration (DBA)
  • 36 Months
Bestseller
Ecole Supérieure de Gestion et Commerce International ParisEcole Supérieure de Gestion et Commerce International ParisDoctorate of Business Administration (DBA)
  • 36 Months
Rushford, GenevaRushford Business SchoolDoctorate of Business Administration (DBA)
  • 36 Months
Liverpool Business SchoolLiverpool Business SchoolMBA with Marketing Concentration
  • 18 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityMBA with Marketing Concentration
  • 15 Months
Popular
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Corporate & Financial Law
  • 12 Months
Bestseller
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Intellectual Property & Technology Law
  • 12 Months
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Dispute Resolution
  • 12 Months
IIITBIIITBExecutive Program in Generative AI for Leaders
  • 4 Months
New
IIIT BangaloreIIIT BangaloreExecutive Post Graduate Programme in Machine Learning & AI
  • 13 Months
Bestseller
upGradupGradData Science Bootcamp with AI
  • 6 Months
New
upGradupGradAdvanced Certificate Program in GenerativeAI
  • 4 Months
New
KnowledgeHut upGradKnowledgeHut upGradSAFe® 6.0 Certified ScrumMaster (SSM) Training
  • Self-Paced
upGrad KnowledgeHutupGrad KnowledgeHutCertified ScrumMaster®(CSM) Training
  • 16 Hours
upGrad KnowledgeHutupGrad KnowledgeHutLeading SAFe® 6.0 Certification
  • 16 Hours
KnowledgeHut upGradKnowledgeHut upGradPMP® certification
  • Self-Paced
upGrad KnowledgeHutupGrad KnowledgeHutAWS Solutions Architect Certification
  • 32 Hours
upGrad KnowledgeHutupGrad KnowledgeHutAzure Administrator Certification (AZ-104)
  • 24 Hours
KnowledgeHut upGradKnowledgeHut upGradAWS Cloud Practioner Essentials Certification
  • 1 Week
KnowledgeHut upGradKnowledgeHut upGradAzure Data Engineering Training (DP-203)
  • 1 Week
MICAMICAAdvanced Certificate in Digital Marketing and Communication
  • 6 Months
Bestseller
MICAMICAAdvanced Certificate in Brand Communication Management
  • 5 Months
Popular
IIM KozhikodeIIM KozhikodeProfessional Certification in HR Management and Analytics
  • 6 Months
Bestseller
Duke CEDuke CEPost Graduate Certificate in Product Management
  • 4-8 Months
Bestseller
Loyola Institute of Business Administration (LIBA)Loyola Institute of Business Administration (LIBA)Executive PG Programme in Human Resource Management
  • 11 Months
Popular
Goa Institute of ManagementGoa Institute of ManagementExecutive PG Program in Healthcare Management
  • 11 Months
IMT GhaziabadIMT GhaziabadAdvanced General Management Program
  • 11 Months
Golden Gate UniversityGolden Gate UniversityProfessional Certificate in Global Business Management
  • 6-8 Months
upGradupGradContract Law Certificate Program
  • Self paced
New
IU, GermanyIU, GermanyMaster of Business Administration (90 ECTS)
  • 18 Months
Bestseller
IU, GermanyIU, GermanyMaster in International Management (120 ECTS)
  • 24 Months
Popular
IU, GermanyIU, GermanyB.Sc. Computer Science (180 ECTS)
  • 36 Months
Clark UniversityClark UniversityMaster of Business Administration
  • 23 Months
New
Golden Gate UniversityGolden Gate UniversityMaster of Business Administration
  • 20 Months
Clark University, USClark University, USMS in Project Management
  • 20 Months
New
Edgewood CollegeEdgewood CollegeMaster of Business Administration
  • 23 Months
The American Business SchoolThe American Business SchoolMBA with specialization
  • 23 Months
New
Aivancity ParisAivancity ParisMSc Artificial Intelligence Engineering
  • 24 Months
Aivancity ParisAivancity ParisMSc Data Engineering
  • 24 Months
The American Business SchoolThe American Business SchoolMBA with specialization
  • 23 Months
New
Aivancity ParisAivancity ParisMSc Artificial Intelligence Engineering
  • 24 Months
Aivancity ParisAivancity ParisMSc Data Engineering
  • 24 Months
upGradupGradData Science Bootcamp with AI
  • 6 Months
Popular
upGrad KnowledgeHutupGrad KnowledgeHutData Engineer Bootcamp
  • Self-Paced
upGradupGradFull Stack Software Development Bootcamp
  • 6 Months
Bestseller
upGradupGradUI/UX Bootcamp
  • 3 Months
upGradupGradCloud Computing Bootcamp
  • 7.5 Months
PwCupGrad CampusCertification Program in Financial Modelling & Analysis in association with PwC India
  • 5 Months
upGrad KnowledgeHutupGrad KnowledgeHutSAFe® 6.0 POPM Certification
  • 16 Hours
upGradupGradDigital Marketing Accelerator Program
  • 05 Months
upGradupGradAdvanced Certificate Program in GenerativeAI
  • 4 Months
New
upGradupGradData Science Bootcamp with AI
  • 6 Months
Popular
upGradupGradFull Stack Software Development Bootcamp
  • 6 Months
Bestseller
upGradupGradUI/UX Bootcamp
  • 3 Months
PwCupGrad CampusCertification Program in Financial Modelling & Analysis in association with PwC India
  • 4 Months
upGradupGradCertificate Course in Business Analytics & Consulting in association with PwC India
  • 06 Months
upGradupGradDigital Marketing Accelerator Program
  • 05 Months

Comprehensive Guide to Exception Handling in Java: Best Practices and Examples

Updated on 07 January, 2025

7.29K+ views
15 min read

Did you know that Java powers over 9 million developers globally? Its reliability and scalability make it a favorite for everything from Android apps to complex financial systems. But what happens when things go wrong, like a file not found, invalid user input, or a network failure? That’s where exception handling in Java becomes crucial.

Exception handling allows you to anticipate, catch, and manage these errors, ensuring your application doesn’t crash and burn. Instead, it continues running smoothly, providing a better experience for users.

In this blog, you’ll learn how to master exception handling in Java with examples. From understanding the basics to implementing best practices, it will equip you with the tools to write resilient, error-tolerant code. 

Let’s get started!

What are Exceptions in Java and Their Significance? Understand with Examples

In Java, exceptions are disruptions that occur during the program's execution, preventing it from proceeding normally. These runtime issues don’t have to spell disaster. Instead, Java provides a structured framework for detecting and managing these errors.

By handling exceptions, you can ensure smooth program flow, provide meaningful error messages to users, and safeguard against crashes. 

Let’s explore exception handling in Java in more detail!

Understanding Exceptions in Java

Exceptions in Java are broadly categorized into two types: checked and unchecked exceptions. These categories help you determine which errors must be explicitly handled and which can occur without interrupting the program.

Have a look at the comparison table of both:

Aspect Checked Exceptions Unchecked Exceptions
Definition Exceptions checked at compile time. Exceptions that occur at runtime.
Examples IOException, SQLException ArithmeticException, NullPointerException
Handling Requirement Must be handled using try-catch or declared with throws. No explicit handling required, but can be managed using try-catch.
Occurrence Often caused by external resources like files or databases. Arises from logical errors in the code.

To understand its implementation better, go through the examples given.

1. Checked Exception Example:

import java.io.FileReader;
import java.io.IOException;

public class CheckedExceptionExample {
    public static void main(String[] args) {
        try {
            // Attempting to read a file that may not exist
            FileReader file = new FileReader("nonexistentfile.txt");
        } catch (IOException e) {
            // Handling the IOException
            System.out.println("Error: File not found!");
        }
    }
}

Output:

Error: File not found!

Code Explanation:

  • The FileReader attempts to open a file that doesn’t exist, which triggers an IOException.
  • The compiler forces you to handle the IOException because it’s a checked exception.

2. Unchecked Exception Example:

public class UncheckedExceptionExample {
    public static void main(String[] args) {
        // Attempting to divide by zero
        int result = 10 / 0;  // Triggers ArithmeticException
        System.out.println("Result: " + result);
    }
}

Output:

Exception in thread "main" java.lang.ArithmeticException: / by zero

Code Explanation:

  • Dividing by zero triggers an ArithmeticException at runtime.
  • Unlike checked exceptions, the compiler doesn’t enforce this error but should be addressed logically to prevent program failure.

Hierarchy of Exceptions in Java

Java’s exception system follows a clear hierarchy that starts with the Throwable class. This class has two main subclasses: Error and Exception. This structure helps you understand how to categorize and manage different types of errors.

Here’s an overview of the hierarchy of Java expectation classes:

  • Throwable: The root of all exceptions and errors in Java.
  • Error: Represents severe issues beyond the application’s control (e.g., OutOfMemoryError, StackOverflowError).
  • Exception: Represents application-level issues that can be caught and handled.

Have a look at a code example demonstrating the hierarchy:

public class ExceptionHierarchyExample {
    public static void main(String[] args) {
        try {
            // Attempting to access a null object
            String str = null;
            System.out.println(str.length());  // Triggers NullPointerException
        } catch (NullPointerException e) {
            System.out.println("Caught a NullPointerException!");
        } catch (Exception e) {
            System.out.println("Caught a general Exception!");
        }
    }
}

Output:

Caught a NullPointerException!

Explanation:

  • Accessing a null object triggers a NullPointerException, a RuntimeException subclass.
  • The exception is caught by the specific NullPointerException block first. It would fall back to the general Exception block if not handled there.

By understanding these fundamental concepts of exception handling in Java, you can write resilient, error-free applications. 

 

If you wish to upskill and enhance these in-demand tech concepts, explore upGrad’s comprehensive software development programs from top universities!

 

Now, in any robust application, errors are inevitable — but how you handle them makes all the difference. Let’s see how you do it!

How Java Facilitates Efficient Exception Handling and Essential Methods

Java, one of the most developer-friendly languages, offers a structured exception-handling framework to tackle errors efficiently without disrupting the program flow. It ensures you’re not just catching errors but managing them intelligently. 

Exception handling in Java includes key constructs like try-catch, finally, throw, and throws, which enable developers to handle errors effectively, combined with robust APIs and tools.

Let’s explore how these mechanisms work and how they prevent abrupt program failures.

1. try-catch: Controlled Error Resolution

  • The try block contains code that might throw an exception.
  • The catch block handles specific exceptions, allowing you to define a response.

Code Example:

public class TryCatchExample {
    public static void main(String[] args) {
        try {
            int result = 10 / 0;  // Triggers ArithmeticException
        } catch (ArithmeticException e) {
            System.out.println("Error: Cannot divide by zero!");
        }
    }
}

Output:

Error: Cannot divide by zero!

In this code example of try-catch in Java, the program gracefully handles the exception instead of crashing and continues.

2. finally: Ensuring Cleanup

The finally block is always executed, regardless of whether an exception occurs, making it ideal for cleanup operations like closing files or releasing resources.

Code Example:

public class FinallyExample {
    public static void main(String[] args) {
        try {
            int[] numbers = {1, 2};
            System.out.println(numbers[5]);  // Triggers ArrayIndexOutOfBoundsException
        } catch (ArrayIndexOutOfBoundsException e) {
            System.out.println("Error: Index out of bounds!");
        } finally {
            System.out.println("Execution complete.");
        }
    }
}

Output:

Error: Index out of bounds!  
Execution complete.

In this example, the ‘finally’ block ensures that critical tasks are completed, even if an exception occurs.

3. throw: Manually Raising Exceptions

  • The throw keyword is used explicitly to raise an exception in the program.
  • It is typically used when a specific condition in your code requires you to generate an error manually.

Code Example:

public class ThrowExample {
    public static void main(String[] args) {
        int age = 15;
        if (age < 18) {
            throw new IllegalArgumentException("Age must be 18 or older.");
        }
    }
}

Output:

Exception in thread "main" java.lang.IllegalArgumentException: Age must be 18 or older.

In the code snippet, when the condition age < 18 is met, the program raises an IllegalArgumentException.

4. throws: Declaring Exceptions

The throws keyword is used to declare the exceptions a method might throw, allowing the caller of the method to handle them.

Code Example:

public class ThrowsExample {
    public static void checkNumber(int number) throws Exception {
        if (number < 0) {
            throw new Exception("Negative number not allowed!");
        }
    }

    public static void main(String[] args) {
        try {
            checkNumber(-5);
        } catch (Exception e) {
            System.out.println("Error: " + e.getMessage());
        }
    }
}

Output:

Error: Negative number not allowed!

Here, the throws keyword informs the caller that checkNumber might throw an exception, which must be handled explicitly in the calling method.

This structured way of exception handling in Java prevents programs from abruptly stopping by:

  • Isolating Errors: Code prone to errors is isolated within try blocks, ensuring the rest of the program is unaffected.
  • Providing Recovery Paths: The catch block offers a way to handle errors, recover gracefully, or provide alternative logic.
  • Cleanup with finally: Resources like file handles or network connections are properly closed, preventing resource leaks.

Also Read: File Handling in Java: How to Work with Java Files?

Do you also wish to write codes like this fluently? Because Python is an easy language to learn, you can headstart your journey with upGrad’s basic Python programming course and explore further!

While Java’s built-in exceptions cover many standard error scenarios, some applications require custom error messages or specific behavior.

This is where custom exceptions come in. Let’s dive into it!

Designing and Implementing Custom Exceptions in Java

A custom exception allows you to create meaningful, application-specific error handling. For example, if you’re building a banking application, you might want to create exceptions like InsufficientFundsException or AccountNotFoundException to represent domain-specific errors.

Custom exceptions are useful when:

  • The built-in exceptions don’t clearly convey the problem in your application’s context.
  • You must add extra information or logic to the error, such as user-friendly messages or custom behavior.
  • You want to improve code readability and make debugging easier.

Now, let’s see how to create the custom exception step-by-step:

Step 1. Decide the Type of Custom Exception:

  • Extend the Exception class if you want your exception to be a checked exception (requires explicit handling).
  • Extend the RuntimeException class if you want it to be an unchecked exception (it can occur without explicit handling).

Step 2. Define the Exception Class:

Add constructors to initialize the exception with custom messages or additional data.

Step 3. Throw and Handle the Custom Exception:

  • Use the throw keyword to raise the exception.
  • Use try-catch blocks or throws declarations to handle it appropriately.

Code Example: Creating a Custom Exception

Now that you have got the theoretical steps, let’s see the complete process implementation with code examples to help you understand better.

1. Define the Custom Exception Class

// Defining a custom exception
class InsufficientFundsException extends Exception {
    // Constructor with a custom message
    public InsufficientFundsException(String message) {
        super(message);  // Pass the message to the Exception class
    }
}

2. Use the Custom Exception

public class CustomExceptionExample {
    // Method to simulate withdrawing money
    public static void withdraw(double amount, double balance) throws InsufficientFundsException {
        if (amount > balance) {
            // Throw the custom exception if funds are insufficient
            throw new InsufficientFundsException("Insufficient funds: Unable to withdraw " + amount);
        }
        System.out.println("Withdrawal successful! Remaining balance: " + (balance - amount));
    }

    public static void main(String[] args) {
        double balance = 500.0;  // Initial account balance

        try {
            withdraw(600.0, balance);  // Attempt to withdraw more than the balance
        } catch (InsufficientFundsException e) {
            // Handle the custom exception
            System.out.println("Error: " + e.getMessage());
        }
    }
}

Output:

Error: Insufficient funds: Unable to withdraw 600.0

In the above code snippet:

  • A constructor is provided to accept a custom error message, which is passed to the parent Exception class.
  • The withdraw method checks if the withdrawal amount exceeds the balance. If true, it throws the InsufficientFundsException.
  • In the main method, a try-catch block handles the exception.

 

Wish to build your foundational knowledge of Java? Start with upGrad’s core Java basics course and explore further!

 

Also Read: How to Code, Compile, and Run Java Projects

Exceptions are an integral part of programming, and as a Java developer, you’ll encounter various of them. Let’s understand them!

Identifying and Understanding Common Java Exceptions

Understanding the most common exceptions and their causes is essential for debugging effectively and writing error-resilient code. 

Let’s explore some of the frequently encountered exceptions in Java, their causes, and how they occur in everyday coding.

1. NullPointerException: Accessing Null References

This exception occurs when you attempt to access or modify an object that has not been initialized (i.e., is null).

How It Happens:

  • Calling a method on a null object.
  • Accessing a null object’s properties.

Code Example:

public class NullPointerExample {
    public static void main(String[] args) {
        String text = null;  // Uninitialized string
        System.out.println(text.length());  // Triggers NullPointerException
    }
}

Output:

Exception in thread "main" java.lang.NullPointerException

How to Avoid: Always check for null before accessing objects, as shown in the below code:

if (text != null) {
    System.out.println(text.length());
}

2. IOException: Input/Output Operation Failures

If something goes wrong, this exception is thrown during I/O operations, such as reading from or writing to a file.

How It Happens:

  • File not found.
  • Insufficient permissions to access the file.

Code Example:

import java.io.FileReader;
import java.io.IOException;

public class IOExceptionExample {
    public static void main(String[] args) {
        try {
            FileReader file = new FileReader("nonexistentfile.txt");  // File not found
        } catch (IOException e) {
            System.out.println("Error: " + e.getMessage());
        }
    }
}

Output:

Error: nonexistentfile.txt (No such file or directory)

How to Avoid:

  • Verify file paths and permissions before performing file operations.
  • Use exception handling (try-catch) to handle potential errors gracefully.

3. ArithmeticException: Invalid Arithmetic Operations

This exception is triggered by invalid arithmetic operations, such as dividing a number by zero.

How It Happens:

  • Performing a division by zero.
  • Using invalid mathematical expressions.

Code Example:

public class ArithmeticExample {
    public static void main(String[] args) {
        int result = 10 / 0;  // Division by zero
        System.out.println("Result: " + result);
    }
}

Output:

Exception in thread "main" java.lang.ArithmeticException: / by zero

How to Avoid: Check denominators before performing division. See how it’s done below:

if (denominator != 0) {
    System.out.println(10 / denominator);
} else {
    System.out.println("Cannot divide by zero.");
}

Understanding these exceptions is the first step toward mastering exception handling in Java with examples!

Also Read: Top 32 Exception Handling Interview Questions and Answers [For Freshers & Experienced]

Next, let’s get into some advanced techniques for exception handling in Java with examples!

upGrad’s Exclusive Software Development Webinar for you –

SAAS Business – What is So Different?

 

 

 

Advanced Techniques for Exception Handling in Java

Exception handling in Java goes beyond basic try-catch blocks. Advanced techniques enable developers to write cleaner, more efficient, context-aware, error-handling code.

Let’s explore these advanced concepts of exception handling in Java with examples.

1. Exception Propagation

Exception propagation is a mechanism in Java where an exception raised in one method moves up the call stack until a try-catch block catches it or terminates the program if unhandled.

When an exception occurs:

  • The method where it’s thrown doesn’t handle it.
  • It is passed (propagated) to the method that called it.
  • This continues up the stack until it’s caught or the program terminates.

Have a look at the code example below:

public class ExceptionPropagationExample {
    // Method that throws an exception
    public static void methodA() {
        throw new ArithmeticException("Division by zero in methodA");
    }

    // Method that calls methodA
    public static void methodB() {
        methodA();  // Exception propagates to methodB
    }

    public static void main(String[] args) {
        try {
            methodB();  // Exception propagates to main
        } catch (ArithmeticException e) {
            System.out.println("Caught Exception: " + e.getMessage());
        }
    }
}

Output:

Caught Exception: Division by zero in methodA

Code Explanation:

  • methodA: Throws an ArithmeticException.
  • methodB: Does not handle the exception, so it propagates to main().
  • main(): Catches the exception and prints the message.

This shows how exceptions travel up the call stack, allowing centralized error handling.

2. Chained Exceptions

Sometimes, one exception causes another. Chaining exceptions lets you link these related errors, preserving the full context for debugging

This is particularly useful when you want to:

  • Preserve the original exception.
  • Add additional context to the error message.

Have a look at the code example below:

public class ChainedExceptionExample {
    public static void main(String[] args) {
        try {
            methodA();
        } catch (Exception e) {
            System.out.println("Caught Exception: " + e.getMessage());
            System.out.println("Caused by: " + e.getCause());
        }
    }

    // Method that simulates an error and chains it
    public static void methodA() throws Exception {
        try {
            int result = 10 / 0;  // Triggers ArithmeticException
        } catch (ArithmeticException e) {
            throw new Exception("Error in methodA", e);  // Chaining exception
        }
    }
}

Output:

Caught Exception: Error in methodA  
Caused by: java.lang.ArithmeticException: / by zero

Code Explanation:

  • The ArithmeticException is caught in methodA and wrapped in a new Exception using chaining.
  • In main(), both the original and chained exceptions are accessible for debugging, preserving the context.

3. Using try-with-resources (Java 7 and Above)

Managing resources like file streams or database connections can be tricky. If not closed properly, they can lead to resource leaks. 

The try-with-resources statement, introduced in Java 7, automates resource management by automatically closing resources when no longer needed.

Have a look at the code example below:

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class TryWithResourcesExample {
    public static void main(String[] args) {
        // Using try-with-resources for automatic resource management
        try (BufferedReader reader = new BufferedReader(new FileReader("example.txt"))) {
            String line;
            while ((line = reader.readLine()) != null) {
                System.out.println(line);  // Print file content
            }
        } catch (IOException e) {
            System.out.println("Error reading the file: " + e.getMessage());
        }
    }
}

Output (if file exists):

File content line 1  
File content line 2  
...

Output (if file doesn’t exist):

Error reading the file: example.txt (No such file or directory)

Code Explanation:

  • Automatic Resource Management: The BufferedReader resource is automatically closed after the try block, even if an exception occurs.
  • Simplified Code: No need for explicit finally blocks to close resources manually.

There you go! You must practice these concepts to take your exception handling in Java skills to the next level!

Also Read: Top 10 Java Books to Read to Improve Your Knowledge

Best Practices for Robust Exception Handling in Java

Exception handling in Java is not just about catching errors; it’s about doing so in a way that ensures your application remains reliable, maintainable, and efficient. Following best practices helps you write clean and predictable code that is easier to debug.

Let’s explore key practices to make your exception handling robust and effective.

1. Catch Specific Exceptions

Always catch specific exceptions instead of using generic ones like Exception or Throwable. Specific handlers make debugging easier and provide clearer error messages.

Understand it with the code snippet below:

try {
    int result = 10 / 0;
} catch (ArithmeticException e) {
    System.out.println("Arithmetic Error: Division by zero.");
}

2. Log Exceptions with Meaningful Messages

Use proper logging frameworks (e.g., Log4j, SLF4J) to log exceptions. Include meaningful messages and stack traces to provide a clear context for debugging.

See how it should be implemented:

import java.util.logging.Logger;

public class LoggingExample {
    private static final Logger logger = Logger.getLogger(LoggingExample.class.getName());

    public static void main(String[] args) {
        try {
            int result = 10 / 0;
        } catch (ArithmeticException e) {
            logger.severe("Arithmetic error occurred: " + e.getMessage());
        }
    }
}

3. Avoid Using Exceptions for Control Flow

Exceptions are expensive in performance and should not replace regular conditional checks. Use proper logic to handle predictable conditions instead of relying on exceptions.

Example (What NOT to Do):

try {
    int[] numbers = {1, 2, 3};
    System.out.println(numbers[5]);  // Avoid triggering exceptions deliberately
} catch (ArrayIndexOutOfBoundsException e) {
    System.out.println("Index out of bounds!");  // Poor control flow
}

Better Approach:

int[] numbers = {1, 2, 3};
if (5 < numbers.length) {
    System.out.println(numbers[5]);
} else {
    System.out.println("Index out of bounds!");
}

4. Use try-with-resources for Efficient Resource Management

Always use try-with-resources to manage resources like file streams and database connections. It ensures resources are closed automatically, even in the event of an exception.

Here’s how to do it:

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class TryWithResourcesBestPractice {
    public static void main(String[] args) {
        try (BufferedReader reader = new BufferedReader(new FileReader("example.txt"))) {
            String line;
            while ((line = reader.readLine()) != null) {
                System.out.println(line);
            }
        } catch (IOException e) {
            System.out.println("Error reading file: " + e.getMessage());
        }
    }
}

Adopting these best practices will make your applications more resilient and maintainable, helping you become a better Java developer.

Also Read: Java Developer Salary: What to Expect in India 2025

How can upGrad Help You Advance Your Java Skills?

Do you want to level up your Java skills and confidently tackle real-world programming challenges? Mastering core concepts like exception handling in Java is a game-changer for developers aiming to build robust, error-free applications.

upGrad specializes in bridging the gap between learning and career advancement. Their Java programs are tailored to equip you with foundational and advanced skills, ensuring you’re ready to tackle industry challenges head-on. 

Some of the top courses include:

 

Take the first step today: book a career counseling session with upGrad experts, upskill, and excel in your future ahead!

 

Boost your career with our popular Software Engineering courses, offering hands-on training and expert guidance to turn you into a skilled software developer.

Master in-demand Software Development skills like coding, system design, DevOps, and agile methodologies to excel in today’s competitive tech industry.

Stay informed with our widely-read Software Development articles, covering everything from coding techniques to the latest advancements in software engineering.

References: 
https://www.cogentinfo.com/resources/java-developer-demographics-and-statistics-in-the-us

Frequently Asked Questions

1. What is exception handling in Java?

It’s a mechanism to detect and handle runtime errors, ensuring smooth program execution.

2. Why is exception handling important in Java?

It prevents abrupt program termination and helps manage errors gracefully, improving reliability.

3. What are checked and unchecked exceptions?

Checked exceptions are enforced by the compiler (e.g., IOException), while unchecked exceptions occur at runtime (e.g., NullPointerException).

4. What is exception propagation?

It refers to how exceptions move up the call stack until they’re caught or cause program termination.

5. What is the purpose of custom exceptions in Java?

Custom exceptions help define application-specific errors with meaningful context.

6. How does try-with-resources work?

It ensures the automatic closure of resources like file streams after use, preventing resource leaks.

7. What is a NullPointerException?

This occurs when you try to access or modify a null object reference.

8. What is exception chaining?

It links multiple exceptions to preserve context, providing more details about the root cause.

9. Why should exceptions not be used for control flow?

Using exceptions for control flow reduces performance and makes code harder to maintain.

10. How can exceptions be logged effectively?

Use frameworks like Log4j or SLF4J to log exceptions with meaningful messages and stack traces.

11. How can upGrad help me learn Java exception handling?

upGrad offers programs like Advanced Certificate in Java Programming, featuring in-depth modules on exception handling and practical projects.

RELATED PROGRAMS