1. Exceptions:
A. Issues (Throwable): Exceptions (Exception), errors (Error)
Exception (Exception): Run-time exception (runtimeexception), compile-time exception (unhandled Exception)
B. Compile-time exception: The compiler tells us at compile time that we compile the code, in the compilation it is possible to have one or more exceptions, need us to handle, otherwise the compilation does not pass, the manifestation is:
Call method at the red line, referring to the past, the emergence of "unhandled exception".
C. Errors: Problems that programmers cannot solve with code, often hardware problems, environmental problems, system problems
2. Three methods of exception handling:
A. Pre-award, early elimination of abnormal conditions
B. Exception capture: try-catch-finally
C. Do not handle, run out and let others handle: throw (for business exceptions)
3. Exception Capture (try-catch-finally)
A. Syntax:
Try{/*code with the possibility of an exception*/} Catch(/*Exception Reference 1*/){/*Handling 1*/} Catch(/*Exception Reference 2*/){/*Handling 2*/} finally{/*code that must be executed*/}
B. Specification: ①try, catch, finally can not exist alone
Catch or finally must be received after ②try
③catch and finally must be followed by a try
④ when a try is followed by more than one catch, if the exception class in the catch has an inheritance relationship, the catch block that captures the parent class is written behind, and the catch block of the capturing subclass is written in front
Write in C.catch block: ① exception hint
② Record Diary
③ex.printstacktrace (); (used only during development)
4.finally keywords
A. Application: Finally, the cleanup of general resources, the closure of the channel
B. Features: No matter whether it is break, continue, or return, it cannot be prevented from executing, that is, finally is executed before them (the value returned after return is the exception)
Comparison of C.final, Finalize (), finally:
①final: Modifier variable: constant
Modified class: Final class, Eunuch class, cannot be inherited
Modification method: cannot be overridden
②finalize (): Method of Object, GC call, used to destroy object
③finally: Code that must be executed regardless of whether an exception occurred
5.throw and throws:
A. Concept: Throw: Throw this exception object with the Throw keyword
Throws: When a non-runtime exception is thrown, the caller of this method must be warned at compile time, so there should be throws content in the declaration part of the method, which indicates that this method is likely to occur abnormally
B. Discrimination: ①throw and throws are all keywords, all of which belong to the exception handling section
②throw is written in the method body, followed by an exception object
Throws is written at the end of the declaration of the method, followed by one or more exception classes
③ when you run to the throw code, it really happens to throw an abnormal action,
and throws is actually compiling a warning to the caller of this method, what types of exceptions may occur
④ when a method body has a throw statement, there is not necessarily throws unless the throw is followed by a compilation is an exception, it must be written throws
6. Three-tier architecture:
A. Presentation layer: Which classes are used for interface presentation
B. Business layer: The classes used for business processing, such as: Login, Access processing
C. Data access layer: Classes are used to manipulate databases
7. Custom Exceptions
Public class extends exception{ public lovoexception () { } public lovoexception ( Throwable th) { super(TH); } Public lovoexception (String msg) { super(msg);} }
A. Custom exceptions be sure to inherit exception
B. Custom exceptions must have at least two parameter constructs, one with an exception parameter, one with a string parameter
The former is used to convert other exception types to custom exceptions
The latter is used to operate non-JVM unsolicited business exceptions (e.g., salary is negative)
C. Extension methods can be provided for custom exceptions
Java Exception Handling