⚠️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
Catches an ArrayIndexOutOfBoundsException from accessing an invalid array index.
View Program →Demonstrates the core try-catch mechanism with a manually thrown exception.
View Program →Demonstrates a built-in checked exception the compiler forces you to handle.
View Program →Catches a ClassCastException from casting an object to an incompatible type.
View Program →Shows that a finally block still runs even when the try block returns early.
View Program →Catches an ArithmeticException from integer division.
View Program →Catches an InputMismatchException from reading the wrong type with Scanner.
View Program →Catches two different exception types from one try block, each with its own catch clause.
View Program →Catches a NullPointerException from calling a method on a null reference.
View Program →Catches a NumberFormatException from parsing a non-numeric string.
View Program →Uses the throw statement to raise an exception when a precondition fails.
View Program →Demonstrates a built-in unchecked exception thrown to reject invalid input.
View Program →Defines and throws a user-defined exception class.
View Program →Catches a failure fully inside a method and returns a safe default instead of letting it escape.
View Program →Catches a specific exception through a reference to one of its ancestor classes.
View Program →Lets an exception travel up through several method calls before anything catches it.
View Program →Catches the exception thrown when opening a file that doesn't exist.
View Program →Catches an I/O failure at the general IOException level instead of a specific subtype.
View Program →Handles two different exception types with a single catch clause.
View Program →Handles a narrow failure with an inner try-catch while an outer try-catch guards a broader operation.
View Program →Catches an exception, reacts to it locally, then throws the same exception again for a caller further up to also handle.
View Program →Declares a checked exception on a method signature and lets the caller handle it.
View Program →Automatically closes a custom resource using try-with-resources syntax.
View Program →