🎓 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
Introduction
RuntimeException in Java is an unchecked exception that occurs during the execution of a program. It represents programming errors that can be caught by the program but are not required to be handled.
Table of Contents
- What is
RuntimeException? - Characteristics of
RuntimeException - Common Subclasses
- Best Practices
- Examples
- Conclusion
1. What is RuntimeException?
RuntimeException is a superclass of exceptions that can occur during the normal operation of the JVM. It is part of the unchecked exceptions and does not need to be declared or caught.
2. Characteristics of RuntimeException
- Unchecked: Not required to be caught or declared in a method's
throwsclause. - Programming Errors: Represents errors such as logic flaws or improper use of APIs.
- Inheritance: Extends
java.lang.Exception.
3. Common Subclasses
- NullPointerException: Accessing a method or property on a
nullobject. - ArrayIndexOutOfBoundsException: Accessing an array with an illegal index.
- ClassCastException: Invalid casting of an object.
- IllegalArgumentException: Invalid arguments passed to a method.
- ArithmeticException: Errors like division by zero.
4. Best Practices
- Avoid Unnecessary Use: Use
RuntimeExceptionfor unexpected errors only. - Catch Specific Exceptions: Catch specific subclasses rather than the general
RuntimeException. - Input Validation: Prevent
RuntimeExceptionby validating inputs and handling possible errors.
5. Examples
Example 1: Catching NullPointerException
This example demonstrates how to handle a NullPointerException.
public class NullPointerExample {
public static void main(String[] args) {
try {
String str = null;
System.out.println(str.length());
} catch (NullPointerException e) {
System.out.println("Caught NullPointerException: " + e.getMessage());
}
}
}
Output:
Caught NullPointerException: Cannot invoke "String.length()" because "<local1>" is null
Example 2: Catching ArrayIndexOutOfBoundsException
This example shows how to handle an ArrayIndexOutOfBoundsException.
public class ArrayIndexExample {
public static void main(String[] args) {
try {
int[] numbers = {1, 2, 3};
System.out.println(numbers[5]);
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Caught ArrayIndexOutOfBoundsException: " + e.getMessage());
}
}
}
Output:
Caught ArrayIndexOutOfBoundsException: Index 5 out of bounds for length 3
Example 3: Using IllegalArgumentException
This example demonstrates throwing an IllegalArgumentException.
public class IllegalArgumentExample {
public static void main(String[] args) {
try {
printAge(-5);
} catch (IllegalArgumentException e) {
System.out.println("Caught IllegalArgumentException: " + e.getMessage());
}
}
public static void printAge(int age) {
if (age < 0) {
throw new IllegalArgumentException("Age cannot be negative.");
}
System.out.println("Age: " + age);
}
}
Output:
Caught IllegalArgumentException: Age cannot be negative.
6. Conclusion
RuntimeException in Java is used for errors that occur during the program's runtime and do not require explicit handling. By following best practices and catching specific exceptions, developers can create more robust and error-resistant applications.
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
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment