“This is the 7th day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021.”

1. Basic concepts of exceptions

1) Abnormal events change the program flow;

2) An exception is thrown when an exception event occurs;

3) The code that responds to an exception is called an exception handler;

4) Exception handler catches exceptions;

5) Exception handling allows you to focus on solving the problem in one place and then separate the code that handles the error and put it in another place.

Catch exceptions

1) Set a try/catch block;

2) If any code within the try block throws an exception specified by the catch clause

A. The program skips other code in the try block;

B. The program executes the processor code in the catch clause.

3) Skip the catch clause if no exception is thrown in the try block.

4) The method exits immediately if the exception thrown inside the try block is not specified in the catch clause.

Three, handling exceptions

1. How to control the scope of a try: Depending on the concatenation and dependencies of operations, if a previous block throws an error that affects the execution of subsequent programs, then the two programs are related and should be placed in the same try.

For those exceptions that have been identified, there are two ways to deal with them: throw and try catch.

Put the try catch in a place that can handle exceptions well (that is, in a place that has the ability to handle exceptions). If you can’t handle it, keep tossing.

When we define an exception class ourselves, we must make it inherit Excepiton or RuntimeException.

  1. Exceptions thrown to subclass methods must not be outside the scope of the parent method throws directive. If the parent method does not throw any exceptions, every checked exception must be caught in the subclass method.

Catch multiple exceptions

1) Use a catch clause for each exception type;

2) If an exception is caught in the preceding catch clause, the content of the following catch clause will be directly skipped;

3) It is recommended to order catch clauses by subclass of the exception type -> superclass.

5, Finally statement

The code in the finally clause executes whether or not an exception is caught;

Example:

finally{ con.close(); }Copy the code

6. Exception call stack

1. Which method call code has an exception and returns to the place where the method was called;

2. The code called by the main method is abnormal and returned to the VM.

7. Anomaly level

A: 1) Classes derived from Error represent uncommon environments (usually at the hardware level);

2) The application cannot recover from Error;

3) All Java exceptions originate from Exceptions;

4) RuntimeExcepiton is also called an unchecked exception;

5) Unchecked exceptions do not need to be captured;

6) Other exceptions, also called check exceptions, must be handled

                 Object

                   ↑

               Throwable

                   ↑

| ˉ ˉ ˉ ˉ ˉ ˉ ˉ ˉ |

         Error      Exception

| write

| | ˉ ˉ ˉ ˉ ˉ ˉ ˉ |

              RuntimeException

Eight, some unchecked exceptions

Answer: 1) Java. Lang. ArithmeticException such as: in addition to 0;

2) Java. Lang. NullPointerException, such as: not initialize a References were used;

3) Java. Lang. ArrayIndexoutofBoundsException such as calling a has ten elements the eleventh elements of the Array of content;

4) Java. Lang. A NumberFORMatException such as an Integer. The parseInt (” a “);

Write about your own anomalies

A: 1) All you need to do is to inherit from Exception or derive a class from Exception;

2) Get used to providing a default constructor for each exception class and a constructor with details.

Throw your own exception

A: 1) Add a throws modifier to the definition of a method to inform the caller that an exception may be thrown;

2) Construct an instance of the class in a method and throw it.