I heard that wechat search “Java fish” will change strong oh!

This article is posted on Github and Gitee, and contains my entire Java series of articles for study and interview purposes

(a) Exceptions in Java

Exceptions in Java all come from the java.lang.Throwable class. From the types of exceptions, exceptions in Java can be divided into Exception and Error. Exception can be handled by the program itself, Error cannot be handled by the program.

Exceptions can be divided into checked exceptions and unchecked exceptions. The so-called checked exceptions refer to those exceptions that need to be tried/caught or throws during programming. Unchecked exceptions refer to exceptions that do not need to be explicitly caught by code during programming. I have drawn a diagram to show the types of exceptions:

(2) How to handle exceptions

Exceptions of the Error type are exceptions that should be avoided rather than handled in code, so we’ll focus on exceptions in the Exception Handling section.

Unchecked exceptions are often the result of programmer code logic negligence, such as null pointer exceptions, only need to determine whether the object is empty before calling can be avoided; Subscript out-of-bounds exceptions can be avoided by ensuring that access is always within the subscript range.

Check exceptions must pass a try/catch or throw in code to pass compilation, such as the following code:

public class ExceptionTest{
    public void readFile(a) throws FileNotFoundException {
        FileInputStream fileInputStream=new FileInputStream("xxx.text"); }}Copy the code

When a new FileInputStream(“xxx.text”) is used to fetch a file, a FileNotFoundException is a checked exception and must be thrown or caught. In addition to throws, try/catch can also be used

public class ExceptionTest{
    public void readFile(a) {
        try {
            FileInputStream fileInputStream=new FileInputStream("xxx.text");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }finally{}}}Copy the code

(3) The difference between try/catch and throws

There are two methods to catch checked exceptions: try/catch and throws. The difference is that a try/catch catches an exception and processes it directly inside the catch block. Throws an exception to its upper level for processing, and the program is never executed further down.

Once an exception is caught using a catch, it can be handled, usually by printing it out or writing it to a log. The following are common methods in the Throwable class.

public String getMessage(a) // Return brief information about the exception
public String getLocalizedMessage(a) // Return the localization message of the exception. The default is the same as getMessage. Subclasses override this method to add localization messages
public String toString(a) // Return more details about the exception
public void printStackTrace(a) // Output exception information on the console.
Copy the code

A try/catch is usually followed by a finally block. The code in the finally block is executed at the end of the program’s processing, regardless of whether an exception is entered. If there is a return statement ina code block, the code ina finally statement block is executed before the return is executed. The return value of the finally statement will override the original return value.

(4) Custom exceptions

In addition to using Java’s own exceptions, we can also define some of our own exceptions. It is easy to customize exceptions by inheriting Exception or RuntimeException and returning Exception information via super through the constructor.

public class MyException extends Exception{
    public MyException(a){
        super(a); }public MyException(String message){
        super(message);
    }
    public MyException(String message,Throwable cause){
        super(message,cause);
    }
    public MyException(Throwable cause){
        super(cause); }}Copy the code

(5) Summary

No matter how powerful your code is, there are exceptions. Properly handling exceptions can make your code more stable and easier to troubleshoot. I’m fish boy, and I’ll see you next time.