Exception: Exception: is not normal.
Abnormal conditions that occur when the program is running. is actually the problem that appears in the program. This problem is described in terms of object-oriented thinking and encapsulated as an object. Because of the cause of the problem, a problematic name, a problematic description, and many other attribute information exists. The most convenient way to have multi-attribute information is to encapsulate that information.
The exception is that Java encapsulates the problem by object-oriented thinking. This will facilitate the operation of the problem and deal with the problem. There are a number of problems, such as the angle mark out of bounds, null pointers and so on. To classify these issues. And these problems have common content such as: Each problem has a name, but also the problem of the description of the information, the location of the problem, so you can continue to extract upward. Formed an anomaly system. --------Java.lang.Throwable:Throwable: Can be thrown. |--error:errors, in general, do not write targeted code for processing, usually the JVM occurs, need to fix the program. |--exception: Exceptions, can be targeted processing methods whether it is error or exception, they have a specific subclass to reflect each problem, their subclasses have a common, that is, the parent class name only as a subclass of the suffix name. All classes and objects in this system have a unique feature, which is parabolic. The embodiment of the parabolic: the class and object in this system can be manipulated by throws and throw two key words. ------------------------------------------------------class Exceptiondemo{public static void Main (string[] args) {// byte[] buf = new byte[1024*1024*700];//java.lang.outofmemoryerror memory overflow Error}}----------------------------------------- -------------at the time of development, if you define a feature, you will see some problems with the feature, and you should identify the problem when defining the function, so that the caller can provide a way to handle this function in advance. How to mark it? By throws keyword completion, format: Throws Exception class name, Exception class name ...After this is marked, the caller, when using this function, must be processed, or the compilation fails. There are two kinds of processing methods: 1, capturing, 2, throwing. For capture: Java has a targeted block of statements for processing. Try {Code that needs to be instrumented;}catch (Exception class variable name) {exception handling code; code that}fianlly{is bound to execute;}-------------------------------------------------------- catch (Exception e) {//e is used to receive the exception object that the try detects. SYSTEM.OUT.PRINTLN ("Message:" +e.getmessage ());//Gets the information of the exception. System.out.println ("toString:" +e.tostring ());//Gets the exception's name + exception information. E.printstacktrace ();//print exception information in the stack, exception name + exception information + location of exception. ---------------------------------------------------------Exception Handling principle: function throws a few exceptions, function call if try processing, need corresponding catch processing code block, Such treatment is targeted, a few to deal with a few.Special case: When a try corresponds to multiple catch, it must be placed below if there is a catch statement block for the parent class. the difference between the throw and the throws keyword: throw is used to throw an exception object, followed by an exception object, and the throw is used within the function. Throws is used to throw exception classes, followed by the exception class name, can be followed by multiple, separated by commas. Throws is used on functions. Generally: function content if there is a throw, throw exception object, and do not handle, then the function must be declared, otherwise the compilation fails. But there are also special cases.The exception is divided into two types: 1: The exception that is checked at compile time, as long as the exception and its subclasses are detected at compile time. 2: Run-time exception, where exception has a special subclass RuntimeException, and the subclass of RuntimeException is a run exception, it is said that the exception is not checked at compile time. The difference between the exception checked at compile time and the run-time exception: Compile-checked exceptions are thrown inside the function, the function must be declared, and no compilation fails. Reason for declaration: This exception needs to be handled by the caller. Run-time exception if thrown inside a function, no declaration is required on the function. Not declared reason: Do not need the caller to handle, run-time exception occurs, can no longer let the program continue to run, so, do not let the call processing, directly let the program stop, by the caller to modify the code. When defining a try when you define exception handling, when do you define throws? If an exception occurs inside the function, use try if it can be handled internally, and if the function cannot handle it, it must be declared and let the caller handle it. Custom exceptions: When developing a problem that is not defined in Java in the project, then we need to follow the Java exception to establish the idea, the project's unique problems are also encapsulated objects. This exception, called a custom exception. For division operations, 0 is not possible as a divisor. This problem is described in Java with the ArithmeticException class. For this function, in our project, the divisor can not be a negative number except 0. But the negative part of Java does not have a description. So we need to customize this exception. To customize the exception: 1: Define a subclass to inherit exception or RuntimeException to make the class parabolic. 2: Operate by throw or throws. Abnormal conversion idea: When the exception that occurs is not handled by the caller, it is necessary to convert this exception to an exception that the caller can handle. Several combinations of try Catch finally: Try catchfinally Remember that finally is useful and is primarily used to close resources. The resource must be closed, regardless of whether an exception occurred.system.exit (0);//exit the JVM, only this case finally does not execute. When the exception occurs, when the child parent class overwrites, there are some new features:1: When a subclass overrides a method of the parent class, if the method of the parent class throws an exception, the child class's method either throws an exception or throws the parent class exception or subclass of the exception, and cannot throw other exceptions. 2: If the parent throws more than one exception, the child class can only throw a subset of the exception of the parent class when overridden. Attention:if a method in the parent class or interface does not throw an exception, then the subclass is not allowed to throw an exception, and if an exception occurs in the overridden method of the subclass, only try cannot throws. If this exception subclass can not be processed, has affected the specific operation of the subclass method, at this time can be in the subclass method, through the throw throw RuntimeException exception or its subclasses, so that the subclass of the method is not required throws declaration. Common exceptions: 1, the target bounds exception (indexoutofboundsexception) includes array, string, null pointer exception (NULLPOINTEREXCEPTION) 2, type conversion exception: ClassCastException3, There is no exception to this element: NullPointerException4, operation exception is not supported, exceptions should be avoided, if not avoided, a pre-processing method is required.
Java Fundamentals (9)---exceptions