Java programming ideology notes (7) -- exceptions and java programming ideology notes
1. What is an exception?
Exceptions in java refer to exceptions in programs (excluding hardware exceptions (such as memory overflow), such as syntax errors (less semicolons), divisor 0, and nullPoint.
2. Why exceptions?
No one wants the code to run, and suddenly the program has a problem. At this time, problems such as unstable functions and data error display will occur, leading to a crash and no response from the program, developers do not know what is going on. They can only troubleshoot through logs. Therefore, java requires exceptions to solve program robustness or robustness. The exceptions in java do not mean that there will be no problems with the exception program, but can be completed according to the exception logic when there is an exception in the program, and there will be no downtime or other problems.
3. java solves exceptions
3.1java abstracts Exception resolution into a Throwable Exception class. According to the specific situation (hardware Exception or program Exception), it is divided into two sub-classes: Error and Exception. program Exception (Exception) runtimeException and non-runtime Exception are classified into RuntimeException and non-runtime Exception (Exception inherited ).
3.2 everyone knows that a class is an abstract template with the same attributes and behavior objects. A specific task can be completed only through a specific object during the program running, at this time, we need to talk about the creation of the exception object. Java exception objects can be created in two ways: 1. One is an exception automatically thrown when the program is running. We can throw these exceptions without using the throw keyword. 2: The programmer uses the throw new exception method to throw an exception object based on the specific situation.
3.3 For the specific implementation and use of java exception classes, you can view the api;
4. java exception Mechanism
Try {// Running code} catch (Exception e) {// catch the code executed after an Exception e. printStackTrace ();} finally {// final execution (whether an exception exists )}
More than 4.1 is the java exception mechanism. I personally feel that the book is too long-winded.
4.2 besides try catch finally, there are two keywords: throw and throws.
Throw: An exception object used in the method body to throw the Throwable class. If a running exception is thrown, the method must also declare the thrown exception type in the method header. You must also capture, process, or throw an exception when calling this method. If none of the methods are captured and thrown to the virtual machine, the virtual machine will handle the exception.
Throws: Used to declare the method body and declare the possible types of exceptions. If an exception is declared during running, the method that calls this method must be handled by itself or throw an exception.
4.3 difference and connection between throw and throws:
Throw is an exception thrown by a statement, such as throw new RuntimeException;
Throws an exception.
It is obvious that throws continue to throw an exception after throws are converted to a method exception.
Throws cannot be used independently, but throws can.
5. User-defined exceptions
The custom Exception can be implemented by inheriting the Exception or RuntimeException Exception class.
You can implement the desired results by re-setting or overwriting the parent exception class.
Use new throw myException.
Summary of exceptions .!!!