Thoroughly understand the abnormal treatment of Java-2

zhaozj2021-02-17  46

2. Exception Specification

1) Declaration of exception specifications when the function is defined. If a function declares Non-RuntimeException in an exception specification, it is necessary to capture the non-runtimeException in an abnormal specification when calling this function.

Import java.lang.RuntimeException;

Import java.lang.nullpointersRexception;

Import java.sql.sqlexception;

Class testException {

/ / (1) The declaration of the abnormal specifications will throw the RuntimeException exception

Public void testime () throws runtimeexception {}

// (2) Abnormal specifications will throw NULLPOINTEREXCEPTION

Public void testnullpointer () throws nullpointersException {}

// (3) Declaration in an abnormal specification will throw Non-RuntimeException

Public void testnonruntime () throws sqlexception {}

}

Public class test {

Public static void main (String [] args) {

TESTEXCEPTION TE = New TESTEXCEPTION ();

TE.TESTRUNTIME (); // (4)

TE.TESTNULLPOINTER (); // (5)

//te.testnonruntime (); (6)

Try {

TE.TESTNONRUNTIME ();

}

Catch (SQLException EX) {}

}

}

In the above code, (1) is in an exception specification declaration will throw RuntimeException; (2) declaration in an exception specification will throw NullPointRexception, and NullPointerexception is the subclass of RuntimeException, so when calling these two functions, Capture abnormality, such as the code at (4) (5) is directly called. But (3) The declaration in an exception specification will throw SQLException, and SQLException is not a subclass of RuntimeException, so you must capture the SQLException exception.

2) If you want to throw Non-RuntimeException in a function, you must declare this exception in an exception specification.

Import java.sql.sqlexception;

Import java.io.ioException;

Class test1 {

Public void f () throws sqlexception {// (2)

Throw new oException ("ioException"); // (1)

}

}

PUBLIC CLASS ExplicitStatic {

Public static void main (String [] args) {

TEST1 TE = New Test1 ();

Try {

TE.f ();

}

Catch (Exception EX) {

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

}

}

In (1) throws a Non-RuntimeException that is not declared in an exception specification, it will be wrong when compiling.

2. Several functions of information in an abnormality

1) String getMessage (), getLocalizedMessage, Tostring

Take information in an abnormal object

Import java.lang.RuntimeException;

Import java.lang.nullpointersRexception;

Import java.sql.sqlexception;

Import java.io.ioException;

Class testException {

Public void tsql () throws sqlexception {

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

Throw New Sqlexception ("Throw in TSQL");

}

}

Public class test {

Public static void main (String [] args) {

TESTEXCEPTION TE = New TESTEXCEPTION ();

Try {

TE.TSQL ();

}

Catch (Sqlexception EX) {

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

System.out.println ("EX.GetMessage ():" ex.getMessage ());

System.out.println ("ex.getlocalizedMessage ():"

EX.GETLOCALIZEDMESSAGE ());

System.out.println ("EX.TOSTRING ():" EX.TOSTRING ());

}

Catch (Exception EX) {

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

}

}

}

operation result:

Originating the Exception in TSQL ()

Catch sqlexception in main

Ex.getMessage (): throw in tsql

Ex.getlocalizedMessage (): throw in tsql

EX.TOSTRING (): java.sql.sqlexception: throw in tsql

2) Void PrintStackTrace (), throwable filmstacktrace ()

PrintStackTrace prints Throwable and its Call Stack TRACE.

FillStackTrace sets new Stack TRACE information at the call point

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 ("IN f (), E.PrintStackTrace ()");

EX.PrintStackTrace ();

throw ex; // (1)

// throw (Sqlexception) ex.FillInstackTrace (); (2)

}

}

}

Public class test {

Public static void main (String [] args) {

TESTEXCEPTION TE = New TESTEXCEPTION ();

Try {

TE.f ();

}

Catch (Sqlexception EX) {

System.out.println ("Catch in main, E.printStackTrace ()");

EX.PrintStackTrace ();

}

Catch (Exception EX) {

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

}

}

}

The result is:

Originating the Exception in TSQL ()

IN f (), E.PrintStackTrace ()

Catch in main, E.PrintStackTrace ()

Java.sql.sqlexception: throw in tsql

Void testException.tsql ()

Test.java: 5

Void testException.f ()

Test.java: 9

Void Test.main (java.lang.string [])

Test.java: 22

Java.sql.sqlexception: throw in tsql

Void testException.tsql ()

Test.java: 5

Void testException.f ()

Test.java: 9

Void Test.main (java.lang.string [])

Test.java: 22

If you comment (1) code, the results will become:

Originating the Exception in TSQL ()

IN f (), E.PrintStackTrace ()

Catch in main, E.PrintStackTrace ()

Java.sql.sqlexception: throw in tsql

Void testException.tsql () // (3)

Test.java: 6

Void testException.f ()

Test.java:10

Void Test.main (java.lang.string [])

Test.java: 24

Java.sql.sqlexception: throw in tsql

void testException.f () // (4)

Test.java: 16

Void Test.main (java.lang.string [])

Test.java: 24

Since the new STACK TRACE information is set at the code (2), the exception is considered to be emitted in f (), so the abnormality obtained in main () is f () (see (3) ), In f () TSQL () (see (6)).

3) If you reap out a different type of exception, you can also generate the effect of the FillStackTrace () function. If the F () function of the above code is modified to the following: public void f () throws sqlexception, oException {

Try {

TSQL ();

}

Catch (Sqlexception EX) {

System.out.println ("IN f (), E.PrintStackTrace ()");

EX.PrintStackTrace ();

Throw new oException (); // (1)

}

}

The result is:

Originating the Exception in TSQL ()

IN f (), E.PrintStackTrace ()

Catch Exception In Main

Java.sql.sqlexception: throw in tsql

Void testException.tsql ()

Test.java: 6

Void testException.f ()

Test.java:10

Void Test.main (java.lang.string [])

Test.java:25

Java.io.ioException

Void testException.f ()

Test.java:17

Void Test.main (java.lang.string [])

Test.java:25

Since a new type of abnormality is thrown at (1), the new exception is captured in main (), so the original throw point of the abnormality captured in main () is f ().

3. RuntimeException exception

The exception represented by RuntimeException and its subclasses we don't have to capture in the program. If this exception occurs, Java will automatically throw the corresponding exception object, such as:

Class testException {

Public Static Void G (INT X) {

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

}

}

Public class test {

Public static void main (String [] args) {

TESTEXCEPTION.G (2);

TESTEXCEPTION.G (0); // (1)

}

}

The above code does not have errors when compiling, and an error occurs only at runtime (1). Although the division may have an error, we don't have to capture. When an error occurs, Java will automatically throw the corresponding exception.

two. Clean up with FINALLY

1. If a certain code is executed whether there is an exception, it can be changed into the Finally block.

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)}

Finally {

System.out.println ("Finally 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 () 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 ()

Finally in f ()

Catch te.f () SQLEXCEPTION IN MAIN

Although the exception is re-thrown at the code (1), the code in the finally block is still executed.

2. Abnormal loss caused by Finally

If the code executed in Finally has an exception, the starting throw of the abnormality captured in the last layer call is the function of Finally.

Import java.sql.sqlexception;

Class testException {

Public static void tsql1 () throws sqlexception {

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

Throw new Sqlexception ("Throw in TSQL1");

}

Public static void tsql2 () throws sqlexception {

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

Throw New Sqlexception ("Throw in TSQL2");

}

Public void f () throws sqlexception {

Try {

TSQL1 ();

}

Catch (Sqlexception EX) {

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

Throw ex; // (2)

}

Finally {

System.out.println ("Finally IN F ()");

// TSQL2 (); (1)

}

}

}

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");

System.out.println ("GetMessage:" ex.getMessage ());

System.out.println ("PrintStackTrace:");

EX.PrintStackTrace ();

}

}

}

The result of the operation is:

Originating the Exception in TSQL ()

Catch sqlexception in f ()

Finally in f ()

Catch te.f () SQLEXCEPTION IN MAIN

GetMessage: throw in tsql1

PrintStackTrace:

Java.sql.sqlexception: throw in tsql1

Void testException.tsql1 ()

Test.java: 5

Void testException.f ()

Test.java :13

Void Test.main (java.lang.string [])

Test.java:29

As can be seen from the results, the starting throw of the captured exception can be properly printed in main (). But if the comment is removed, the result will become:

Originating the Exception in TSQL ()

Catch sqlexception in f ()

Finally in f ()

Originating the Exception in TSQL ()

Catch te.f () SQLEXCEPTION IN MAIN

GetMessage: throw in tsql2

PrintStackTrace:

Java.sql.sqlexception: throw in tsql2

Void testException.tsql2 ()

Test.java: 9

Void testException.f ()

Test.java: 21

Void Test.main (java.lang.string [])

Test.java:29

As can be seen from the results, the exception captured in main () is an exception generated in Finally, and the abnormality thrown in the code (2) is lost.

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

New Post(0)