Java exceptions throw throw and return

Source: Internet
Author: User

Tag: int on () off for false final Dex string index

package is common;

public class TestException {
Public TestException () {
}

Boolean testEx () throws Exception {
boolean ret = true;
try {
ret = testEx1 ();
} catch (Exception e) {
E.printstacktrace ();
System.out.println ("testEx, catch Exception");
ret = false;
Throw e;
} finally {
System.out.println ("testEx, finally; return value= "+ ret);
return ret;
}
}

Boolean testEx1 () throws Exception {
boolean ret = true;
try {
ret = TESTEX2 ();
if (!ret) {
return false;
}
System.out.println ("testEx1, at the end of Try");
return ret;
} catch (Exception e) {
E.printstacktrace ();
System.out.println ("testEx1, catch Exception");
ret = false;
Throw e;
} finally {
System.out.println ("testEx1, finally; return value= "+ ret);
return ret;
}
}

Boolean testEx2 () throws Exception {
boolean ret = true;
try {
int B = 12;
int C;
for (int i = 2; I >=-2; i--) {
c = b/i;
System.out.println ("i=" + i);
}
return true;
} catch (Exception e) {
E.printstacktrace ();
System.out.println ("testEx2, catch Exception");
ret = false;
An important throw can replace return if the back side finally is return and the method will not receive an abnormal
(That is, the exception thrown by the result of the catch is covered by the return in the finally). If there is no return, there is no exception to be thrown, and the exception that will be thrown will be received.
Throw e;
} finally {
System.out.println ("testEx2, finally; return value= "+ ret);
return ret;
throw new ArrayIndexOutOfBoundsException ();
}
System.out.println ("In-line");
return false;
}

public static void Main (string[] args) {
TestException testException1 = new TestException ();
try {
Testexception1.testex ();
} catch (Exception e) {
E.printstacktrace ();
System.out.println ("main");
}
}
}

Results

i=2
I=1
Java.lang.ArithmeticException:/By zero
At an abnormal. Testexception.testex2 (testexception.java:48)
At an abnormal. Testexception.testex1 (TESTEXCEPTION.JAVA:25)
At an abnormal. Testexception.testex (TESTEXCEPTION.JAVA:10)
At an abnormal. Testexception.main (testexception.java:71)
TESTEX2, catch exception
TESTEX2, finally; Return Value=false
Java.lang.ArrayIndexOutOfBoundsException
At an abnormal. Testexception.testex2 (testexception.java:62)
At an abnormal. Testexception.testex1 (TESTEXCEPTION.JAVA:25)
At an abnormal. Testexception.testex (TESTEXCEPTION.JAVA:10)
At an abnormal. Testexception.main (testexception.java:71)
TESTEX1, catch exception
TESTEX1, finally; Return Value=false
TestEx, finally; Return Value=false

Material http://www.cnblogs.com/to-creat/p/5688235.html

Java exceptions throw throw and return

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.