Simple use examples for try and throw

  • 2020-04-01 02:08:56
  • OfStack

 
//Every once in a while, try gets a little rusty, and a special program is written to test the following. Deepen your impression.
//There are three ways to exit a piece of code (a Method, or a block) : throw, return, and execute normally.
//Sometimes a throw is thrown from the bottom, and if you don't handle it, you throw it by default.
// TestTry.java 

 
public class TestTry { 


public static void test1(){ 
System.out.println("test1()  perform "); 
// 
test2(); 
// 
System.out.println("test1(), after test2 after   perform "); 
} 
public static void test2(){ 
System.out.println("test2()  Enter the "); 
try { 
test3(true); 
System.out.println("test2() try  The rest "); 
} catch (Exception e) { 
System.out.println("test2() catch (Exception e)"); 
e.printStackTrace(); 
} 
System.out.println("test2() try catch  The rest ..."); 
} 

public static void test3(boolean isThrow) throws Exception{ 
System.out.println(" Enter the  test3() . "); 
try { 
if (isThrow) { 
throw new Exception("test3()  Exceptions thrown "); 
} 
// 
System.out.println("test3()  After the exception is thrown try content ..."); 
} catch (Exception e) { 
// 
System.out.println("test3() catch (Exception e)"); 
throw e; 
} 
// 
System.out.println("test3() try catch  The rest ..."); 
} 


public static void main(String[] args) { 
test1(); 
} 
} 

Related articles: