Object Oriented Programming With Java
COSC-1102
Assignment: Java Exception Handling
Prepared by: Sana Khalid | Lecturer, Computer Science
Q1: What is Exception Handling?
Definition
Exception handling is a mechanism in Java that allows the program to deal with unexpected or
erroneous situations that arise during the execution of a program. When an error occurs, Java
throws an exception — meaning it generates an error message and normally stops execution.
There are many types of errors that can occur in a Java program:
• Coding errors made by the programmer
• Errors caused by wrong or unexpected user input
• Errors due to unforeseeable circumstances (e.g., file not found, network failure)
How Exception Handling Works in Java
Java uses the try and catch keywords to handle exceptions:
• The try block contains the code that might throw an exception.
• The catch block contains the code that runs if an exception is thrown.
• These two keywords always come in pairs.
Syntax:
try {
// Block of code to try
}
catch(Exception e) {
// Block of code to handle errors
}
Example — Without Exception Handling:
public class Main {
public static void main(String[] args) {
int[] myNumbers = {1, 2, 3};
[Link](myNumbers[10]); // error!
}
}
Output: Exception in thread "main" [Link]: 10
Example — With Exception Handling:
public class Main {
public static void main(String[] args) {
try {
int[] myNumbers = {1, 2, 3};
[Link](myNumbers[10]);
} catch (Exception e) {
[Link]("Something went wrong.");
}
}
}
Output: Something went wrong.
Q2: What is the finally Keyword?
Definition
The finally keyword is used to define a block of code that will always execute, regardless of
whether an exception was thrown or caught. It runs after the try...catch block completes.
The finally block is useful for:
• Closing file connections or database connections
• Releasing resources that were opened in the try block
• Ensuring cleanup code always runs no matter what happens
How finally is Used in Java
Syntax:
try {
// Code that might throw an exception
} catch (Exception e) {
// Code to handle the exception
} finally {
// Code that always executes
}
Example:
public class Main {
public static void main(String[] args) {
try {
int[] myNumbers = {1, 2, 3};
[Link](myNumbers[10]);
} catch (Exception e) {
[Link]("Something went wrong.");
} finally {
[Link]("The 'try catch' is finished.");
}
}
}
Output:
Something went wrong.
The 'try catch' is finished.
As shown above, both lines are printed — the catch block handles the error, and then the
finally block always runs afterward.
Q3: What is the throw Keyword?
Definition
The throw keyword in Java is used to manually create and throw a custom exception.
Instead of waiting for Java to automatically generate an error, the programmer can define their
own condition and throw an exception when it is met.
Exception Types Available in Java
The throw statement is used with an exception type. Some common exception types include:
• ArithmeticException — for arithmetic errors (e.g., divide by zero)
• FileNotFoundException — when a file is not found
• ArrayIndexOutOfBoundsException — when an invalid array index is accessed
• SecurityException — for security violations
How throw is Used in Java
Syntax:
throw new ExceptionType("Error message");
Example — Age Verification Using throw:
public class Main {
static void checkAge(int age) {
if (age < 18) {
throw new ArithmeticException(
"Access denied - You must be at least 18 years old.");
} else {
[Link]("Access granted - You are old enough!");
}
}
public static void main(String[] args) {
checkAge(15); // age is below 18
}
}
Output:
Exception in thread "main" [Link]:
Access denied - You must be at least 18 years old.
at [Link]([Link])
at [Link]([Link])
In this example, when checkAge(15) is called with an age below 18, the throw keyword is used
to throw an ArithmeticException with a custom message. This gives the programmer full
control over when and how exceptions are raised.
Summary Table
Keyword Purpose When it Executes
try Contains code that might cause an Always runs first
error
Keyword Purpose When it Executes
catch Handles the exception if one occurs Only runs if an exception is thrown
finally Cleanup code after try/catch Always runs, no matter what
throw Manually throws a custom exception When the programmer triggers it