The Java language has many features that allow for exceptions to be thrown when certain conditions are met. This tutorial will cover the basics of how these work, but also show two common patterns in exception handling.
This tutorial covers the basics of exceptions in Java. You will learn about what an exception is, how they are created and handled, and when to use them. We’ll show you a few code samples for each topic introducing all different types of Java exceptions including IOException, ArithmeticException, ClassNotFoundException
An exception is an error in a computer program that occurs during the execution of the program. The most common exceptions are caused by errors in the code, such as divide by zero or out of memory.
This article will teach you how to:
- Describe how the notion of exceptions works.
- Recognize the many kinds of exceptions.
Exceptions and their Causes
An exception is a condition that occurs as a result of a unique circumstance that disrupts the program’s instructions. Exceptions are used to report errors in a program. Exceptions may arise in programs for any of the following reasons:
These exceptions occur as a result of API problems, such as NullPointerException. These errors are unavoidable for programs that use these APIs.
Errors in client code
The client code tries things like reading the contents of a file without opening it. This will result in an exception being raised. The client code will handle the exception, which will give details about the error’s cause.
Errors that are beyond the program’s control
Certain exceptions, such as memory errors or network connection failure errors, are not intended to be detected by the client code. These are errors that the run-time environment has thrown forth.
Exceptions are classified in a variety of ways.
There are two kinds of exceptions in Java, which are as follows:
Exceptions that have been checked
Checked exceptions are thrown when anything unexpected happens throughout the course of a program’s execution. Requesting missing files, incorrect user input, and network problems are all instances of checked exceptions. To prevent compile-time problems, these exceptions should be handled. If an exception occurs while a method is being executed, the method may either manage the exception or throw it back to the calling method to indicate that a problem has happened. The exception may be handled by the calling method again, or it can be thrown to the calling method. This procedure may be repeated until the exception reaches the top of a thread, at which point the thread will be terminated. The Call-Stack mechanism is what it’s called. The primary benefit of this approach is that it allows a developer to place error handling code wherever he wants.
Exceptions of this kind are descended from the Exception class. The software either handles the checked exception or pushes it higher on the stack. It is verified throughout the compilation process.
Exceptions that haven’t been checked
Unchecked exceptions are thrown when a program is unable to recover from a condition. Attempting to access an element beyond an array’s maximum length is a common example of this problem. It is not necessary for an application to check for these types of exceptions. Unchecked exceptions include runtime exceptions as well. They’re usually caused by logical flaws. Errors are unchecked exceptions that occur as a result of environmental issues or errors and are difficult to recover from. A frequent error is running out of a program’s allotted memory.
The RuntimeException class is inherited from the Exception class, which is derived from the Exception class. Unchecked exceptions do not need to be handled by the program. It is created during the program’s execution.
The exception hierarchy in Java is shown in the diagram below.
When a method fails during runtime, an object is generated that contains information about the error, its kind, and the program’s current state. Exception object is the name given to this object. Exception objects are instances of classes that inherit from the Throwable base class.
Checked Exceptions Come in a Variety of Forms
The Exception class is the root of all checked exceptions. Some of the most frequent Checked exceptions are shown in the table below.
|InstantiationException||If you try to make an instance of the abstract class, you’ll get this exception.|
|InterruptedException||When a thread is interrupted, an exception occurs.|
|NoSuchMethodException||If the Java Virtual Machine is unable to determine which method should be called, an exception is thrown.|
|RuntimeException||If an error occurs during regular functioning of the Java Virtual Machine, this exception will be raised.|
Unchecked Exceptions Come in a Variety of Forms
All unchecked exceptions are descended from the RuntimeException class, either directly or indirectly.
Some of the most frequent Unchecked exceptions are included in the table below.
|ArithmeticException||The ArithmeticErrorException is derived from RuntimeException and signals an ArithmeticError situation.|
|ArrayIndexOutOfBoundsException||The IndexOutOfBoundsException class is derived from the IndexOutOfBoundsException class and is thrown when an array index is less than zero or larger than the array’s actual size.|
|IllegalArgumentException||RuntimeException is the source of this class. Method is confronted with an illegitimate argument.|
|NegativeArraySizeException||RuntimeException is the source of this class. The size of the array is smaller than zero.|
|NullPointerException||RuntimeException is the source of this class. Attempt to access a member of a null object.|
|NumberFormatException||IllegalArgumentException is the source of this exception. The string could not be converted to a number.|
|StringIndexOutOfBoundsException||IndexOutOfBoundsException is the ancestor of this exception. The string’s index is negative or larger than the string’s length.|
An “exception” is a special kind of error that occurs when something unexpected happens. Java has many different types of exceptions, and they’re all defined in the java.lang package. Reference: checked exception in java.
Frequently Asked Questions
What are exceptions in Java?
A: In Java, some exceptions are as follows
How do you introduce an exception in Java?
A: You can use the keyword throws when defining an exception. For example, if you wanted to make a method that throws an exception when something bad happens, you would write it like this:
What is exception in Java with example?
A: An exception can be created when the computer has encountered an error that keeps it from continuing. This is similar to a stop sign on the road, which means something must have happened for this to happen. The most common errors are programming mistakes or runtime exceptions, such as trying to access an invalid memory location or not having enough available memory in your programs current state.
- types of exception in java
- exception handling in java example
- custom exception in java
- exception class in java
- java exceptions list