Home > Java > javaTutorial > body text

What are the exception types in Java?

藏色散人
Release: 2019-01-19 10:26:03
Original
8280 people have browsed it

Errors are the bane of users and programmers. Developers obviously don't want their programs to fall off at every turn, and users are now so used to having bugs in their programs that they reluctantly accept to pay the price for software that is almost certain to have at least one bug.

What are the exception types in Java?

#Java is designed to give programmers the opportunity to design a bug-free application. When an application interacts with resources or users, the programmer may be aware of exceptions that can be handled. Unfortunately, there are exceptions that the programmer has no control over or simply ignores. In short, not all exceptions are the same, so programmers need to consider several types.

Exceptions are events that cause a program to fail to function in its intended execution. There are three types of exceptions - checked exceptions, errors and runtime exceptions.

The Checked Exception (checked exception)

A checked exception is an exception that a Java application should be able to handle. For example, if the application reads data from a file, it should be able to handle FileNotFoundException. After all, there's no guarantee that the expected file will appear where it's supposed to. Anything can happen on the file system and the application knows nothing about it.

Let's take a closer look at this example. Suppose we use the FileReader class to read character files. If you take a look at the FileReader constructor definition in the Java api, you will find its method signature:

public FileReader(String fileName)
throws FileNotFoundException
Copy after login

As you can see, the constructor explicitly states that the FileReader constructor can throw FileNotFoundException. This makes sense since the filename string is likely to be wrong from time to time. Look at the following code:

public static void main(String[] args){
FileReader fileInput = null;
//打开输入文件
fileInput = new FileReader("Untitled.txt");
}
Copy after login

Syntactically, these statements are correct, but this code will never compile. The compiler knows that the FileReader constructor can throw FileNotFoundException, and it is up to the calling code to handle this exception. There are two options - first, we can pass the exception by specifying a throw clause:

public static void main(String[] args) throws FileNotFoundException{
FileReader fileInput = null;
//打开输入文件
fileInput = new FileReader("Untitled.txt");
}
Copy after login

Or we can handle the exception:

public static void main(String[] args){
FileReader fileInput = null;
try
{
//打开输入文件
fileInput = new FileReader("Untitled.txt");
}
catch(FileNotFoundException ex)
{
//告诉用户去找文件
}
}
Copy after login

A well-written Java application should be able to handle the check Extremely abnormal.

Errors

The second type of exception is called an error. When an exception occurs, the JVM will create an exception object. These objects are derived from throwable classes. The throwable class has two main subclasses - Error and Exception. The Error class represents exceptions that are unlikely to be handled by the application.

These exceptions are considered rare. For example, a JVM may run out of resources because the hardware cannot handle all the processes it has to handle. Applications can catch errors and notify the user, but typically the application must shut down until the underlying problem is dealt with.

Runtime Exceptions

Runtime exceptions occur simply because the programmer made a mistake. You've written the code and everything will look fine to the compiler. When you run the code, it will crash because it tried to access an array element that doesn't exist or a logic error caused a method to be called with a null value. Or any number of mistakes a programmer might make. But that’s okay, we find these exceptions through exhaustive testing, right?

Errors and runtime exceptions fall under the category of unchecked exceptions.

The above is the detailed content of What are the exception types in Java?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template