Java programming ideology Reading Notes-5 (chapter 5)

Source: Internet
Author: User

Chapter 4 error handling through exception
I. Basic exceptions
1. Principle of throwing an exception
1) a new exception object is generated on heap as if a Java object was generated.
2) stop the current execution route and throw the reference of the above exception object from the current context.
3) The exception handling mechanism takes over and looks for appropriate locations for continued execution.
2. generate an exception object
The exception class has two constructor functions: a default constructor and a constructor with String parameters. The parameter information can be retrieved through various methods in the exception class.
3. Structure of exception classes

1) Error is a compilation Error or system Error. Generally, you do not need to catch an Error exception in the program.
2) Exception is an Exception that we can capture. Exception exceptions are classified into RuntimeException and non-RuntimeException.

Ii. Exception capturing and handling
1. Capture exceptions
1.1 try... Catch can catch exceptions.

  1. ImportJava. lang.RuntimeException;
  2. ImportJava. lang.NullPointerException;
  3. ImportJava. SQL.SQLException;
  4. ImportJava. io.IOException;
  5. ClassTestException {
  6. Public VoidTestSQLException ()Throws SQLException{
  7. Throw New SQLException();
  8. }
  9. Public VoidTestIOException ()Throws IOException{}
  10. }
  11. Public ClassTest {
  12. Public Static VoidMain (String[] Args ){
  13. TestException te =NewTestException ();
  14. Try{
  15. Te. testSQLException ();
  16. Te. testIOException ();
  17. }
  18. Catch(SQLExceptionEx ){
  19. System. Out. println ("catch SQLException in main ");
  20. }
  21. Catch(IOExceptionEx ){
  22. System. Out. println ("catch IOException in main ");
  23. }
  24. Catch(ExceptionEx ){// (1)
  25. System. Out. println ("catch Exception in main ");
  26. }
  27. }
  28. }

Running result: catch SQLException in main
Only catch with the same or similar parameter type and exception type will be executed.
1.2 catch all exceptions
To capture all exceptions, you only need to catch exceptions, as shown in (1) of the above Code.
2. exception specification)
1) when defining a function, you can declare an exception specification. If a function declares a non-RuntimeException exception in the exception type, you must capture the non-RuntimeException exception in the exception type when calling this function.
  1. ImportJava. lang.RuntimeException;
  2. ImportJava. lang.NullPointerException;
  3. ImportJava. SQL.SQLException;
  4. ClassTestException {
  5. // (1) the declaration in the exception specification will throw a RuntimeException
  6. Public VoidTestRuntime ()Throws RuntimeException{}
  7. // (2) an NullPointerException will be thrown in the description of the exception specification.
  8. Public VoidTestNullPointer ()Throws NullPointerException{}
  9. // (3) the non-RuntimeException will be thrown in the description of the exception specification.
  10. Public VoidTestNonRunti

Related Article

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.