14, abnormal

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/weixin_45536396/article/details/102736245

abnormal

First, the concept of exceptions

1. Concept: all abnormal phenomena that occur during program run collectively referred to as abnormal.

2. The need exception handling: Program inevitable there will be a lot of mistakes, problems that must be dealt with, no treatment will cause some damage to the user, it is necessary to deal with.

3. Abnormal classification

a) Throwable: all exceptions in the parent class java.lang package.

method

String getMessage (): Returns a string abnormal content information.

void printStackTrack (): print details of the exception tracking station.

b) Throwable

1. Error: error running program appear to rely on the program can not solve the problem

Such as: JVM memory shortage | memory stack overflow
Can not handle

2. Exception: Abnormal

. A RuntimeException: abnormal, abnormal operation unchecked

Not compile-time error, the compiler does not check, runtime error, for such an exception can be avoided, it can not handle processing
Abnormal direct runtime | indirectly inherited from RuntimeException
As long as inherited from RuntimeException are all abnormal operation

. B Non-RuntimeException: checked exceptions

The compiler checks will compile-time error for such an exception must be dealt with, otherwise the compiler is not passed.
There is no direct | indirect inherited RuntimeException, directly or indirectly, inheritance Exception are all checked exceptions

c) Common unchecked exceptions | runtime exception

1. NullPointerException NullPointerException

2. The array subscript bounds exception ArrayIndexOutOfBoundsException

3. Type translation exception ClassCastException

4. The digital format abnormality (abnormality data type conversions) a NumberFormatException

5. math exception ArithmeticException: / by zero

Second, there was an exception

1. Automatic abnormal

Encounters runtime error codes, automatically generate an exception, due to abnormal termination of the program is running.

Here Insert Picture Description

2. Manual abnormal

Syntax: throw new Exception class name ( "arguments");

Location: method, manually throw an exception

Equivalent to a return statement, the program terminates, the code behind less than execution

Here Insert Picture Description

Third, abnormal delivery

Reverse pass along the call chain method until passed to the main function, the main function is passed to the JVM, JVM may throw an exception generated.

Here Insert Picture Description

Fourth, exception handling

1. unusual statement (negative handling exceptions)

Syntax: throws exception name

Rearmost method declaration: Location

The only negative to handle exceptions, will only make the program compile, does not really solve the anomaly, the program will also be terminated because of an exception. Exceptions can accept all of a subclass by a parent.

Here Insert Picture Description

Definition syntax methods

Modifier return type method name (scale parameter) throws an exception name, exception names exception name 2 ... n {

// method to achieve

}

2. Capture abnormal (positive handling exceptions)

grammar

try{

// code may appear abnormal

} Catch (Exception caught e) {

// exception handling code

}

try{}

catch(){}

catch(){}

catch(){}

note

1. When the program is running abnormality occurs abnormality catch blocks one by one and an exception, will be captured

Matching, which matches a catch when the block will execute appropriate exception of exception handling.

2 may be used catch (Exception parent e) catch exceptions, the exception type can be captured all the parent class exception +

A subclass

3. If the parent class to capture exception, the exception is the parent class must be placed in the final capture of the catch block.

Five, finally

finally the code is the code that must be executed anyway

try{

// code may appear abnormal

} Catch (Exception caught e) {

// exception handling code

}finally{

// code must be executed

}

Note: finally in the code is the statement will be executed no matter what circumstances, if placed in the return statement finally, the return is finally in the return statement. The return statement is not finally placed in a general statement; the code is generally placed in the release of resources, detailed explanation will follow.

try-catch-finally common structure

try{}catch(){}catch(){}catch(){}

try{}catch(){}finally{}

try{}finally{}

try{try{}catch(){}}catch(){}

try{try{}catch(){}finally{}}catch(){}

try{try{}catch(){}}catch(){try{}catch(){}finally{}}finally{}

try{}catch(){}finally{try{}catch()finally{}}

note:

1. try block can not exist alone, must catch | finally block used in conjunction

2. If the try-catch-finally there must be finally on the last.

3. If the behind the try finally appeared, only appear a.

The difference between final-finally-finalize the

Fifth, the custom exception class

1. Write a class that inherits an existing exception class

Abnormal inherited RuntimeException if you want to make a run to become the exception class

If you want to make this exception class to become a checked exception inherited Exception

2. Providing two constructors

A no-argument constructor, a constructor with parameters of type String

3. With the exception of the need to provide directly throw new exception class name ( "arguments");

Sixth, the method of covering the requirements (full version)

Method name, parameter list, return the same value

Same access modifiers subclasses and superclasses or sub analogy wider parent

Subclass can not throw more exceptions than the wider parent

(The number can not simply come from the parent class and subclasses, as long as the scope of the exception subclass thrown exceptions same or narrower than the parent line)

3. With the exception of the need to provide directly throw new exception class name ( "arguments");

Sixth, the method of covering the requirements (full version)

Method name, parameter list, return the same value

Same access modifiers subclasses and superclasses or sub analogy wider parent

Subclass can not throw more exceptions than the wider parent

(The number can not simply come from the parent class and subclasses, as long as the scope of the exception subclass thrown exceptions same or narrower than the parent line)

Guess you like

Origin blog.csdn.net/weixin_45536396/article/details/102736245