Default Exception in JavaException Handling in Java is one of the effective means to handle runtime errors so that the regular flow of the application can be preserved. Java Exception Handling is a mechanism to handle runtime errors such as ClassNotFoundException, IOException, SQLException, RemoteException, etc. In Java, exception is an unwanted or unexpected event that occurs during the execution of a program, i.e. at run time, that disrupts the normal flow of the program's instructions. Exceptions can be caught and handled by the program. When an exception occurs within a method, it creates an object. The object is called the exception object. It contains information about the exception, such as the name and description of the exception and the state of the program when the exception occurred. Why an Exception Occurs?
Errors represent irrecoverable conditions such as Java virtual machine (JVM) running out of memory, memory leaks, stack overflow errors, library incompatibility, infinite recursion, etc. Errors are usually beyond the control of the programmer, and we should not try to handle errors. Exception HierarchyAll exception and error types are subclasses of the class Throwable, which is the base class of the hierarchy. One branch is headed by Exception. This class is used for exceptional conditions that user programs should catch. NullPointerException is an example of such an exception. Another branch, Error is used by the Java run-time system(JVM) to indicate errors having to do with the run-time environment itself(JRE). StackOverflowError is an example of such an error. Let us discuss the above-defined listed exception that is as follows: 1. Built-in Exceptions Built-in exceptions are the exceptions that are available in Java libraries. These exceptions are suitable to explain certain error situations. 2. Checked Exceptions Checked exceptions are called compile-time exceptions because these exceptions are checked at compile-time by the compiler. 3. Unchecked Exceptions The unchecked exceptions are just opposite to the checked exceptions. The compiler will not check these exceptions at compile time. In simple words, if a program throws an unchecked exception, and even if we didn't handle or declare it, the program would not give a compilation error. 4. User-Defined Exceptions Sometimes, the built-in exceptions in Java are not able to describe a certain situation. In such cases, users can also create exceptions, which are called user-defined Exceptions. The advantages of Exception Handling in Java are as follows:
Methods to Print the Exception Message1. printStackTrace() MethodThe method prints exception information in the format of the Name of the exception: description of the exception, stack trace. Exception.java Output: java.lang.ArithmeticException: / by zero at Exception.main(File.java:10) 2. toString() MethodThe toString() method prints exception information in the format of the Name of the exception: description of the exception. ToString.java Output: java.lang.ArithmeticException: / by zero 3. getMessage() MethodThe getMessage() method prints only the description of the exception. GetMessage.java Output: Zero Default Exception HandlingThe default exception handler is a method that is called when an exception is thrown but not caught by any of the exception handlers in the call stack. Whenever inside a method, if an exception has occurred, the method creates an Object known as an Exception Object and hands it off to the run-time system(JVM). The exception object contains the name and description of the exception and the current state of the program where the exception has occurred. Creating the Exception Object and handling it in the run-time system is called throwing an Exception. There might be a list of the methods that had been called to get to the method where an exception occurred. This ordered list of methods is called Call Stack. Now the following procedure will happen.
The default exception handler is defined in the ThreadGroup class and has the following signature public void uncaughtException(Thread t, Throwable e) The uncaughtException() method takes two parameters:
The default exception handler simply prints the exception class name, exception description, and stack trace to the console and then terminates the program abnormally. Here is the default exception example. DefaultException.java Output: Uncaught exception in thread Thread-0 Something went wrong! [Ljava.lang.StackTraceElement;@1d251891 Default exceptions in Java play a crucial role in exception handling but have both benefits and drawbacks to consider. Advantages of Default ExceptionSimplicity Uncaught exceptions automatically propagate up the call stack, requiring no explicit handling beyond the default behaviour. It can be helpful for simple programs or prototypes. Robustness Default exception handling prevents programs from silently ignoring potentially critical errors, forcing developers to address them at some point. Clear error reporting The default handler prints the stack trace, providing valuable information for debugging and identifying the source of the problem. Drawbacks Unpredictability Uncontrolled propagation can lead to unexpected program termination, making debugging more challenging. Limited information The default message of Exception in thread... provides basic information but lacks details about the specific error cause. Lack of customization Default handling offers no opportunity to tailor the response to different types of exceptions, potentially impacting user experience or data integrity. Next TopicHow to Install Multiple JDK's in Windows |
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