GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||||||||||||||||||||||
GeezerVison is showing www.tutorialspoint.com — Java - RuntimeException
Selected Reading
Java - RuntimeExceptionThe Java RuntimeException is a subclass of the Exception class. It is an unchecked exception that occurs during the runtime of the program. The RuntimeException class is the superclass of those exceptions that can be thrown during the normal operation of the Java Virtual Machine (JVM). It is part of the java.lang package and is used to indicate that a method has been passed an illegal or inappropriate argument. It is an unchecked exception, meaning it does not need to be declared in a method's throws clause if it can be thrown by the execution of the method and propagate outside the method boundary. Hierarchy of RuntimeExceptionGiven below is the class hierarchy of RuntimeException: java.lang.Object
↳ java.lang.Throwable
↳ java.lang.Exception
↳ java.lang.RuntimeException
Causes of RuntimeExceptionFollowing are the reasons when JVM throws a RuntimeException in Java:
Constructors of RuntimeExceptionThere are two constructors of RuntimeException class:
Methods of RuntimeExceptionThere are some methods of RuntimeException class: Methods of RuntimeExceptionThere are some methods of RuntimeException class:
Subclasses of RuntimeExceptionThere are many subclasses in RuntimeException. Some of the commonly used subclasses are:
Example of RuntimeExceptionIn this example, we are trying to divide a number by zero, so JVM will throw a RuntimeException. public class RuntimeExceptionExample {
public static void main(String[] args) {
int num1 = 10;
int num2 = 0;
int result = num1 / num2; // This will throw RuntimeException
}
}
OutputFollowing is the output of the above code: Exception in thread "main" java.lang.ArithmeticException: / by zero
at RuntimeExceptionExample.main(RuntimeExceptionExample.java:5)
Example 2 of RuntimeExceptionIn this example, we are trying to access an array element with an invalid index, so JVM will throw a RuntimeException. public class RuntimeExceptionExample {
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5};
int num = arr[10]; // This will throw RuntimeException
}
}
OutputFollowing is the output of the above code: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 10 out of bounds for length 5
at RuntimeExceptionExample.main(RuntimeExceptionExample.java:5)
Handling RuntimeExceptionIn this example, we are trying to divide a number by zero, so JVM will throw a RuntimeException. We are handling this exception using try-catch block. public class RuntimeExceptionExample {
public static void main(String[] args) {
try {
int num1 = 10;
int num2 = 0;
int result = num1 / num2; // This will throw RuntimeException
} catch (ArithmeticException e) {
System.out.println("Cannot divide by zero");
}
}
}
OutputFollowing is the output of the above code: Cannot divide by zero How to avoid RuntimeException?Following are the ways to avoid RuntimeException in Java:
ConclusionIn this chapter, we have learned about the RuntimeException class in Java. We have discussed its hierarchy, causes, constructors, methods, subclasses, and how to handle it using try-catch blocks. We have also seen some examples of RuntimeException and how to avoid it. java_lang_exceptions.htm Advertisements
| ||||||||||||||||||||||||||||||||
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |