java core technology - exception

 

error and runtimeExcption are unchecked exceptions

The throws clause is used to throw unchecked exceptions. The contract defined by the throws clause is very strict - we can only throw checked exceptions declared in the throws clause. A method without a throws clause does not mean it No exceptions can be thrown: this simply means that checked exceptions cannot be thrown.

The rest are checked exceptions

A checked exception represents a situation where, although the situation is abnormal, its occurrence is predictable to a certain extent, and once it does occur, it can be handled in some way. For such an exception to be detected, it must be flagged that it exists, and it must be ensured that the caller of the method handles the exception in some way, or at least consciously chooses to ignore it.

An unchecked exception's runtime represents a situation where, by and large, it reflects an error in our program's runtime logic and cannot be recovered at runtime.

Build a new exception class

Such as:

public class NoSuchAttributeException extends Exception{

         public final String attrname;

         public NoSuchAttributeException(String name){

                   super(“No attribute named \””+name+”\”found”);

         }

}

When defining the idiom of an exception class, provide at least the following four forms or their variants of constructors to handle exception-specific data:

throw throws an exception

public void replaceValue(String name,Object newValue)throws NoSuchAttributeException{

}

Once an exception occurs, the action after the point of the exception will not occur, and the action that will occur at this time is either in the finally block or in the catch block that catches the exception.

Synchronous exceptions : The throw statement can generate synchronous exceptions that occur directly as a result of executing a particular instruction.

Asynchronous exceptions : can occur at any time, regardless of the instruction being executed. Asynchronous exceptions can only occur in two specific forms, the first is an internal error of the Java virtual machine, which is raised when executing the internal instructions of the Java virtual machine instead of the instructions of the program; the second mechanism is the use of deprecated The Thread.stop method, or the stopThread method in the Java Virtual Machine Tools interface that is not deprecated.

try、catch和finally

 

Reason for entering the final clause:

1) The try code ends normally

2) The try code executes a control flow statement such as return

3) The code executed in the try block threw an exception

 

Guess you like

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