About Try-catch, throw, finally in the execution order of the exception

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

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.