JAVA Basics PATH(4) - Exception

Abnormal: just abnormal. An abnormal condition that occurs when the program is running. In fact, it is a problem in the program. This problem is described in accordance with object-oriented thinking and encapsulated into objects. There are multiple attribute information such as the cause of the problem, the name of the problem, the description of the problem, etc. When there is multi-attribute information, the most convenient way is to encapsulate the information. Exception is that java encapsulates the problem according to the object-oriented idea. This makes it easier to operate and deal with problems.

 

There are many kinds of problems, such as angle mark out of bounds, null pointer, etc. categorize these issues. And these questions have common content, such as: each question has a name, as well as information about the problem description, and the location of the problem, so it can be continuously extracted upwards. An anomalous system was formed .

 

--------java.lang.Throwable:

Throwable: Throwable .

|--Error: Error, under normal circumstances, do not write targeted code for processing, usually occurs in jvm, and the program needs to be corrected.

|--Exception: Exception, which can be handled in a targeted manner

 

Whether it is an error or an exception, they all have specific subclasses to reflect each problem, and their subclasses have one thing in common, that is, the parent class name is used as the suffix name of the subclass .

 

All classes and objects in this system have a unique characteristic; that is throwability .

The embodiment of throwability: that is, the classes and objects in this system can be manipulated by the keywords throws and throw.

------------------------------------------------------

class  ExceptionDemo{

public static void main(String[] args) {

//byte[] buf = new byte[1024*1024*700]; //java.lang.OutOfMemoryError memory overflow error

}

}

------------------------------------------------------

During development, if you find some problems with this function when you define a function, you should mark the problem when you define the function , so that the caller can give the processing method in advance when using this function.

 

How to mark it? Completed by the throws keyword, the format: throws exception class name, exception class name...

After being marked in this way, the caller must process it when using this function, otherwise the compilation will fail.

 

There are two processing methods: 1. Catch; 2. Throw.

For capture: java targeted statement block for processing.

try {

The code that needs to be checked;

}

catch(Exception class variable name) {

exception handling code;

}

fianlly{

code that must be executed;

}

--------------------------------------------------------

catch (Exception e) { //e is used to receive the exception object detected by try.

System.out.println("message:"+e.getMessage());//Get the exception information.

System.out.println("toString:"+e.toString());//The name of the exception + the information of the exception is obtained.

e.printStackTrace();//Print exception information on the stack; exception name + exception information + exception location.

}

---------------------------------------------------------

Exception handling principle: The function throws several exceptions. If the function call is processed by try, the corresponding catch processing code block is required. This kind of processing is targeted, and only a few are thrown.

 

Special case: When try corresponds to multiple catches, if there is a catch statement block of the parent class, it must be placed below.

 

The difference between throw and throws keywords:

throw is used to throw an exception object, followed by the exception object; throw is used inside a function.

throws is used to throw an exception class, followed by the exception class name, which can be followed by more than one, separated by commas. throws are used on functions.

 

Usually: if there is a throw in the function content, an exception object is thrown, and no processing is performed, then the function must be declared, otherwise the compilation will fail. But there are also special cases.

 

There are two types of exceptions:

1: Exceptions that are checked at compile time, as long as Exception and its subclasses are exceptions that are checked at compile time.

2: Runtime exception, in which Exception has a special subclass RuntimeException, and the subclass of RuntimeException is a runtime exception, which means that this exception is an exception that is not checked at compile time.

 

The difference between compile-time checked exceptions and runtime exceptions:

Compilation checked exceptions are thrown inside the function, the function must be declared, otherwise the compilation fails.

The reason for the declaration: is that the caller needs to handle the exception.

A runtime exception that is thrown inside a function does not need to be declared on the function.

Reason for not declaring: The caller does not need to deal with it, the runtime exception occurs, and the program can no longer continue to run. Therefore, if the call is not allowed to be processed, the program is stopped directly, and the code is corrected by the caller.

 

When defining exception handling, when to define try and when to define throws?

If there is an exception inside the function, if it can be handled internally, use try;

If the function cannot handle it internally, it must be declared and let the caller handle it.

 

Custom exception: When a problem that is not defined in java occurs in the project during development, we need to follow the idea of ​​java exception establishment and encapsulate the unique problems in the project. This exception is called a custom exception.

 

For division operations, 0 is not allowed as a divisor. ArithmeticException class is used to describe this kind of problem in java. For this function, in our project, the divisor cannot be negative except that it cannot be 0. But the negative part of java is not for the description. So we need to customize this exception.

 

Steps to customize exception:

1: Define a subclass to inherit Exception or RuntimeException to make the class throwable.

2: Operate through throw or throws.


When the exception occurs, when the sub-parent class is overwritten, there are some new features:

1: When a subclass overrides the method of the parent class, if the method of the parent class throws an exception, then the method of the subclass either does not throw an exception or throws an exception of the parent class or a subclass of the exception, and cannot throw other exceptions .

2: If the parent class throws multiple exceptions, then the child class can only throw a subset of the parent class's exceptions when overriding.

 

Notice:

If the method in the parent class or interface has not thrown an exception, then the subclass cannot throw an exception. If an exception occurs in the overridden method of the subclass, it can only try but not throw.

If this exception subclass cannot be handled, it has affected the specific operation of the subclass method. At this time, the RuntimeException exception or its subclass can be thrown through throw in the subclass method. In this way, the method of the subclass does not need the throws declaration. of.

 

Common exceptions:

1. IndexOutOfBoundsException includes arrays and strings;

NullPointerException

2. Type conversion exception: ClassCastException

3. No this element exception: NullPointerException

4. Abnormal operation is not supported;

Exceptions should be avoided as much as possible. If they cannot be avoided, the handling methods should be given in advance. Such as household medicines, such as fire extinguishers.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324733313&siteId=291194637
Recommended