006 of Java basics-exception
Learning Java at the age of 35
1.1 system throwable -- error usually has major problems, such as the running class does not exist or memory overflow. If you do not compile the code to handle it-exception occurs when running, you can try catch finally
1.2 Both the exception and error sub-classes use the parent class name as the suffix. When designing an exception system, Java encapsulates all situations that are prone to exceptions into objects.
1.3 method 1 in throwable) getmessage () gets exception information and returns a string. 2) tostring () gets the exception class name and exception information, and returns a string. 3) printstacktrace () obtains the exception class name and exception information, as well as the location where the exception occurs in the program. Return Value void. 4) printstacktrace (printstream s) usually saves the exception content in the log file for reference.
1.4 throws and throw1) throws are used to identify the exceptions exposed by the function. 2) throw is used to throw an exception object. 3) Difference between throws and throw: thorws are used in functions, followed by the exception class name. Throw is used in the function, followed by the exception object. Note: When defining functional methods, you need to expose the problems for the caller to handle. Then it is identified by throws on the function. In some internal situations of function methods, the program cannot continue to run. When a jump is required, throw the exception object. 1.5 Exception Handling try {code to be detected;} catch (exception class variable) {Exception Handling Code;} finally {code to be executed ;} the finally code block is not executed in only one case. System. Exit (0) is executed before ).
Handling process: When an exception is detected, the exception object is passed to the catch object, and the catch object is caught to the exception for processing. Finally is usually used to close resources. For example, database resources and I/O resources. Note: Try is an independent code block. The variables defined in try are only valid in the variable block. If you want to continue using it outside of try, you must create a reference in try. Initialize it in try. Io, socket will encounter.
1.6 custom exception 1) the custom class inherits exception or its subclass. 2) define the exception information through the constructor. For example, class demoexception extendsexception {demoexception (stringmessage) {super (Message) ;}3) throws a custom exception through throw.
1.7 exception details 1) If runtimeexception and its subclass are throttled by throw in the function, you do not need to declare it on the function. 2) When a method is overwritten, the method that overwrites it must throw the same exception or exception subclass. 3) if the parent class throws multiple exceptions, the override (overwrite) method must throw a subset of those exceptions and cannot throw new exceptions. 4) To put it simply, the subclass overwrites the parent class and can only throw exceptions of the parent class or subclasses or subsets. Note: If the method of the parent class does not throw an exception, you must try to overwrite the Child class.