
Compile-Time and Runtime Exceptions
In Java, exceptions are mainly divided into two types: Compile-Time Exceptions (Checked Exceptions) Runtime Exceptions (Unchecked Exceptions) What is a Compile-Time Exception? A Compile-Time Exception (also called a Checked Exception) is an error that is detected during compilation. The compiler checks these exceptions before the program runs. Examples: IOException SQLException FileNotFoundException Example Program: import java.io.* ; class Test { public static void main ( String [] args ) { FileReader file = new FileReader ( "test.txt" ); // Compile-time error } } Correct Way: import java.io.* ; class Test { public static void main ( String [] args ) { try { FileReader file = new FileReader ( "test.txt" ); } catch ( FileNotFoundException e ) { System . out . println ( "File not found" ); } } } What is a Runtime Exception? A Runtime Exception (also called an Unchecked Exception) occurs while the program is running. Examples: ArithmeticException NullPointerException ArrayIndexOutOfBound
Continue reading on Dev.to Tutorial
Opens in a new tab




