Java Programming thought note 12th through exception handling errors

Source: Internet
Author: User

1. Exceptions are also objects
    • The standard exception class has two constructors, one default and one accepts strings.

The 1.1 throw exception is different from the method return type, but a similar effect causes the current method to exit and return, and throwing exceptions can be considered as a different return mechanism. (no need to delve into the similarities and differences.)

The 1.2 Throwable class object can be thrown by any exception, which is the root class of the exception.

1.3 Exception objects are only exception types, and other content is generally not.

1.4 A method throws an exception internally or internally, and the method ends when the exception is thrown.

1.5 identifier ID may not be used but cannot be written, catch is immediately after the try block, after matching to the most appropriate exception after entering the catch after the catch does not match, this is not a break will continue to match the switch is different.

Try {                    }catch(Type1 id1) {                    }catch(Type2 id2) {                    } ...
2. Recovery model
    • Catch does not throw an exception to fix an error, or put a try into a while loop so that it continues to enter the try until a satisfactory result is obtained.
3. Custom Exceptions
    • To customize an exception, you must inherit from an existing exception.
4. Record exceptions in the log
Logger Logger  = Logger.getlogger (exceptionname)                new  StringWriter ();        Printstacktrace (new  PrintWriter (Trace));        Logger.severe (trace.tostring);
5. Exception description
    • Keyword throws follows the method, such as f () throws Exception,exception1, ...  {} means that this method F may throw exceptions in the exception list. Keyword throw, throws an exception.
6. Exception class hierarchy diagram and its individual important subclass 1. Throwable 1.1 Error
    • Errors, the system itself is wrong, the JVM encounters an unhandled error, and is generally not code-independent.
1.2 Exception
    • All exception base classes related to programming.
    • 1.2.1 IOException: input, output exception
    • 1.2.2 RuntimeException: Run-time exception
7. Non-checked exceptions
    • Error and RuntimeException are also known as non-checked exceptions (unchecked exceptions) The compiler does not require a forced disposition of exceptions, can be accepted without catch, and other exception are exceptions that can be checked (checked Exceptions) The compiler requires exceptions that must be disposed of.
8. Stack Track

8.1 The 2 methods of the Throwable class to get exception information.

    • String getMessage (); Gets exception details (added description inside the constructor details)
    • String Getlocalizedmessage (); the details of the local language description, default and GetMessage ().
    • ToString (); Direct Print exception identifier E

8.2 Print Stack Track

    • void Printstacktrace (); Output to standard error
    • void Printstacktrace (Printstrean); output byte stream
    • void Printstacktrace (java.io.PrintWriter); output to character stream
9. Re-throw the accepted exception
    • THROW.E, thrown to the upper level, the subsequent catch in this level will no longer execute. Re-throw the back stack trajectory or the original trajectory, update this information with throw (Exception) e.fillinstacktrace (); This is where the unusual new occurrence takes place, just as a new exception is thrown here, and the stack trajectory starts here.
10. The abnormal chain 11.finally always executes.
    • try--(Catch can be none)--finally

11.1 Try finally in the loop you can do "a certain condition must be met before the program continues to execute".

11.2 Restoring resources to the initial state can finally

11.3 Try block execution stops (no catch) and finally executes immediately before performing the next action

    • A finally is executed before the exception is thrown to a higher level
    • When break, continue, or tagged, finally is executed first.
    • Return will be executed first in finally

11.4 Finally usage that could cause the original exception to be lost:

    • Finally, another exception is generated.
    • Finally in return. Typically, this exception is a run-time exception.
12. Limitations of exceptions
    • Overriding a method can only throw one or more exceptions in the exception list of a base class method or its subclass exception or not, but cannot throw an exception that the base class does not have.
13. Constructor exception

13.1 The constructor throws an exception, and an exception is thrown after the object is created, and is exactly the same as the constructor.

13.2 The parent class constructor throws an exception, and the subclass constructor must throw all the exceptions of the base class, not be thrown, and can throw other exceptions.

14. Exception Matching
    • The base class exception matches the subclass exception and matches only the first catch that matches, so when the base class and subclass exception are matched in multiple catches, the subclass exception catch is to appear before the parent class catch, and if it appears after the parent class, the matching sub-class catch will never be matched. So the error will be.
15. Exception principle
    • Exception handling a principle is: only know how to deal with the exception to catch the exception, or it may happen to swallow, both abnormal hair and do not know.

Java Programming thought note 12th through exception handling errors

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.