test1: 调用者main抛出异常
public class ExceptionTest {
public void method() throws Exception {
// 将异常抛出,由调用这个方法的方法去处理这个异常,如果main方法也将异常抛出,则交给Java虚拟机来处理
System.out.println("Hello World");
// 抛出异常
throw new Exception();
}
public static void main(String[] args) throws Exception {
// main方法选择将异常继续抛出
CartTestt test = new CartTestt();
test.method(); // main方法需要对异常进行处理
System.out.println("this line is after exception");
}
}
output1:
Hello World
Exception in thread “main” java.lang.Exception
at com.ibm.commerce.cw.cart.test.CartTestt.method(CartTestt.java:10)
at com.ibm.commerce.cw.cart.test.ExceptionTest.main(ExceptionTest.java:18)
test2:调用者main try catch 处理异常
public class ExceptionTest {
public void method() throws Exception {
// 将异常抛出,由调用这个方法的方法去处理这个异常,如果main方法也将异常抛出,则交给Java虚拟机来处理
System.out.println("Hello World");
// 抛出异常
throw new Exception();
}
public static void main(String[] args) {
// main方法选择将异常继续抛出
CartTestt test = new CartTestt();
try {
test.method();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
System.out.println("this is finally line");
}
// main方法需要对异常进行处理
System.out.println("this line is after exception");
}
}
output2:
Hello World
java.lang.Exception
at com.ibm.commerce.cw.cart.test.CartTestt.method(CartTestt.java:10)
at com.ibm.commerce.cw.cart.test.ExceptionTest.main(ExceptionTest.java:16)
this is finally line
this line is after exception
可以看出,如果调用者main 不捕获 exception的话,如有exception,则其后的语句不会被执行;如果调用者main 捕获 了exception的话,如有exception,捕获处理之后的语句也会被执行。
test3 try catch + return
public class ExceptionTest {
public void method() throws Exception {
// 将异常抛出,由调用这个方法的方法去处理这个异常,如果main方法也将异常抛出,则交给Java虚拟机来处理
System.out.println("Hello World");
// 抛出异常
throw new Exception();
}
public static void main(String[] args) {
// main方法选择将异常继续抛出
CartTestt test = new CartTestt();
try {
test.method();
return;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
System.out.println("this is finally line");
}
// main方法需要对异常进行处理
System.out.println("this line is after exception");
}
}
output3
Hello World
java.lang.Exception
at com.ibm.commerce.cw.cart.test.CartTestt.method(CartTestt.java:10)
at com.ibm.commerce.cw.cart.test.ExceptionTest.main(ExceptionTest.java:15)
this is finally line
this line is after exception
test4
public class ExceptionTest {
public void method() {
try {
System.out.println("entering try block");
} catch (Exception e) {
System.out.println("entering catch block");
e.printStackTrace();
} finally {
System.out.println("entering finally block");
}
System.out.println("this line is after exception");
}
public static void main(String[] args) {
ExceptionTest test = new ExceptionTest();
test.method();
}
}
output4
entering try block
entering finally block
this line is after exception
test5 try catch + return
public class ExceptionTest {
public void method() {
try {
System.out.println("entering try block");
return;
} catch (Exception e) {
System.out.println("entering catch block");
e.printStackTrace();
} finally {
System.out.println("entering finally block");
}
System.out.println("this line is after exception");
}
public static void main(String[] args) {
ExceptionTest test = new ExceptionTest();
test.method();
}
}
output5
entering try block
entering finally block
可以看出,当try block中return时,仍然先执行finally block后再return。
test6 + System.exit(0)
public class ExceptionTest {
public void method() {
try {
System.out.println("entering try block");
System.exit(0);//终止java虚拟机
} catch (Exception e) {
System.out.println("entering catch block");
e.printStackTrace();
} finally {
System.out.println("entering finally block");
}
System.out.println("this line is after exception");
}
public static void main(String[] args) {
ExceptionTest test = new ExceptionTest();
test.method();
}
}
output6
entering try block
可以看出,当try block中有System.exit(0)时,程序直接return,不再执行finally block。