Checked Exception Runtime Exception Error Exception

Checked Exceptions

It is normally a user error or problems which are not seen by the developer are termed as checked exceptions. Some Checked exception examples are:

FileNotFoundException: This is a checked exception (where it tries to find a file when the file is not found on the disk). IO Exception: This is also checked exception if there is any exception occurred during reading or writing of a file then the IO exception is raised. SQLException: This is also a checked exception when the file is connected with SQL database, and there is issue with the connectivity of the SQL database then SQLException is raised

Runtime Exceptions

Runtime exceptions are the one which could have avoided by the programmer. They are ignored at the time of compilation. Some Runtime exception examples are:

ArrayIndexOutOfBoundsException: This is a runtime exception when array size exceeds the elements. ArithmeticException: This is also a runtime exception when there are any mathematical operations, which are not permitted under normal conditions, for example, dividing a number by 0 will give an exception. NullPointer Exception: This is also a runtime exception which is raised when a variable or an object is null when we try to access the same. This is a very common exception.

Errors: The problem arises due to the control of the user or programmer. If stack overflows, then error can occur. Some examples of the error are listed below:

Error: This error is a subclass of throwable which indicates serious problems that an application cannot catch. Instantiation Error: This error occurs when we try to instantiate an object, and it fails to do that. Internal Error: This error occurs when there is an error occurred from JVM i.e. Java Virtual Machine.

Error Exceptions

It is an instance of the throwable class, and it is used in error pages. Some methods of throwable class are:

Public String getMessage() – returns the message of the exception. Public throwablegetCause() – returns cause of the exception Public printStackTrace()– returns the stacktrace of the exception.

How to Handle Exception in JSP

Here is an example of how to handle exception in JSP: Exception_example.jsp Guru_error.jsp Explanation of the code: Exception_example.jsp Code Line 1: Here we are setting error page to guru_error.jsp which will be used when the error will be redirected. Code Line 15: we are taking a variable num and setting it to 10 and checking a condition if num is 10 then to throw a Runtime Exception with the message as Error Condition. Guru_error.jsp Code Line 1: Here we are setting isErrorPageattribute to true. Code Line 12: The exception has been raised in exception_example.jsp using throw object and that exception will be shown here as IsErrorPage attribute is marked as true. Using the exception (this is an object which allows the exception data to be accessed by the JSP.) object we are trying to print the stacktrace of the error which was occurred in exception_example.jsp. When you execute the above code you get the following output:

Output: The exception has been raised which was thrown from exception_example.jsp using throw object of runtime exception and we get the above code. Also guru_error.jsp is called from which Guru Exception has occurred from this file.

Summary

Exceptions in JSP occur when there is an error in the code either by the developer or internal error from the system. Exceptions in JSP are of 3 types: Checked Exceptions, Runtime Exceptions, and Error Exceptions Checked exception is normally a user error or problems which are not seen by the developer are termed as checked exceptions. Runtime exceptions are the one which could have avoided by the programmer. They are ignored at the time of compilation. Error exception is an instance of the throwable class, and it is used in error pages.