Untitled Document
Untitled Document
runtime errors so that the normal flow of the application can be maintained.
exceptions are categorized into two main types: checked exceptions and
unchecked exceptions.
In Java, exceptions are categorized into two main types: checked exceptions and
unchecked exceptions. Additionally, there is a third category known as errors.
Let's delve into each of these types:
1. Checked Exception
2. Unchecked Exception
3. Error
Errors
Errors represent exceptional conditions that are not expected to be caught under
normal circumstances. They are typically caused by issues outside the control of
the application, such as system failures or resource exhaustion. Errors are not
meant to be caught or handled by application code. Examples of errors include:
Checked Exceptions
Checked exceptions are the exceptions that are checked at compile-time. This
means that the compiler verifies that the code handles these exceptions either
by catching them or declaring them in the method signature using the throws
keyword.
1. FileNotFoundException:
The FileNotFoundException is thrown while an attempt to get right of entry to a
record that does not exist or cannot be opened takes place. It is part of the java.io
package. example:
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class FileNotFoundExceptionExample {
public static void main(String[] args) {
try {
File = new File("nonexistent_file.txt");
Scanner = new Scanner(file);
} catch (FileNotFoundException e) {
System.out.println("File not found: " + e.getMessage());
}
}
}
2. IOException:
The IOException class is a well-known exception that suggests a mistake passed
off while acting enter/output operations. It is part of the java.io package. The
following program demonstrates the coping with of an IOException:
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class IOException {
public static void main(String[ ] args) {
String filePath = "example.txt";
try (BufferedReader reader = new BufferedReader(new
FileReader(filePath))) {
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
System.out.println("An error occurred while reading the
file:"+e.getMessage());
e.printStackTrace();
}
}
}
3. SQLException:
The SQLException is thrown when an error occurs while working with a database.
It is part of the java.sql package. The code snippet below illustrates a basic
scenario involving a database connection:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class SQLExceptionExample {
public static void main(String[ ] args) {
try {
Connection=DriverManager.getConnection("jdbc:mysql://localhost:3306
/mydatabase","username", "password");
connection.close();
}
catch (SQLException e) {
System.out.println("SQL error occurred: " + e.getMessage());
}
}
}
4.ParseException:
The ParseException is thrown when an error occurs while parsing a string into a
selected format, along with a date or more than a few. It is part of the java.text
content package. Consider the following example:
ParseExceptionExample.java
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class ParseException {
public static void main(String[ ] args) {
DateFormat = new SimpleDateFormat("yyyy-MM-dd");
String invalidDate = "2023-13-45";
try {
Date parsedDate = dateFormat.parse(invalidDate);
System.out.println("Parsed date: " + parsedDate);
} catch (ParseException e) {
System.out.println("Invalid date format: " + e.getMessage());
}
}
}
5.nterruptedException:
The InterruptedException is thrown when a thread is interrupted while it is in a
sleeping or waiting state. It is often used to handle scenarios involving thread
synchronization and concurrency. Here's an example:
6.MalformedURLException:
The MalformedURLException is thrown when an invalid or malformed URL is
encountered. It is part of the java.net package. Consider the following program:
import java.net.MalformedURLException;
import java.net.URL;
public class MalformedURLException {
public static void main(String[] args) {
String invalidURL = "http://example.com";
try {
URL url = new URL(invalidURL);
System.out.println("Valid URL: " + url.toString());
} catch (MalformedURLException e) {
System.out.println("Malformed URL: " + e.getMessage());
}
}
}
7.ClassNotFoundException:
The ClassNotFoundException is thrown when an attempt is made to load a class
dynamically using the Class.forName() method, but the specified class cannot be
found.
Types
Arithmetic exception : It is a type of unchecked error in code that is thrown
whenever there is a wrong mathematical or arithmetic calculation in the code,
especially during run time. For instance, when the denominator in a fraction is
zero, the arithmetic exception is thrown.
public class Arithmetic {
ae.divideInt(1, 0);
ref.toString();
System.out.println(arr[i]);
int a = Integer.parseInt(null);
System.out.println(name);
System.out.println(age);
list.add("apples");
list.add("mangoes");
ListIterator<String> it = list.listIterator();
it.remove();
}
}