Package test; The return and throw in the JDK 1.8 public class TestException1 {/** * catch cannot coexist (no matter who has compiled it first) * If you only throw E, you must try-catch capture the XOR
Often e or throw an exception with throws E * If only return, then the normal return value in the catch segment */int testEx0 () {Boolean ret = true;
try {int c = 12/0;
System.out.println ("testex,successfully");
return 0;
} catch (Exception e) {System.out.println ("testEx, Catch Exception");
ret = false;
return-1;
Throw e; }}/** * Return and throw in finally are not co-exist (no matter who does not compile first) * If only throw E, you must try-catch catch exception e or throw an exception with throws E * If only return, then the CA
TCH segment Normal return value */int testEx00 () {int ret = 0;
try {int c = 12/0;
System.out.println ("testex,successfully");
return ret;
} catch (Exception e) {System.out.println ("testEx, Catch Exception");
ret =-1;
}finally{ret = 1; System.out.println ("testEx, finally;
return value= "+ ret);
Throw e;
return ret; }}/** * results: testEx, catch exception testEx, finally; Return Value=false false conclusion: In finally there is noReturn: Executes the finally statement, and then the return of the Catch, */Boolean testEx01 () {Boolean ret = true;
try {int c = 12/0;
System.out.println ("testex,successfully");
return true;
} catch (Exception e) {System.out.println ("testEx, Catch Exception");
ret = false;
return ret; }finally{System.out.println ("testEx, finally;
return value= "+ ret); }}/** * Results: * testEx, catch exception testEx, finally;
Return value=1 1 Conclusion: When there is return in finally: The finally statement and return are executed, the return of the catch is ignored */int testEx02 () {int ret = 0;
try {int c = 12/0;
System.out.println ("testex,successfully");
return ret;
} catch (Exception e) {ret =-1;
System.out.println ("testEx, catch Exception");
return ret;
}finally{ret = 1; System.out.println ("testEx, finally;
return value= "+ ret);
return ret;
}}/** * Compile can pass, * but run-time thrown exception (of course there is no return value) * @return * * * Boolean testEx03 () {Boolean ret = true;
try {int c = 12/0; System.out.println ("TeStex,successfully ");
return true;
} catch (Exception e) {System.out.println ("testEx, Catch Exception");
ret = false;
Throw e; }finally{System.out.println ("testEx, finally;
return value= "+ ret); }}/** * Compile cannot pass (must be added throws active throw exception, or Try-catch capture,) * But run-time throw exception (of course no return value) * @return * @throws Exception */Boolean
testEx031 () {Boolean ret = true;
try {int c = 12/0;
System.out.println ("testex,successfully");
return true;
} catch (Exception e) {System.out.println ("testEx, Catch Exception");
ret = false;
throw new Exception (e); }finally{System.out.println ("testEx, finally;
return value= "+ ret); }}/** * Results: * testEx, catch exception testEx, finally;
Return value=1 1 Conclusion: The function returns the return value normally in Finally, no exception, obviously the throw in catch is ignored */int testEx04 () {int ret = 0;
try {int c = 12/0;
System.out.println ("testex,successfully");
return ret; } catch (Exception e) {System.out.println ("testEx, Catch ExcePtion ");
ret =-1;
Throw e;
}finally{ret = 1; System.out.println ("testEx, finally;
return value= "+ ret);
return ret;
}} public static void Main (string[] args) {try {System.out.println (New TestException1 (). testEx0 ());
System.out.println (New TestException1 (). testEx00 ());
System.out.println (New TestException1 (). TESTEX01 ());
System.out.println (New TestException1 (). testEx02 ());
System.out.println (New TestException1 (). testEx03 ());
System.out.println (New TestException1 (). testEx031 ());
System.out.println (New TestException1 (). testEx04 ());
} catch (Exception e) {e.printstacktrace ();
}
}
}