13
DecCyber Monday Deal : Flat 30% OFF! + free self-paced courses - SCHEDULE CALL
Java was developed by Sun Microsystems in 1995 and is considered as a high-end programming language which can operate on many platforms like the Windows, Mac OS and even different versions of UNIX. Java SE 8, is considered as the latest version of Java Standard Edition. Many configurations were built in Java to cater to different platforms due to its increasing popularity. It was guaranteed to be Write Once, Run Anywhere. It is endowed with many advantages like bring simple, secure, portable, robust, dynamic, multithreaded, etc.
An exception in Java is basically an unexpected or an exceptional event which has occurred during the execution of a program, i.e., during run time and often leads to disruption of the program. It results in abnormal termination of the application, which is of course not recommended, and thus it becomes imperative to take care of the exceptions in time. These can occur due to many reasons. Some of these are:
It is apparent that some of these errors are caused by the user while others are programming errors and some by the failure of some of the physical resources due to some mechanical issue. Thus, the exceptions can then be classified into three different categories:
Common Terminology
The reasons why the exceptions are used are as follows:
All the exceptions in Java, along with the Errors, are part of the Throwable class. Throwable is considered as the elementary class of the hierarchy. Under Throwable, there are two branches one led by Exceptions and the other led by Errors. The exception is used for some of the exceptional conditions which should be caught by the user programs; for instance, the NullPointerException is one such example. The Error is used by the JVM or the Java run-time system for indicating errors which have to do with the run-time environment of Java or the JRE, for instance, StackOverflow Error is an example of the error.
Read: Top 105 Angular Interview Questions and Answers In 2023
Default Handling: If an exception has happened inside a method, the method itself leads to the creation of an Object known as the Exception Object and gives it next to the run-time system. The object has both the name and the description of the exception and also the current state of the program where the exception has taken place. This is what is precisely referred to as the throwing of the exception when the exception object is created and handed over to the run-time system. There can be many lists of the methods where the exception has taken place. This list is called the Call Stack. The process of the same is as follows:
Customized Handling: Herein, the Java Exception handling is done by five keywords: try, catch, throw, throws, and finally. All the program statements which can be thought about can actually give rise to exceptions which are contained in the try block. If the exception takes place in the try block, it is basically thrown. The exception can be caught by your code and even handle it rationally by making use of the catch block. Many system-generated exceptions are often thrown by the Java run-time system. In case you want to throw the exception manually, you have to use the keyword throw. Any exception which is thrown out of a method has to be marked by a throws clause. Also, any code which is must be executed after the completion of the try block has to put in the finally block.
Here is a list of various methods which are available in the Throwable Class:
public String getMessage():
This returns a laid out message about the exception which has taken place. This message is generated in the Throwable constructor.public Throwable getClause():
This returns the cause of the exception as shown by the Throwable object.public String toString():
This returns the name of the class, which is concatenated with the result of getMessage().public void printStackTrace():
This actually prints the result of toString() to System.err along with the stack trace. System.err is the error output system.public StackTraceElement [] getStackTrace():
This returns an array which has every element on the stack trace. The element which is at index 0 it stands for the top of the call stack, and the ultimate element in the array which represents the method which is at the bottom of the call stack.public Throwable fillInStackTrace():
This is used in filling the stack trace of this Throwable object with the current stack trace, thereby adding to any of the previous information in the stack trace.An exception is caught by a method by using a mixture of the try and catch keywords. A typical try/catch block is kept around the code which has to give rise to an exception. Code which is in the try/catch block is known as the protected code and has a particular syntax.
try {
//Protected code
} catch (ExceptionName e1) {
//Catch block
}
The code which is vulnerable to exceptions is kept in the try block. When the exception takes place, it is handled by the associated catch block. Every try block has to be immediately followed by either a catch block or a finally block. A catch statement comprises declaration of the type of exception which you are trying to catch. If on the other hand, an exception occurs in the protected code, the catch block (or blocks) which come after try is checked. If the type of exception which has taken place is mentioned in the catch block, the exception is then given to the catch block just like the argument is passed to the method parameter.
Read: Java Access Modifiers - Public, Private, Protected & Default
As mentioned above, a try block can have many catch blocks following it, and the syntax for the multiple catch blocks is as follows:
try {
//Protected code
} catch (Exceptiontype1 e1) {
//Catch block
} catch (Exceptiontype2 e2) {
//Catch block
} catch (Exceptiontype3 e3) {
//Catch block
}
There can be any number of catch blocks after the try block. In case the exception has taken place in the protected code, it is thrown into the first catch block which appears on the list. If the data type of the so-called Exception which is thrown is the same as the ExceptionType1, then it is caught there itself. If it is not the case, then the exception has to pass down to the next catch statement. This process is continued until the exception is caught or falls through all the catches. In the latter case, the present method ends the execution and the exception is pushed down to the last method in the call stack.
In case of a method, where the checked exception is not handled, the method has to declare the same by making use of a throws keyword. The latter usually comes at the end of the signature of the method. The exception can be thrown, either a recently instantiated one or the one which has just been caught by making use of the throw keyword. The difference between the throws and the throw keywords can be stated as follows:
The throws are employed for postponing the handling of a checked exception and throw is used for invoking an exception explicitly.
This block comes after a try block or even a catch block, and it always performs execution even if an exception has occurred. The finally block lets you perform many cleanup-type statements which you have to execute irrespective of the fate of the protected code. The syntax of the final block is as follows:
try {
//Protected code
} catch (Exceptiontype1 e1) {
//Catch block
} catch (Exceptiontype2 e2) {
//Catch block
} catch (Exceptiontype3 e3) {
//Catch block
} finally {
//The finally block always executes.
}
Users can also create their own exceptions in Java. There are some points to be considered when doing so. These are as follows:
Read: What is Inheritance in Java? Different Types of Inheritance in Java
The Exception class can be defined as follows:
Class MyException extends Exception {
}
The predefined Exception class has to be extended for creation of own exception by the users. All these are seen as the checked exceptions.
Two categories of Exceptions and Errors are defined in Java:
The software world is highly prone to errors caused by many factors like a void user input, a lack of response by an external system or even can be a simple programming error. Whatever the reason, any error which occurs during run-time has to be handled by the application else the whole thing simply crashes and cannot process any more requests. Java has a robust platform which allows handling of all such exceptional events where they basically take place in some of the higher methods of the call stack. You can keep yourself abreast with more of such insightful topics by referring to our website www.janbasktraining.com.
A dynamic, highly professional, and a global online training course provider committed to propelling the next generation of technology learners with a whole new way of training experience.
Cyber Security
QA
Salesforce
Business Analyst
MS SQL Server
Data Science
DevOps
Hadoop
Python
Artificial Intelligence
Machine Learning
Tableau
Search Posts
Related Posts
Hibernate Interview Questions And Answers 184.5k
How to Create Object in Java with Examples? 553.7k
What is A Java Constructor? Type of Constructors & Uses in Java 457.6k
Top Java Developer skills To Have For Better Employment Opportunities 996.1k
React JS Interview Questions - Ace Your React Interviews in 2024 204.9k
Receive Latest Materials and Offers on Java Course
Interviews