Thorough understanding Java's abnormal treatment-1

zhaozj2021-02-17  46

One. Substantially unusual

1. Throw an abnormal principle

1) An exception object is generated on the HEAP like generating a Java object.

2) Stop the current execution route, and throw the Reference of the above exception objects from the current context.

3) The abnormal handling mechanism takes over to find the appropriate location to continue to perform.

2. Produce an exception object

Abnormal classes have two constructor: a default constructor; a constructor with String type parameters, information of parameters can be taken out by various methods in the anomaly class.

3. Structure of anomalous class

1) Error is some compile period errors or system errors, generally do not need to capture Error exceptions in the program.

2) Exception is an exception we can capture, where Exception is unusually divided into two categories of RuntimeException and Non-runtimeException.

two. Abnormal capture and processing

Abnormal capture

1.1 Capture anomalies through try ... catch

Import java.lang.RuntimeException;

Import java.lang.nullpointersRexception;

Import java.sql.sqlexception;

Import java.io.ioException;

Class testException {

Public void testsqlexception () throws sqlexception {

Throw new sqlexception ();

}

Public void testioException () throws oException {}

}

Public class test {

Public static void main (String [] args) {

TESTEXCEPTION TE = New TESTEXCEPTION ();

Try {

TE.TESTSQLEXCEPTION ();

TE.TESTIOEXCEPTION ();

}

Catch (Sqlexception EX) {

System.out.println ("catch sqlexception in main);

}

Catch (IOException EX) {

System.out.println ("Catch IOException In Main);

}

Catch (Exception EX) {// (1)

System.out.println ("Catch Exception In Main");

}

}

}

The result of the operation is: Catch Sqlexception in Main

Only the CATCH that the parameter type is the same or similar or similar.

1.2 discussion of abnormal processing

1.2.1 When the statement in the TRY produces an exception, when the exception handler captures the exception and does not re-throw an exception, the TRY block that occurs will be jumped out after executing the processing function, and then the following statement is performed. :

Import java.sql.sqlexception;

Class testException {

Public static void tsql () throws sqlexception {

System.out.println ("Originating the Exception in TSQL ()");

Throw New Sqlexception ("Throw in TSQL");

}

Public void f () throws sqlexception {TRY {

TSQL ();

}

Catch (Sqlexception EX) {

System.out.println ("Catch Sqlexception In f ()");

// Throw EX; (1)

}

System.out.println ("After exception handle in f ()"); // (2)

}

}

Public class test {

Public static void main (String [] args) {

TESTEXCEPTION TE = New TESTEXCEPTION ();

Try {

TE.f ();

}

Catch (Sqlexception EX) {

System.out.println ("Catch TE.f () SQLEXCEPTIONIN Main");

}

Catch (Exception EX) {

System.out.println ("Catch TE.f () Exception In Main");

}

}

}

The result of the operation is:

Originating the Exception in TSQL ()

Catch sqlexception in f ()

After Exception Handle In f ()

After performing an exception handler in the f () function, the code outside the Try ... .catch except that occurs (2) is continued.

1.2.2 When the statement in the TRY generates an exception, when the exception handler captures the exception and re-throwing an exception, the exception will be thrown into the last level of Context, the code after the abnormal statement will not carried out.

If the comment at the above code (1) last year, the resulting result becomes:

Originating the Exception in TSQL ()

Catch sqlexception in f ()

Catch te.f () SQLEXCEPTION IN MAIN

Since the abnormality is reopened at (1), the code is not executed.

1.2.3 If the corresponding exception is processed, the exception will be thrown into the last layer Context, the code after the abnormal statement will not be executed.

Import java.sql.sqlexception;

Class testException {

Public static void tsql () throws sqlexception {

System.out.println ("Originating the Exception in TSQL ()");

Throw New Sqlexception ("Throw in TSQL");

}

Public void f () throws sqlexception {

INT x = 0;

System.out.println ("10 /" x "=" 10 / x); // (1)

System.out.Println ("After Exception Handle In f ()");

}

}

Public class test {

Public static void main (String [] args) {

TESTEXCEPTION TE = New TESTEXCEPTION ();

Try {

TE.f ();

}

Catch (SQLEXCEPTION EX) {System.out.println ("Catch TE.f () sqlexception in main");

}

Catch (Exception EX) {

System.out.println ("Catch TE.f () Exception In Main");

}

}

}

The result of the operation is:

Catch te.f () Exception in main

An exception occurs at code (1), but does not process it in the F () function, so it will be thrown into the last layer Context and jump out of the F () function.

1.3 capture all exceptions

If you want to capture all anomalies, just capture

EXCEPTION

Exception, such as the above code (

1

Place

转载请注明原文地址:https://www.9cbs.com/read-29197.html

New Post(0)