1. Exception: This is the abnormal situation that occurs during the operation of the program.
The origin of the exception: the problem itself is a specific thing in daily life, can also be described in the form of Java classes, and encapsulated into objects. In fact, Java is the embodiment of the abnormal description of the object. The division of the problem: one is the severity of a problem, a non-serious problem. For serious, Java is described with the error class. For the error class, it is generally not written against the type code to handle it. For non-critical, Java is described by the exception class. For the exception class, you can use a targeted processing method for processing. Both the error class and the exception class have some common content, such as abnormal information, cause and so on. Throwable--error--exception2. Exception handling Java provides a unique statement to handle: try{needs to be detected code,}catch (Exception class name variable) {processing mode;}finally{is bound to execute the statement;} 3. Common method operations are performed on the caught exception object. GetMessage (); Gets the information of the exception ToString (); Exception Name: Exception information printstacktrace (); Prints the trace information of the exception in the stack, including the exception name, exception information, where the exception occurred. In fact, the JVM default exception handling method is called Printstacktrace (). Declaring an exception on a method makes it easier to improve security, let the call handle it, and the compilation fails without processing. Handling of multiple Exceptions 1. When declaring an exception, it is best to declare it as a more specific exception type, which can also be handled more concretely. 2. The other party declares several exceptions, corresponding to several catch blocks. If the exception in multiple catch blocks has an inheritance relationship, the catch block that uses the parent exception is placed on the last side, because the catch block is processed sequentially. Also, do not define redundant catch blocks. When we recommend catch handling, it is important to define the specific handling in the catch, not to simply define a sentence e.printstacktrace (), or simply write an output statement.
Custom ExceptionsBecause there may be specific problems in the project, and these problems are not described and encapsulated by Java as objects, so for these problems, you can follow the problem encapsulation in Java, the unique problem of a custom exception encapsulation. Custom exception classes should inherit exception. Reasons for inheriting exception: because both the exception class and the exception object need to be thrown, they all have the ability to be parabolic, which is the characteristic of the throwable system. There are classes and objects in this system that can be manipulated by throw and throws. When a throw throws an exception object inside the method, the corresponding processing action must be given. It is either handled internally with Try,catch, or it is declared on the method to be handled by the caller. In general, there is an exception inside the method that needs to be declared on the method.
the difference between throws and throwThrows are used on methods (between parentheses and curly braces), and throw is used inside the method. Throws followed by the exception class name, you can follow a number, the middle separated by commas. The throw is followed by an exception object. There is a special subclass in the exception
runtimeexception(runtime exception), if the class or its subclass exception is thrown inside the method, it can no longer be declared on the method, the compiler can pass, or if the class or its subclass exception is declared on a method, the caller can pass the compilation without processing. There is no need to declare on a method because the caller is not required to handle it. When the exception occurs, the program stops running because there is a situation in which the operation cannot continue, and you want to modify the code after you stop the program. When you customize an exception, let the custom exception inherit runtimeexception If the exception occurs so that the program cannot continue to run. There are two types of exceptions: 1. The exception that was detected at compile time. 2. Exceptions that cannot be detected at compile time. (Run-time exception.) and RuntimeException and their subclasses)
Exception handling--Bi Xiangdong Java Basic Tutorial Learning notes