Copy Code code as follows:
Every once in a while, you will always be a little rusty on try, specially write a program to test the following. Deepen the impression.
Exit a piece of code (that is, a method, or a block), there are three ways: Throw,return, and normal execution.
Sometimes throw is thrown out of the bottom, you do not handle, the default is throw.
Testtry.java
/**
* Output results are:
Exception thrown by Java.lang.Exception:test3 ()
At Test. Testtry.test3 (testtry.java:29)
At Test. Testtry.test2 (TESTTRY.JAVA:16)
At Test. Testtry.test1 (Testtry.java:9)
At Test. Testtry.main (testtry.java:44)
Test1 () execution
Test2 () Enter
Enter Test3 () ...
Test3 () catch (Exception e)
Test2 () catch (Exception e)
Test2 () try to catch the following ...
Test1 (), executed after test2 execution
*/
public class Testtry {
public static void Test1 () {
System.out.println ("Test1 () execution");
//
Test2 ();
//
System.out.println ("test1 (), execution after completion of test2");
}
public static void Test2 () {
System.out.println ("Test2 () enter");
try {
Test3 (TRUE);
System.out.println ("Test2 ()" After the contents of the try);
catch (Exception e) {
System.out.println ("Test2 () catch (Exception e)");
E.printstacktrace ();
}
System.out.println ("Test2 () the content following the try catch ...");
}
public static void Test3 (Boolean isthrow) throws exception{
System.out.println ("Enter Test3") ... ");
try {
if (Isthrow) {
throw new Exception ("Test3 () thrown exception");
}
//
System.out.println ("Test3 () throws an exception after the try content ...");
catch (Exception e) {
//
System.out.println ("Test3 () catch (Exception e)");
Throw e;
}
//
System.out.println ("Test3 () the content following the try catch ...");
}
public static void Main (string[] args) {
Test1 ();
}
}