
Compile-Time vs Run-Time Exceptions in Java
What is a Compile-Time Exception? A compile-time exception (also called a checked exception) is an error that occurs before the program runs, during compilation. The Java compiler checks these errors and forces the programmer to handle them using try-catch or the throws keyword. If not handled, the program will not compile. Examples: --> IOException --> FileNotFoundException --> ClassNotFoundException import java.io.* ; public class Main { public static void main ( String [] args ) throws IOException { FileReader file = new FileReader ( "file.txt" ); } } What is a Run-Time Exception? A run-time exception (also called an unchecked exception) occurs while the program is running. These are not checked by the compiler because they usually happen due to logical errors in the code. Examples: --> ArithmeticException (divide by zero) --> NullPointerException --> ArrayIndexOutOfBoundsException public class Main { public static void main ( String [] args ) { int a = 10 / 0 ; // runtime exception
Continue reading on Dev.to
Opens in a new tab




