public String testTryCatchFinally() {
try {
System.out.println("try开始执行");
System.out.println("try执行完了");
return "try中的返回值";
} catch (Exception e) {
System.out.println("捕获到的异常");
} finally {
System.out.println("finally执行完了");
}
return "最终中的返回值";
}
try开始执行
try执行完了
finally执行完了
try中的返回值
public String testTryCatchFinally2() {
try {
System.out.println("try开始执行");
String str = null;
str.length();
System.out.println("try执行完了");
return "try中的返回值";
} catch (Exception e) {
System.out.println("捕获到的异常");
} finally {
System.out.println("finally执行完了");
}
return "最终中的返回值";
}
Throw 几种情况 第0种try开始执行
捕获到的异常
finally执行完了
最终中的返回值
public class Person {
public String high() {
int a = 5;
a = a / 0;
return "tall";
}
}
@Test
public void test11() {
Person person = new Person();
person.high();
}
java.lang.ArithmeticException: / by zero at exception.throwandthrows.Person.high(Person.java:14) at exception.throwandthrows.ThrowsAndThrowMainTest.test11(ThrowsAndThrowMainTest.java:63) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.junit.runners.model.frameworkMethod$1.runReflectiveCall(frameworkMethod.java:50) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.frameworkMethod.invokeExplosively(frameworkMethod.java:47) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) at org.junit.runners.ParentRunner.run(ParentRunner.java:363) at org.junit.runner.JUnitCore.run(JUnitCore.java:137) at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69) at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33) at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:235) at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:54) Process finished with exit code -1第1种
public class Person {
public String weight(int num) {
if (num > 90) {
throw new MyException(400, "overWeight");
}
return "light";
}
}
@Test
public void test(){
Person person = new Person();
person.weight(91);
}
exception.throwandthrows.MyException: overWeight at exception.throwandthrows.Person.weight(Person.java:7) at exception.throwandthrows.ThrowsAndThrowMainTest.test(ThrowsAndThrowMainTest.java:19) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.junit.runners.model.frameworkMethod$1.runReflectiveCall(frameworkMethod.java:50) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.frameworkMethod.invokeExplosively(frameworkMethod.java:47) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) at org.junit.runners.ParentRunner.run(ParentRunner.java:363) at org.junit.runner.JUnitCore.run(JUnitCore.java:137) at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69) at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33) at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:235) at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:54) Process finished with exit code -1第2种
可以包装一下而已
public class Person {
public String weight(int num) {
if (num > 90) {
throw new MyException(400, "overWeight");
}
return "light";
}
}
@Test
public void test2(){
try {
Person person = new Person();
person.weight(91);
} catch (Exception e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
}
overWeight exception.throwandthrows.MyException: overWeight at exception.throwandthrows.Person.weight(Person.java:7) at exception.throwandthrows.ThrowsAndThrowMainTest.test2(ThrowsAndThrowMainTest.java:25) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.junit.runners.model.frameworkMethod$1.runReflectiveCall(frameworkMethod.java:50) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.frameworkMethod.invokeExplosively(frameworkMethod.java:47) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) at org.junit.runners.ParentRunner.run(ParentRunner.java:363) at org.junit.runner.JUnitCore.run(JUnitCore.java:137) at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69) at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33) at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:235) at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:54) Process finished with exit code 0第3种
public class Person {
public String weight(int num) {
if (num > 90) {
throw new MyException(400, "overWeight");
}
return "light";
}
}
@Test
public void test3() {
try {
Person person = new Person();
person.weight(91);
} catch (MyException e) {
System.out.println(e.getCode() + e.getMessage());
} catch (Exception e) {
System.out.println(e.getMessage());
throw new MyException(500, e.getMessage());
}
}
400overWeight Process finished with exit code 0第4种
public class Person {
public String weight(int num) {
if (num > 90) {
throw new MyException(400, "overWeight");
}
return "light";
}
}
public class Doctor {
public void check() {
try {
Person person = new Person();
person.weight(91);
} catch (MyException e) {
throw e;
} catch (Exception e) {
throw new MyException(500, e.getMessage());
}
}
}
@Test
public void test4() {
try {
Doctor doctor = new Doctor();
doctor.check();
} catch (MyException e) {
System.out.println(e.getCode() + e.getMessage());
} catch (Exception e) {
System.out.println(e.getMessage());
throw new MyException(500, e.getMessage());
}
}
400overWeight Process finished with exit code 0第5种
public class Person {
public String high() {
int a = 5;
a = a / 0;
return "tall";
}
}
public class Doctor {
public void check2() {
try {
Person person = new Person();
person.high();
} catch (MyException e) {
throw e;
} catch (Exception e) {
throw new MyException(500, e.getMessage());
}
}
}
@Test
public void test12() {
try {
Doctor doctor = new Doctor();
doctor.check2();
} catch (MyException e) {
System.out.println(e.getCode() + e.getMessage());
} catch (Exception e) {
}
}
500/ by zero Process finished with exit code 0第6种
public String weight(int num) {
if (num > 90) {
throw new MyException(400, "overWeight");
}
return "light";
}
public class Doctor {
public void check3() {
Person person = new Person();
person.weight(91);
}
}
@Test
public void test13() {
try {
Doctor doctor = new Doctor();
doctor.check3();
} catch (MyException e) {
System.out.println(e.getCode() + e.getMessage());
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
400overWeight Process finished with exit code 0第7种
public class Person {
public String high() {
int a = 5;
a = a / 0;
return "tall";
}
}
public class Doctor {
public void check4() {
Person person = new Person();
person.high();
}
}
@Test
public void test14() {
try {
Doctor doctor = new Doctor();
doctor.check4();
} catch (MyException e) {
System.out.println(e.getCode() + e.getMessage());
} catch (Exception e) {
System.out.println(e.getMessage());
}
}



