Ooops Unit 2
Ooops Unit 2
Exception indicates
An Error indicates a serious
conditions that a reasonable
problem that a reasonable
Definition application might try to catch
Aspect Error Exception
OutOfMemoryError IOException
Examples StackOverFlowError NullPointerException
Checked Exception
Unchecked Exception
2. User-Defined Exceptions
1. Built-in Exception
Build-in Exception are pre-defined exception classes provided by Java to handle
common errors during program execution.
1.1 Checked Exceptions
Checked exceptions are called compile-time exceptions because these exceptions
are checked at compile-time by the compiler. Examples of Checked Exception are
listed below:
1. ClassNotFoundException: Throws when the program tries to load a class at
runtime but the class is not found because it’sbelong not present in the correct
location or it is missing from the project.
2. InterruptedException: Thrown when a thread is paused and another thread
interrupts it.
3. IOException: Throws when input/output operation fails
4. InstantiationException: Thrown when the program tries to create an object of
a class but fails because the class is abstract, an interface, or has no default
constructor.
5. SQLException: Throws when there’s an error with the database.
6. FileNotFoundException: Thrown when the program tries to open a file that
doesn’t exist
1.2 Unchecked Exceptions
The unchecked exceptions are just opposite to the checked exceptions. The
compiler will not check these exceptions at compile time. In simple words, if a
program throws an unchecked exception, and even if we didn’t handle or declare it,
the program would not give a compilation error. Examples of Unchecked Exception
are listed below:
1. ArithmeticException: It is thrown when there’s an illegal math operation.
2. ClassCastException: It is thrown when you try to cast an object to a class it
does not belongThis to.
3. NullPointerException: It is thrown when you try to use a null object (e.g.
accessing its methods or fields)
4. ArrayIndexOutOfBoundsException: ThisThis occurs when we try to access
an array element with an invalid index.
5. ArrayStoreException: Thishandle happens when you store an object of the
wrong type in an array.
6. IllegalThreadStateException: It is thrown when a thread operation is not
allowed in its current state
User-Defined Exception
Sometimes, the built-in exceptions in Java are not able to describe a certain situation.
In such cases, users can also create exceptions, which are called “user-defined
Exceptions“.
Methods to Print the Exception Information
Method Description
Base
Derived from Exception Derived from RuntimeException
class
External factors like file I/O and Programming bugs like logical
database connection cause the errors cause unchecked
Cause checked Exception. Exceptions.
Java catch block is used to handle the Exception by declaring the type of exception
within the parameter. The declared exception must be the parent class exception ( i.e.,
Exception) or the generated exception type. However, the good approach is to declare
the generated type of exception.
The catch block must be used after the try block only. You can use multiple catch block
with a single try block.
finally Block
The finally Block is used to execute important code regardless of whether an
exception occurs or not.
Note: finally block is always executes after the try-catch block. It is also used for
resource cleanup.
try {
// Code that may throw an exception
} catch (ExceptionType e) {
// Code to handle the exception
}finally{
// cleanup code
}
Difference Between throw and throws in Java
The differences between throw and throws in Java are:
S. Key
No. Difference throw throws
ArithmeticException Example
public class ArithmeticExceptionDemo {
public static void main(String[] args) {
int a = 10;
int b = 0; // This will cause division by zero
try {
int result = a / b; // ArithmeticException occurs here
System.out.println("Result: " + result);
} catch (ArithmeticException e) {
System.out.println("Exception caught: " + e.getMessage());
} finally {
System.out.println("Finally block executed.");
}
}
}
Output:
Exception caught: / by zero
Finally block executed.
NullPointerException Example
public class NullPointerExceptionDemo {
public static void main(String[] args) {
String str = null; // str is not pointing to any object
try {
// This will throw NullPointerException
int length = str.length();
System.out.println("Length of the string: " + length);
} catch (NullPointerException e) {
System.out.println("Exception caught: " + e.getMessage());
} finally {
System.out.println("Finally block executed.");
}
}
}
Output:
Exception caught: Cannot invoke "String.length()" because "str" is null
Finally block executed.
try {
// Trying to access an out-of-bounds index
System.out.println("Value at index 5: " + arr[5]);
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Exception caught: " + e);
System.out.println("Please access a valid index (0 to " + (arr.length - 1) + ").");
}
System.out.println("Program continues after exception handling.");
}
}
Output:
Exception caught: java.lang.ArrayIndexOutOfBoundsException: Index 5 out of bounds for
length 5
Please access a valid index (0 to 4).
Program continues after exception handling.
(StringIndexOutOfBoundsException Example)
try {
char ch = str.charAt(5);
} catch (StringIndexOutOfBoundsException e) {
}
OUTPUT:
Exception caught: java.lang.StringIndexOutOfBoundsException: String index out of
range: 5
Valid indices are from 0 to 4
Program continues after exception handling.
What is ClassNotFoundException in Java?
ClassNotFoundException is a checked exception in Java that occurs when an
application tries to load a class at runtime using:
Class.forName("...")
ClassLoader.loadClass("...")
...and the class cannot be found in the classpath.
Key Points:
try {
Class.forName("com.example.NonExistentClass");
} catch (ClassNotFoundException e) {
Output:
Exception caught: java.lang.ClassNotFoundException: com.example.NonExistentClass
The specified class was not found in the classpath.
Program continues after handling ClassNotFoundException.
try {
} catch (ClassCastException e) {
}
}
Output:
Exception caught: java.lang.ClassCastException: java.lang.String cannot be cast to
java.lang.Integer
Cannot cast String to Integer.
Program continues after exception handling.
try {
} catch (NumberFormatException e) {
OUTPUT:
Exception caught: java.lang.NumberFormatException: For input string: "123abc"
Invalid number format: '123abc'
Program continues after exception handling.
if (age < 0) {
try {
} catch (IllegalArgumentException e) {
OUTPUT:
Exception caught: java.lang.IllegalArgumentException: Age cannot be negative: -5
Program continues after exception handling.
Java Program Demonstrating throw and throws
Scenario:
You are developing a banking login system where a user is allowed to enter their PIN only 3
times. If the user fails to enter the correct PIN after 3 attempts, the system should throw a
custom exception called TooManyAttemptsException and lock the account.
Question:
Write a Java program that simulates a simple PIN verification system. The correct PIN is 1234. The
user is allowed a maximum of 3 attempts to enter the correct PIN.
If the user fails all 3 times, throw a custom exception TooManyAttemptsException with an
appropriate message.
Catch and handle the exception in the main method and display the error message
import java.util.Scanner;
// Custom exception class
super(message);
int attempts = 0;
if (inputPin == CORRECT_PIN) {
return;
} else {
attempts++;
System.out.println("Incorrect PIN. Attempt " + attempts + " of " + MAX_ATTEMPTS);
try {
verifyPin();
} catch (TooManyAttemptsException e) {
System.out.println("Program ends.");
Output:
Enter your 4-digit PIN: 1111
Incorrect PIN. Attempt 1 of 3
Enter your 4-digit PIN: 2222
Incorrect PIN. Attempt 2 of 3
Enter your 4-digit PIN: 3333
Incorrect PIN. Attempt 3 of 3
Exception caught: Too many incorrect attempts. Your account is locked.
Program ends.
Scenario:
You are building a system for a library that allows users to check out books by entering their
library card number. The library card number must be a 6-digit numeric value. Users will
input their card numbers as strings, and the system must validate the format of the card
number.
If a user enters a card number that doesn't match the expected 6-digit numeric format, the
system should throw a NumberFormatException (which is a subclass of
InputMismatchException) and notify the user that the card number is invalid.
import java.util.Scanner;
if (!input.matches("\\d{6}")) {
validateCardNumber(cardNumber);
} catch (NumberFormatException e) {
System.out.println("Program ends.");
OUTPUT:
Enter your 6-digit library card number: 123456
Library card number 123456 is valid. Access granted.
Program ends.
Enter your 6-digit library card number: ab123
Exception caught: Invalid library card number. Must be exactly 6 digits.
Program ends.
Scenario:
You are developing a simple ATM-like application that allows users to withdraw money from their
account. The application asks the user to enter the amount to withdraw, which must be an integer
value (e.g., 100, 500, 1000).
However, if the user accidentally enters a non-integer input such as text ("abc") or a decimal
number ("100.50"), the program should catch an InputMismatchException, inform the user of
the mistake, and allow them to try again.
import java.util.Scanner;
import java.util.InputMismatchException;
int amount = 0;
while (!validInput) {
try {
if (amount <= 0) {
} else {
} catch (InputMismatchException e) {
OutPut: