Exception Handling In Java
Join me as we discuss the important step of handling exceptions in our Java program.
Table of contents
- Hey Users ๐๐พ
Hey Users ๐๐พ
Today we are going to be treating Exception Handling In Java.
One of the many constants of programming is errors. They're basically what keeps most programmers stuck in the office or front of their desk for hours that could seep into the late night. Errors can be frustrating for both the user and the programmer. Thankfully, Java provides some useful mechanisms for handling these errors and exceptions.
What are Exceptions and Errors?
An Exception: is any unexpected event or scenario that interrupts the natural flow of the program. In the planning stage, Developers have to generate countermeasures that could tackle these events. Exceptions could arise due to wrong inputs or logical failure in the program.
Errors: are faults that pose serious threats to the program. We cannot execute the program unless they are resolved. They also cause the program to behave abnormally. They are the nightmares of every programmer and user. They can also be referred to as " bugs ". Debugging is the removal of these bug friends. They are capable of collapsing an entire software if left uncared for.
What is Exception Handling?
Exception Handling is the mechanism used to handle the various exceptions that could occur in the program so as to maintain the normal flow of the program. It drives the compiler to the next statement by making it understand that the malfunction is just an unexpected event that can be ignored by sending the user a relevant message that explains the exception.
The Exception Hierarchy.
This is the hierarchical arrangement of the classes that handle exceptions in Java.
Throwable: This is the root class in the exception hierarchy that is derived from the Object class from which all exception classes are derived directly or indirectly. With the Throwable class, you can create your own custom exceptions.
Exceptions: This is a subclass of the Throwable class. It is also a superclass of all Exception classes i.e all Exception classes are derived directly or indirectly from this super class.
Errors: This is a subclass of the Throwable class that handles all errors. It is also the class that terminates the program once threatening errors are spotted.
TYPES OF EXCEPTIONS AND ERRORS:
We have two types of exceptions namely:
- Built-in Exceptions: These are exceptions that are readily available in Java libraries. Java supports Exception Handling by providing a number of predefined exceptions to handle all exception cases. This means that when an exception occurs, the Java Virtual Machine creates an object of the predefined exception class.
- The most simplest instance is the ArrayIndexOutOfBoundException;This Exception is thrown when you try to access an element in an array that is out of the specified index.
- User Defined Exceptions: Naturally, Java libraries come with many built-in exceptions but there are situations where its library may not be enough to handle the kind of exception you want to handle and you will have to define your own. Please note that a user defined exception has to be an inheritor of a throwable class i.e the child of a built-in exception class.
We also have three types of Errors, which are:
Syntax Errors: This type of error is found where the compiler finds a problem with your code as a result of the wrong syntax and so refuses to execute it. E.g an incorrect punctuation or a variable that hasn't been declared. Syntax Errors are found during compile time although the red squiggly lines are hints enough.
Runtime Errors: This type of error is detected while your program is running. When the error is discovered, it sends out an error message telling you the kind of error and a stack trace to help you debug.
Logic Errors: This type of error occurs when your program compiles and runs without any problem except for the fact that it's not doing what you programmed it to do. This is the trickiest type of errors because Java is oblivious to what you're trying to do and so is very unhelpful.
Side note: Logic Errors are the dark circles you see in developers.
Plot Twist: Runtime Errors are also called exceptions.
DIFFERENCES BETWEEN CHECKED AND UNCHECKED EXCEPTIONS
Exceptions can be classified into two kinds namely:
- CHECKED EXCEPTIONS: These are exceptions that are checked by the compiler itself and so is said to come up during compile time. The classes that directly inherit Throwable class except RuntimeException and Error are called checked exceptions.
- UNCHECKED EXCEPTIONS: These are exceptions that come up during runtime. They are not checked by the compiler because they occur during the runtime of a program. The classes that directly inherit RuntimeException are called unchecked exceptions.
DIFFERENCES BETWEEN ERRORS AND EXCEPTIONS
ERRORS:
- It is classified as an unchecked type. Only Syntax Errors are found during compile time while the rest occur during runtime.
- Until an error is solved, the program is irrecoverable.
- Class:java.lang.error.
- It occurs at runtime.
- It is caused by the environment on which the program is running like its programming language or the IDE.
EXCEPTIONS:
- It can be checked or unchecked.
- It is possible to recover the program even with an exception.
- Class:java.lang.exception
- Can occur at runtime or compile time.
- It is caused by the application or program itself.
EXCEPTION HANDLING METHODS
These are the various methods used to handle Exceptions. They're more like keywords used in handling exceptions. They define the syntax that Java uses to handle our exception friends.
Try: is used to specify the block of code that may throw an exception.
Catch: Specifies the block of code that handles the exception. It's the solution to the problem you highlighted with Try.
Finally: is used to specify the block of code that has to be executed regardless of whether the Exception is handled or not. It's like the final say.
Throw: is used to throw an exception inside a method.
Throws: is used to specify the exceptions that the method is likely to throw.
Author's Note: if you would like to know more about Exception Handling In Java
Unfortunately, I couldn't go in depth with user defined exceptions and I apologize.
Nevertheless, I hope you're leaving this article with a bucket of knowledge and a jar of understanding.
If you would like to subscribe to my newsletter, Please Click Here
Click Here for a special surprise,๐