🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
In this article, we will learn the exception class hierarchy in java.lang package.
- Error
- Exception.
Exceptions Hierarchy in Java
The figure below illustrates the class hierarchy of the Throwable class and its most significant subclasses.
Exception Class Example: In this example, FileReader class try to read a file from an invalid location will throw FileNotFoundException exception.
1. Error Class
The Error class and its subclasses represent abnormal conditions that should generally not be caught in your application. These indicate serious system problems, often JVM related, which an application might not be able to handle.
Hierarchy of Error Class
The figure below illustrates the class hierarchy of Error Class:Learn more about below Java built-in errors:
2. Exception
Checked Exceptions
These are exceptions that a method can throw but must either catch them or declare them using the throws keyword. They are direct subclasses of the Exception class, except RuntimeException.
Java built-in checked exceptions:
The figure below illustrates the class hierarchy of the Exception Class:
Exception Class Example: In this example, FileReader class try to read a file from an invalid location will throw FileNotFoundException exception.
public class FileNotFoundExceptionExample {
public static void main(String[] args) {
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(new File("/invalid/file/location")));
} catch (FileNotFoundException e) {
System.err.println("FileNotFoundException caught!");
}
}
}
Output:
FileNotFoundException caught!
RuntimeException (Unchecked Exceptions)
These exceptions are not checked at compile-time. They're a direct subclass of the RuntimeException class.
Java built-in unchecked exceptions:
The figure below illustrates the class hierarchy of the RuntimeException Class:
RuntimeException Example: In the below example, the Person class object is not created using the new keyword but it is just declared with a null value. Now we are trying to access the personName field value on the null reference so JVM will throw a NullPointerException exception here.
public class NullPointerExceptionExample {
public static void main(String[] args) {
Person personObj = null;
try {
String name = personObj.personName; // Accessing the field of a null object
personObj.personName = "Jon Doe"; // Modifying the field of a null object
} catch (NullPointerException e) {
System.err.println("NullPointerException caught!");
}
}
}
class Person {
public String personName;
public String getPersonName() {
return personName;
}
public void setPersonName(String personName) {
this.personName = personName;
}
}
Output:
NullPointerException caught!
Exception Handling Related Posts
- Java Exception Handling Guide
- How the Exception Handling Works in Java
- Three Types of Exceptions in Java
- Exceptions Hierarchy in Java
- Java Chained Exceptions
- java.lang.Throwable Class in Java
- Exception Handling Keywords in Java
- Java try/catch Block
- Java throw Keyword
- Java throws Keyword
- Java finally Block
- The try-with-resources Statement
- Java Exception Handling Best Practices
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
🆕 High-Demand
80–90% OFF
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
🆕 High-Demand
80–90% OFF
ChatGPT + Generative AI + Prompt Engineering for Beginners
🚀 Trending Now
80–90% OFF
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
🌟 Top Rated
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
🌟 Top Rated
80–90% OFF
Testing Spring Boot Application with JUnit and Mockito
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Master Spring Data JPA with Hibernate
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
🎓 Student Favorite
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business




Comments
Post a Comment
Leave Comment