Built-in Exceptions in Java20 May 2025 | 7 min read Exceptions that are already available in Java libraries are referred to as built-in exception. These exceptions are able to define the error situation so that we can understand the reason of getting this error. Types of Built-in ExceptionsBuilt-in exceptions are of two types:
![]() Checked ExceptionsChecked exceptions are called compile-time exceptions because these exceptions are checked at compile-time by the compiler. The compiler ensures whether the programmer handles the exception or not. The programmer should have to handle the exception; otherwise, the system will show a compilation error. List of Checked Exceptions
Example of Checked ExceptionExampleOutput: Main.java:5: error: unreported exception FileNotFoundException; must be caught or declared to be thrown file_data = new FileInputStream("C:/Users/Desktop/Hello.txt"); ^ Main.java:7: error: unreported exception IOException; must be caught or declared to be thrown while(( m = file_data.read() ) != -1) { ^ Main.java:10: error: unreported exception IOException; must be caught or declared to be thrown file_data.close(); ^ 3 errors In the above code, we are trying to read the Hello.txt file to display its data on the console. When we execute the above program, we get the following exception:
Handling the Checked ExceptionsThere are basically two ways through which we can handle these exceptions.
Using the throws Keyword The exceptions occur in the main() method. We can get rid of these compilation errors by declaring the exception in the main() method using the throws keyword. We only declare the IOException, not the FileNotFoundException, because of the child-parent relationship. The IOException class is the parent class of the FileNotFoundException class, so IOException automatically handles this exception. We will declare the exception in the following way: ExampleWhen we compile and run the above code by handling the IOException using the throws keyword, we get the following output: Hello Java Using try-catch Block We can also handle this exception by using the try-catch block. However, the way we have used above is not correct. We have to give a meaningful message for each exception because it would be easy to understand the error. We will use the try-catch block in the following way: ExampleOutput: File Not Found! Exception in thread "main" java.lang.NullPointerException: Cannot invoke "java.io.FileInputStream.read()" because "<local1>" is null at Main.main(Main.java:12) We will see a proper error message, "File Not Found!" on the console because there is no Hello.txt file at that location. We get the following message on the console. Unchecked ExceptionsThe unchecked exceptions are just the opposite of the checked exceptions. It is also known as a run-time exception. The compiler will not check these exceptions at compile time. In simple words, if a program throws an unchecked exception, and even if we did not handle or declare it, the program would not give a compilation error. Usually, it occurs when the user provides bad data during the interaction with the program. Note: The RuntimeException class is able to resolve all the unchecked exceptions because of the child-parent relationship.List of Unchecked Exceptions
Example of Unchecked ExceptionsArithmeticException: Divide by Zero ExampleOutput: Runtime Error: Exception in thread "main" java.lang.ArithmeticException: / by zero at Main.main(Main.java:7) In the above program, we have divided 35 by 0. The code would be compiled successfully, but it will throw an ArithmeticException error at runtime. Dividing a number by 0 throws the divide by zero exception, which is an unchecked exception. Let's see another example of an unchecked exception. Example of ArrayIndexOutOfBoundsExceptionExampleOutput: Runtime Error: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 7 out of bounds for length 6 at Main.main(Main.java:6) In the above code, we are trying to get the element located at position 7, but the length of the array is 6. The code compiles successfully, but throws the ArrayIndexOutOfBoundsException at runtime. Handling Unchecked ExceptionsUnchecked exceptions in Java are runtime exceptions that aren't required to be caught or declared in the method signature. They typically indicate programming errors, like NullPointerException, ArrayIndexOutOfBoundsException, or IllegalArgumentException.
Differences Between Checked and Unchecked Exceptions
Bugs or errors that we do not want and restrict the normal execution of the programs are referred to as exceptions. Java Checked and Unchecked Exceptions MCQs1. Which of the following is a checked exception?
Answer: 1) Explanation: IOException is a checked exception and other three are unchecked exceptions. 2. Which of the following is an unchecked exception?
Answer: 2) Explanation: NumberFormatException is an unchecked exception and other three are checked exceptions. 3. Which of the following exceptions that are not required to be caught or declared in the method signature?
Answer: 3) Explanation: Unchecked exceptions in Java are runtime exceptions that are not required to be caught or declared in the method signature. They typically indicate programming errors, like NullPointerException, ArrayIndexOutOfBoundsException, or IllegalArgumentException. 4. How can we handle checked exceptions?
Answer: 4) Explanation: There are basically two ways through which we can handle checked exceptions: Using throws Keyword and try-catch block. 5. Which of the following class is the parent class for all the exceptions and errors?
Answer: 1) Explanation: In Java, Throwable is the parent class for all exceptions and errors. It has two direct subclasses: Exception: Represents recoverable conditions in the application. Checked exceptions extend this class. Error: Represents serious issues typically related to the JVM, like OutOfMemoryError. Next TopicBully algorithm in Java |
The Java has a built-in function called DoubleAdder.intValue() that, adhering to a narrowing primitive conversion, returns the sum() as an int. The class object's initial value is zero when it is formed. Syntax: public int intValue() Parameters: There are no parameters that may be given with this method. Return...
3 min read
The ability to call concrete methods of abstract classes is an important concept to understand when programming in Java. Abstract classes provide a way to define a standard set of behaviours and features that multiple classes can share without implementing them from scratch. These classes can...
5 min read
When a block is decorated or associated with the word static, it is called a static block. Static Block is known as the static clause. A static block can be used for the static initialization of a class. The code that is written inside the static...
4 min read
Agile software development has gained immense popularity in recent years due to its flexibility, customer-centric approach, and iterative development practices. Java, being one of the most widely used programming languages, aligns seamlessly with Agile methodologies. In this section, we will explore Agile principles, patterns, and practices...
4 min read
In Java, generics are majorly used for providing a method for the creation of classes and methods which are capable of working with any type of data including the type safety. When generics are utilized in Java, the type of an object is often checked during the...
9 min read
How to Run Java Program in eclipse In this section, we learn how to run a Java program in eclipse step by step. Step 1: Open Eclipse and click File > New > Java Project. Step 2: Provide the Project Name and click on the Finish button. Step 3: In...
1 min read
In Java, method chaining is the chain of methods being called one after another. It is the same as constructor chaining but the only difference is of method and constructor. In this section, we will discuss the method chaining in Java. Method Chaining Method chaining is a common...
2 min read
In the realm of database programming, handling large text data is a common requirement. Java, being one of the most widely used programming languages, provides various mechanisms to interact with databases. One such mechanism is the (Character Large Object), which is designed specifically to manage...
5 min read
In this section, we will learn about pancake sorting in Java. In pancake sorting, one has to sort the array by performing only one operation, and that operation is: flipArr(arr, j): Reverse the array arr from index 0 to j. Generally, in other sorting algorithms, the attempt is...
2 min read
The offset of the buffer's first element within its underlying array can be obtained by calling the arrayOffset() method of the java.nio.DoubleBuffer class. In other words, buffer position p corresponds to array index p + array Offset() if this buffer is supported by an array. We...
3 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India