Which of the following exceptions might be thrown by the methods of the class String
IntroductionAn exception is an error event that can happen during the execution of a program and disrupts its normal flow. Java provides a robust and object-oriented way to handle exception scenarios known as Java Exception Handling. Show
Exceptions in Java can arise from different kinds of situations such as wrong data entered by the user, hardware failure, network connection failure, or a database server that is down. The code that specifies what to do in specific exception scenarios is called exception handling. Throwing and Catching ExceptionsJava creates an exception object when an error occurs while executing a statement. The exception object contains a lot of debugging information such as method hierarchy, line number where the exception occurred, and type of exception. If an exception occurs in a method, the process of creating the exception object and handing it over to the runtime environment is called “throwing the exception”. The normal flow of the program halts and the Java Runtime Environment (JRE) tries to find the handler for the exception. Exception Handler is the block of code that can process the exception object.
So if the method’s call stack is If an appropriate exception handler is found, the exception object is passed to the handler to process it. The handler is said to be “catching the exception”. If there is no appropriate exception handler, found then the program terminates and prints information about the exception to the console. Java Exception handling framework is used to handle runtime errors only. The compile-time errors have to be fixed by the developer writing the code else the program won’t execute. Java Exception Handling KeywordsJava provides specific keywords for exception handling purposes.
An Exception Handling ExampleExceptionHandling.java
The This code will output the following:
Some important points to note:
Java Exception HierarchyAs stated earlier, when an exception is raised an exception object is getting created. Java Exceptions are
hierarchical and inheritance is used to categorize different types of exceptions.
Some useful methods of Exception ClassesJava Some of the useful methods of the
Java 7 Automatic Resource Management and Catch block improvementsIf you are
There are some constraints such as the exception object is final and we can’t modify it inside the Most of the time, we use the
A Custom Exception Class ExampleJava provides a lot of exception classes for us to use, but sometimes we may need to create our own custom exception classes. For example, to notify the caller about a specific type of exception with the appropriate message. We can have custom fields for tracking, such as error codes. For example, let’s say we write a method to process only text files, so we can provide the caller with the appropriate error code when some other type of file is sent as input. First, create MyException.java
Then, create a CustomExceptionExample.java
We can have a separate method to process different types of error codes that we get from different methods. Some of them get consumed because we might not want to notify the user of that, or some of them we will throwback to notify the user of the problem. Here I am extending This was a design decision. Using Checked Best Practices for Exception Handling in Java
While debugging we will have to look out at the stack trace carefully to identify the actual location of exception. If we change our implementation logic to check for these exceptions early as below:
Then the exception stack trace will be indicate where the exception has occurred with clear message:
ConclusionIn this article, you learned about exception handling in Java. You learned about |