L30 Checked, Unchecked Exceptions
L30 Checked, Unchecked Exceptions
Unchecked
Exceptions
Checked Exception
2
Characteristics of Checked
Exceptions
They are exceptions that are checked at compile time by the Java
compiler.
They are usually exceptions that occur due to external conditions,
such as trying to open a file that does not exist or attempting to
access a database that is down.
Examples: IOException, SQLException, FileNotFoundException,
ClassNotFoundException, etc.
3
Handling Checked Exceptions
4
Example
import java.io.*;
6
Characteristics of Unchecked
Exceptions:
They are exceptions that are not checked at compile time.
These exceptions typically represent programming errors.
Unchecked exceptions are subclasses of RuntimeException or
Error.
They don't need to be declared or caught explicitly in the code.
If you don't handle them, the program will terminate with the
exception.
Examples: NullPointerException,
ArrayIndexOutOfBoundsException, ArithmeticException,
ClassCastException, IllegalArgumentException, etc.
7
Example
Checked Exception:
Checked Exception:
• Consider the program to read and prints first three lines of it.
• It also uses readLine() and close() methods, and these methods also
throw checked exception IOException
import java.io.*;
class prg
{
public static void main(String[] args)
{
FileReader file = new FileReader("C:\\test\\a.txt");
BufferedReader fileInput = new BufferedReader(file);
fileInput.close();
}
}
• To fix the program:
we either need to specify list of exceptions using throws, or
need to use try-catch block.
import java.io.*;
class prg
{
public static void main(String[] args)
{
try
{
FileReader file = new FileReader("C:\\test\\a.txt");
BufferedReader fileInput = new BufferedReader(file);
fileInput.close();
}
}
Unchecked Exceptions
• Exceptions that are not checked at compiled time.
class program
{
public static void main(String args[])
{
int x = 0;
int y = 10;
int z = y/x;
}
}
Unchecked Exceptions
Throwable
Exception Error
ArithmeticException FileNotFoundException
ArrayIndexOutOfBoundsException
EOFException
NullPointerException
NumberFormatException
Unchecked Exceptions
Runtime exception and its child class, Error and its
child classes are unchecked exception.
……..
Fully checked exception
RunTime Exception IOException
FileNotFoundException
ArithmeticException
EOFException
ArrayIndexOutOfBoundsException
NullPointerException
NumberFormatException
Create a user defined exception InvalidRegistrationNumber.
Write a java program to check whether the entered registration number
is valid or not.
If not throw an exception of type InvalidRegistrationNumber.
try {
// Step 4: Validate the registration number
validateRegistrationNumber(registrationNumber);
System.out.println("Registration number is valid.");
} catch (InvalidRegistrationNumber e) {
// Step 5: Handle the exception
System.out.println(e.getMessage());
} finally {
scanner.close();
}
}
}
28
Tutorial
Create an Array of 4 Product objects, where Product class contains the members:
productId, productName and productType with appropriate constructors and
methods.
If product type begins with string “prod”, then print the product details otherwise
throw an user exception of type InvalidProductTypeException, which prints the
message "Invalid Product Type Exception" along with the product type.
Use exception handling technique to catch and print the error object.
Tutorial
Write a java program to read 5 subject marks from the keyboard and store only
valid values in an array. If the number entered is negative throw user defined
exception called “NegativeNumberException”. The main java program
performing the work of reading and storing values into array.
Write a java program to read 5 subject marks from the keyboard and store only
valid values in an array. If the number entered is negative throw user defined
exception called “NegativeNumberException”. If the number entered is positive
and out of the range <0-100>, throw user defined exception called
“OutOfRangeException”. The main java program performing the work of
reading and storing values into array.