Unchecked abnormalities and abnormal differences, java.lang.Exception and java.lang.RunTimeException

public class ExceptionTypeTest {    
    public void doSomething()throws ArithmeticException{    
        System.out.println();    
    }    
    public static void main(){    
        ExceptionTypeTest ett = new ExceptionTypeTest();    
        ett.doSomething();    
    }    
}    

Question 1: Can the above program compiles? And explain the reasons.
Answer: can compile. Analysis: As a general common sense, defined doSomething method is defined ArithmeticException abnormal, call the method in the main method in the inside. It should continue to be thrown or capture it. But ArithmeticException abnormality is inherited RuntimeException abnormal operation. java inside abnormal divided into two categories: checkedexception (abnormalities) and unchecked exception (not detected
unchecked exception), also called for unchecked exceptions RuntimeException (abnormal operation), for an exception, java compiler does not require you to run must it must continue to capture or throw, but checkedexception (abnormalities) require you to be on the inside or capture method or continue to throw.

problem 2: the above procedure will be changed ArithmeticException IOException can compile? And explain the reasons.
Answer: You can not compile. Analysis: IOException extends Exception belongs checked exception, must be processed, or must be captured or must be thrown

summary: java exception divided into two categories: checked exception (checked exception) and unchecked exception (unchecked exceptions), for unchecked exceptions also called RuntimeException (a runtime exception).
abnormal unchecked (unchecked exception) handling of several ways:
1, capture
2, continue to throw
3, does not deal
Abnormal examination (checked exception, in addition to RuntimeException, other exceptions are checked exception) handling of several ways:
1, continue to throw, negative way, has been thrown into the java virtual machine can handle
2 with a try .. .catch capture
attention for checked exceptions must be handled, or must be captured or must be thrown

************************************************************************************************************************************************

 

Exception handling (Exception)
1. Abnormal: program again abnormal events occurring during the run, it will interrupt the normal flow of instructions.
An exception occurred in the run-time program, the compiler problem is called a syntax error.

 

2. The exception handling mechanisms:
1) when the program is run again appeared abnormal, JVM automatically creates an exception object of that type. Meanwhile the exception object to the system is running. (Thrown)
2) when the system receives an operation exception object, then it will generate near abnormality code to find the corresponding treatment.
3) exception handling in two ways:
1. Capture and processing: display process (unreasonably) with try / catch, processing queries corresponding catch block after the runtime system acquisition, then the processing block in the vicinity of catch exception code in handle the exception.
2. Check whether there is an exception occurs in the method statement abnormal upward, upward statement to the superior query processing statement, if there is no upward statement, JVM interrupt running programs and processes. Statement outwardly with throws (reasonable approach)

 

3. abnormal Category:
java.lang.Throwable
| - Error Error: serious problems within the JVM. It can not be recovered. Programmers do not deal with.
| --Exception anomalies: a common problem. Reasonable process, the program can also return to the normal flow of execution. It requires the programmer to be processed.
| --RuntimeException: also called Unchecked exceptions (unchecked exception) Such exceptions are logical problems programmers. They should be held accountable. Java compiler does not perform mandatory treatment. In other words, such an exception then the program can be processed, can not handle.
| - Non-RuntimeException: also called Unchecked exceptions (checked exception) Such exceptions are generated by some external causal factors caused. Java compiler mandatory treatment. In other words, the program must be of such abnormal processing.

 

4. Common exceptions:
1) the non-subject: NullPointerException, ClassCastException, ArrayIndexsOutOfBoundsException, ArithmeticException ( arithmetic exceptions, overflow except 0)
2) Test: Exception, a FileNotFoundException, IOException, SQLException.

5. The exception handling two ways:
1 .) display processing (unreasonably) with the try / the catch
the try {
// code segment may appear abnormal;
} the catch (exception type 1 variable name) {// handle exception of the development of type
// the type of exception handling code segments;
e.printStackTrace ();
} the catch () {
} [the finally {
// whether the code segments to be executed abnormality occurs;
// often used to clean up resources
}]
. 2) upwardly declared: Categories with throws outward declaration (reasonable approach)
exception list statement this method may be thrown.
... method name (parameter list) throws Exception type 1, type 2 abnormal
upward statement tells the caller of this method, when using this method, these exceptions should be treated.


6. Manual throw an exception: when the program does not meet the desired logic to suspend execution of the code behind.
In the code section method, you can use the manual keyword throw throw an exception.
Note: If the subject is manually thrown exception, then the method must be treated (up exception statement should be used); if the subject is non-manually thrown exception, then may be processed, may not be processed.

 

7. custom exception: When you need some anomalies associated with a specific class information services.
You can inherit inherit Exception to define an abnormal subjects. Or it may be inherited from a subclass RuntimeException to define a non-subject exception.

8. exception summary: a two approaches FIG.

 

9. abnormal practice:
abnormal name and line number thrown observation is very important.
You should catch and handle those exceptions known how to deal with, and I do not know how to handle the transfer of those exceptions.
Minimize the volume of the try block.
for (int I =. 1; I <= 1000; I ++) {
the try {
...
} the catch (...)
} unreasonable
minimize nested try-catch statement.
In the catch block code segments, it should be printed in addition to the exception stack trace information to facilitate debugging.

 

 

Use keyword to declare method throws exception may be thrown
throws methods may throw can declare one or more exceptions, with the exception among ',' spaced
if statement may be the subject of non-exception thrown, the caller of the method of treatment may not be processed.
If the statement might throw exceptions are the subjects of the caller of the method must be treated.

With the throw keyword to manually throw an exception
if an exception is thrown by a manually examined exception, it must be dealt
treatment in two ways:
1. Display processing (unreasonable) with the try / the catch
2. throws with statement outwardly (reasonable approach)

You should catch and handle those exceptions known how to deal with, and pass those exceptions do not know how to handle.

 

Transfer: https://yangshen998.iteye.com/blog/1311682

Guess you like

Origin blog.csdn.net/zp357252539/article/details/89395173