Java Programs›Exceptions

⚠️Java Exceptions Programs

Good Java code fails gracefully. These programs show how exceptions are thrown, caught and propagated, when to create your own exception types, and how try-with-resources closes files and connections automatically.23 programs · each with full code, output and a step-by-step explanation

Most Asked in Interviews

🔍
Beginner
01beginner
ArrayIndexOutOfBoundsException
Error Handling

Catches an ArrayIndexOutOfBoundsException from accessing an invalid array index.

View Program →
02beginner
Basic Try-Catch
Error Handling

Demonstrates the core try-catch mechanism with a manually thrown exception.

View Program →
03beginner
Checked Exception
Error Handling

Demonstrates a built-in checked exception the compiler forces you to handle.

View Program →
04beginner
ClassCastException
Error Handling

Catches a ClassCastException from casting an object to an incompatible type.

View Program →
05beginner
Finally Block
Error Handling

Shows that a finally block still runs even when the try block returns early.

View Program →
06beginner
Handling Division by Zero
Error Handling

Catches an ArithmeticException from integer division.

View Program →
07beginner
InputMismatchException
Error Handling

Catches an InputMismatchException from reading the wrong type with Scanner.

View Program →
08beginner
Multiple Catch Blocks
Error Handling

Catches two different exception types from one try block, each with its own catch clause.

View Program →
09beginner
NullPointerException
Error Handling

Catches a NullPointerException from calling a method on a null reference.

View Program →
10beginner
NumberFormatException
Error Handling

Catches a NumberFormatException from parsing a non-numeric string.

View Program →
11beginner
Throw Exception
Error Handling

Uses the throw statement to raise an exception when a precondition fails.

View Program →
12beginner
Unchecked Exception
Error Handling

Demonstrates a built-in unchecked exception thrown to reject invalid input.

View Program →
Intermediate
13intermediate
Custom Exception
Error Handling

Defines and throws a user-defined exception class.

View Program →
14intermediate
Exception Handling in Methods
Error Handling

Catches a failure fully inside a method and returns a safe default instead of letting it escape.

View Program →
15intermediate
Exception Hierarchy
Error Handling

Catches a specific exception through a reference to one of its ancestor classes.

View Program →
16intermediate
Exception Propagation
Error Handling

Lets an exception travel up through several method calls before anything catches it.

View Program →
17intermediate
FileNotFoundException
Error Handling

Catches the exception thrown when opening a file that doesn't exist.

View Program →
18intermediate
IOException
Error Handling

Catches an I/O failure at the general IOException level instead of a specific subtype.

View Program →
19intermediate
Multi-Catch
Error Handling

Handles two different exception types with a single catch clause.

View Program →
20intermediate
Nested Try-Catch
Error Handling

Handles a narrow failure with an inner try-catch while an outer try-catch guards a broader operation.

View Program →
21intermediate
Rethrowing Exception
Error Handling

Catches an exception, reacts to it locally, then throws the same exception again for a caller further up to also handle.

View Program →
22intermediate
Throws Keyword
Error Handling

Declares a checked exception on a method signature and lets the caller handle it.

View Program →
23intermediate
Try-With-Resources
Error Handling

Automatically closes a custom resource using try-with-resources syntax.

View Program →