Posted On: May 10, 2023
An Exception is a run time error especially occurring at execution time.
Following are the two types of Exceptions in JAVA:
In Java, The exceptions that are checked during the compilation time are termed as Checked exceptions to verify that a method that is throwing an exception contains the code to handle the exception with the try-catch block or not. if there is no code to handle them, then the compiler checks whether the method is declared using the throws keyword. And, if the compiler finds neither of the two cases, then it gives a compilation error.
A checked exception extends the Exception class. For instance, if we write a program to read data from a file using a FileReader class and if the file does not exist, then there is a FileNotFoundException.
An exception that checks during the execution of a program is called an unchecked or a runtime exception. The unchecked exceptions occur when the user attempts to access an element with an invalid index, calling the method with illegal arguments, etc. Unchecked exceptions are checked during the runtime. Therefore, the compiler does not check whether the user program contains the code to handle them or not.
For instance, if a program attempts to divide a number by zero. Or, when there is an illegal arithmetic operation. Suppose, we declare an array of size 10 in a program, and try to access the 12th element of the array, or with a negative index like -5, then we get an ArrayIndexOutOfBounds exception.
Never Miss an Articles from us.