I. Handling of anomalies
(i) Classification of anomalies
No Exceptions checked: Exceptions that are not necessarily thrown or caught (uncontrolled or should be avoided)
Error:java Run-time System Internal Error, resource exhaustion error. (not controlled)
RuntimeException: An exception caused by a program error. such as array bounds, null pointers. (should avoid happening)
checked for exceptions: Exceptions that must be thrown or caught
IOException: Exception caused by other errors.
(ii) Declaration of exceptions
An exception can be thrown when an unhandled situation occurs, indicating what might be wrong.
throw an exception:
1. A method was invoked to throw the checked exception (the method header declares an exception, otherwise no processor catches the exception, the current thread ends)
2. Errors occurred during the operation of the program, and throw thrown the checked exception (in the method header declaration exception, otherwise there is no processor catch exception, the current thread end)
3. Procedural error
4.Java virtual machine and Run-time Library internal exception (no need to declare)
Subclasses cannot throw a larger exception when overriding a parent class method.
(iii) throwing an exception
Step: Find the appropriate exception class, create the object, throw the object
Throw it away, and you don't have to deal with it.
Example: throwing eofexception during file input
String ReadData (Scanner in) throws eofexception{...
if (!in.hasnext ()) {
if (n < len) {//file is accidentally interrupted during the read process, throw eofexception exception
throw new Eofexception ();
}
}
return s;
}
The eofexception also contains a method of constructing a string parameter that describes the occurrence of an exception.
(iv) Create exception classes
Exceptions that are not fully described by the standard exception class are described with a custom exception class, which simply defines a class that derives from exception or its subclasses. Defines two construction methods, one by default, and one with string arguments.
Class Fileformatexception extends ioexception{public
fileformatexception () {} public
fileformatexception ( String message) {
super (message);
Invokes the Throwable constructor method of the parent class, where Throwable overrides the ToString method and prints the message
}
second, catch the exception
If the code in the try statement block throws the exception type in a Catch statement block, the processor code in the catch clause is executed, otherwise the catch statement block is skipped.
try{
Code
}
catch (ExceptionType1 E1) {
handler for this type
}
e1.getmessage (); Get more information about exceptions
catch (ExceptionType2 E2) {
...
}
catch (ExceptionType3 E3) {
...
} Can catch multiple exceptions
catch exceptions that know how to handle, throw exceptions that don't know how to handle (throws) (i) throw out the anomaly & abnormal chain again
Throws an exception in a catch clause, changing the type of the exception. (only indicates a failure, no details displayed)
try{...
}
catch (SQLException e) {
//Use this packaging technique to both throw advanced exceptions without losing the details of the original exception
throwable se = new servletexception ("Database Error");
Se.initcause (e);
Throw SE;
When an exception is caught, the following is the original exception
Throwable ee = se.getcause ();
}
(ii) finally clause
The FINALLY clause executes regardless of whether an exception is thrown.
Graphics g = Image.getgraphics ();
try{
//1
code that might throw exceptions
//2
}
catch (IOException e) {
//3
Show Error dialog
//4
}
finally{
//5
g.dispose ();
}
6
The code did not throw an exception: execute 1, 2, 5, 6.
exceptions caught by the throw catch clause: 1, 3, 4, 5, 6 (The catch clause does not throw an exception) 1, 3, 5 (The catch clause throws an exception), and throws the exception back to the method caller
The Try statement does not throw exceptions caught by a catch clause: 1, 5, and throws the exception back to the method caller
Close the resource with the finally clause.
The return value of the finally clause overrides the return value in the try and catch clauses.
The finally clause throws an exception that overrides the exception thrown by the catch clause,
The exception thrown by a catch clause is ignored when there is a return in the finally clause.