1. Concept
2. Basic exception: An exception condition is an issue that prevents the current method or scope from continuing to execute.
2 (1). Exception parameters: All standard exception classes have two constructors: one is the default constructor, and the other is to accept the string as a parameter so that the relevant information can be placed in the constructor of the exception object.
3. Catching exceptions
3 (1). Try block: If an exception is thrown inside a method, the method ends in the process of throwing an exception. If you do not want the method to end, you can set a special block inside the method to catch the exception. Because in this block "try" a variety of (possibly abnormal) method calls, so called try Block
3 (2). Exception handlers: Exception handling theoretically there are two basic models. Terminating the model: it is critical to assume that the error is so that the program cannot return to the place where the exception occurred. Recovery model: The job of the exception handler is to fix the error, and then try again to invoke the problematic method (then you cannot throw an exception when encountering an error, but instead call the method to fix the error.) Alternatively, place the try block in the while loop, and keep entering the try block until you get a satisfactory result. )
4. Create a custom exception: To customize an exception class, you must inherit from an existing exception class, preferably by choosing an exception class that has a similar meaning. The simplest way to create a new exception type is to have the compiler generate a default constructor for you.
4 (1). Exception and logging: the static Logger.getlogger () method creates a string parameter associated with the Logger object (usually the package name and class name associated with the error), and the Logger object sends its output to System.err. The simplest way to write to logger is to directly invoke the method associated with the level of the logging message, which is used by severe (). In order to generate a log message, we want to get the stack trace where the exception was thrown, but Printstacktrace () does not produce the string by default. In order to get the string, we need to use the overloaded Printstacktrace () method, which takes a Java.io.PrintWriter object as a parameter.
5. Exception Description: If the code in the method produces an exception and is not processed, the compiler will find the problem and remind you: either handle the exception, or in the exception description that this method will produce an exception. The exception description uses the additional keyword throws, followed by a list of all potential exception types.
6. Catch all exceptions: Write only one exception handler to catch all types of exceptions. By capturing the base class of the exception type Exception, you can do this catch (Exception e) {System.out.println ("caught an Exception");}
6 (1). Stack trajectory
6 (2) Re-throw exception: catch (Exception e) {{System.out.println ("caught an Exception"); throw e;}
6 (3). Exception chain: You often want to throw another exception after catching an exception, and want to save the information of the original exception, which is called the exception chain.
7.Java Standard Exception:
7 (1). Exception: RuntimeException. If you call a null reference, Java automatically throws the NullPointerException exception (which you do not have to do yourself, which is part of the standard runtime detection of Java).
8. Use finally to clean up: they can be executed regardless of whether the exception in the try block is thrown. This usually applies to situations outside of memory reclamation.
8 (1). Finally what to do: Use the finally clause when you want to restore resources other than memory to their initialization state. Resources that need to be cleaned up include open files or network connections, graphics on the screen, or even a switch from the outside world.
9. Limitations of Exceptions:
Java Programming thought note (through exception handling errors)