"Java Programming Thought" Notes Chapter 12 Handling Errors Through Exceptions

1. Exceptions are also objects

  •  The standard exception classes have two constructors, one default and one that accepts a string .

1.1 Throwing an exception is different from the method return type, but has a similar effect to make the current method exit and return. Throwing an exception can be regarded as a different return mechanism. (The similarities and differences do not need to go into depth)

1.2 Throwable class object can be thrown by any exception, it is the root class of exception .

1.3 The only thing in the exception object is the exception type, and there is generally no other content.

1.4 If a method internally or internally calls other methods to throw an exception, the method will end in the process of throwing an exception .

1.5 The identifier id may not be used, but it must be written . The catch should be immediately after the try block. After matching the most suitable exception, the catch after entering the catch is not matched, which is different from the switch that will continue to match without break.

		try{
			
		}catch(Type1 id1){
			
		}catch(Type2 id2){
			
		}...

2. Restoring the model

  • catch does not throw exceptions to fix errors, or put try in a while loop and keep entering try until a satisfactory result is obtained.

3. Custom exception 

  • To customize an exception must inherit from an existing exception.

4. Record the exception in the log

		Logger logger  = logger.getLogger(ExceptionName)
		
		StringWriter trace = new StringWriter();
		printStackTrace(new PrintWriter(trace));
		logger.severe(trace.toString);

5. Exception description 

  • The keyword throws follows a method, eg f() throws Exception,Exception1, . . .{} means that this method f may throw an exception in the exception list. The keyword throw, throws an exception.

6. Exception class hierarchy diagram and its individual important subclasses

1.Throwable

    1.1 Error 

  • Errors, system errors, errors that the JVM cannot handle, generally have nothing to do with the code.

    1.2 Exception

  • Base class for all exceptions related to programming.
  •         1.2.1 IOException : Input and output exceptions
  •         1.2.2 RuntimeException : runtime exception

7. Unchecked exceptions

  • Error and RuntimeException, also known as unchecked exceptions, are exceptions that the compiler does not require forcibly disposed of, and can be accepted without catch. Other Exceptions are checked exceptions (checked exceptions) exceptions that the compiler requires to be disposed of.

8. Stack trace

8.1 Two methods of the Throwable class obtain exception information.

  • String getMessage(); Get exception details (detailed descriptions added in the constructor)
  • String getLocalizedMessage(); Details of the local language description, the default is the same as getMessage().
  • toString(); directly print the exception identifier e

8.2 Printing the stack trace

  • void printStackTrace(); output to standard error
  • void printStackTrace(PrintStrean); output byte stream
  • void printStackTrace(java.io.PrintWriter); output to character stream

9. Rethrow the received exception

  • throw.e , thrown to the previous level, the subsequent catch of this level will not be executed again. After re-throwing, the stack trace is still the original trace. To update this information, use throw (Exception) e.fillInStackTrace(); this becomes the new place where the exception occurs, just like throwing a new exception here, the stack trace starts here .

10. Exception chain

11. finally always executes .

  •  try--( catch is optional )--finally

11.1 try finally Putting it in a loop can achieve " a certain condition must be met before the program continues to execute ".

11.2 Restoring resources to their initial state can finally

11.3 After the execution of the try block is stopped (without catch), finally will be executed immediately before the next operation is performed

  • finally executes before the exception is thrown to higher layers
  • When break, continue or label is finally executed first
  • return is also executed first in finally

11.4 Finally usage that may cause the original exception to be lost:

  • Another exception is generated in finally   
  • return in finally. Typically this exception is a runtime exception.

12. Exceptional limitations

  • When overriding a method, only one or more exceptions in the exception list of the base class method or its subclass exceptions can be thrown or not thrown , but exceptions that the base class does not have cannot be thrown.

13. Constructor exception

13.1 If the constructor throws an exception, an exception should also be thrown after the object is created, and it is exactly the same as that of the constructor .

13.2 If the parent class constructor throws an exception, the subclass constructor must throw all the exceptions of the base class, and it can throw other exceptions .

14. Exception matching

  • The base class exception will match the subclass exception and only the first matching catch , so when the base class and the subclass exception must match in multiple catches, the subclass exception catch must appear before the parent class catch , if After appearing in the parent class, it will be captured by the parent class and match the subclass catch will never be matched, so an error will be reported.

15. The Exception Principle

  • A principle of exception handling is that you can only catch the exception if you know how to handle it, otherwise swallowing may occur, and the exception is sent but you don't know it.

Guess you like

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