Simple use examples for try and throw _java

Source: Internet
Author: User
Tags try catch
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 ();
}
}

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.