做一个大方法,要调用很多小方法,如果小方法中出现问题,要在大方法中打印日志,说明是哪个地方出了问题。
一般是通过判断返回值标志是否执行正确,但是有的程序是void。这样就可以try catch一下,如果出现问题,向上抛出异常到主方法中。
第一次:
public static void main(String[] args) {
for (int i = 0; i < 3; i++) {
try {
System.out.println(i + "循环");
inNewException();
System.out.println("我在异常后面---执行");
} catch (Exception e) {
e.printStackTrace();
System.out.println("外面抛异常");
continue;
}
}
}
private static void inNewException() throws TestException {
try {
int a = 0;
int b = 20 / a;
} catch (Exception e) {
e.printStackTrace();
System.out.println("内部抛异常");
}
}
打印日志:
0循环
java.lang.ArithmeticException: / by zero
at com.StudentController.inNewException(StudentController.java:482)
at com.StudentController.main(StudentController.java:460)
内部抛异常
我在异常后面---执行
1循环
java.lang.ArithmeticException: / by zero
at com.StudentController.inNewException(StudentController.java:482)
at com.StudentController.main(StudentController.java:460)
内部抛异常
我在异常后面---执行
2循环
java.lang.ArithmeticException: / by zero
at com.StudentController.inNewException(StudentController.java:482)
at com.StudentController.main(StudentController.java:460)
内部抛异常
我在异常后面---执行
通过日志会发现虽然inException方法异常了,但是主方法还会继续往下运行,在主方法中的try catch是捕获不到inException方法的异常的。(当然这里也可以通过返回值去判断内部方法是否异常了)
第二次:
public static void main(String[] args) {
for (int i = 0; i < 3; i++) {
try {
System.out.println(i + "循环");
inException();
System.out.println("我在异常后面---执行");
} catch (Exception e) {
e.printStackTrace();
System.out.println("外面抛异常");
continue;
}
}
}
private static void inException() throws TestException {
try {
int a = 0;
int b = 100 / a;
} catch (Exception e) {
throw new TestException("inException exception");
}
}
执行日志:
0循环
com.TestException: inException exception
外面抛异常
1循环
外面抛异常
2循环
外面抛异常
at com.StudentController.inException(StudentController.java:475)
at com.StudentController.main(StudentController.java:460)
com.TestException: inException exception
at com.StudentController.inException(StudentController.java:475)
at com.StudentController.main(StudentController.java:460)
com.TestException: inException exception
at com.StudentController.inException(StudentController.java:475)
at com.StudentController.main(StudentController.java:460)
可以看到都是在主方法中的catch 捕获到了异常,并且由于catch中的continue;for循环未中断,但是只执行到异常的位置,System.out.println("我在异常后面---执行");这个未打印出来。
第三次:
public static void main(String[] args) {
for (int i = 0; i < 3; i++) {
try {
inException();
inNewException();
System.out.println("我在异常后面---执行");
} catch (TestException e) {
System.out.println("外面抛异常:"+e.getMessage());
continue;
}catch (Exception e) {
System.out.println("外面抛异常:"+e.getMessage());
continue;
}
}
}
private static void inException() throws TestException {
try {
int a = 0;
int b = 100 / a;
} catch (Exception e) {
throw new TestException("inException exception");
}
}
private static void inNewException() throws TestException {
try {
int a = 0;
int b = 100 / a;
} catch (Exception e) {
throw new TestException("inNewException exception");
}
}
日志:
外面抛异常:inNewException exception
外面抛异常:inNewException exception
外面抛异常:inNewException exception
调用n个方法,每个方法抛出自定义异常,但是message 不同 ,在主方法捕获异常时,可以通过e.getMessage不同,判断具体是哪个方法出现了问题。
一般是通过判断返回值标志是否执行正确,但是有的程序是void。这样就可以try catch一下,如果出现问题,向上抛出异常到主方法中。
第一次:
public static void main(String[] args) {
for (int i = 0; i < 3; i++) {
try {
System.out.println(i + "循环");
inNewException();
System.out.println("我在异常后面---执行");
} catch (Exception e) {
e.printStackTrace();
System.out.println("外面抛异常");
continue;
}
}
}
private static void inNewException() throws TestException {
try {
int a = 0;
int b = 20 / a;
} catch (Exception e) {
e.printStackTrace();
System.out.println("内部抛异常");
}
}
打印日志:
0循环
java.lang.ArithmeticException: / by zero
at com.StudentController.inNewException(StudentController.java:482)
at com.StudentController.main(StudentController.java:460)
内部抛异常
我在异常后面---执行
1循环
java.lang.ArithmeticException: / by zero
at com.StudentController.inNewException(StudentController.java:482)
at com.StudentController.main(StudentController.java:460)
内部抛异常
我在异常后面---执行
2循环
java.lang.ArithmeticException: / by zero
at com.StudentController.inNewException(StudentController.java:482)
at com.StudentController.main(StudentController.java:460)
内部抛异常
我在异常后面---执行
通过日志会发现虽然inException方法异常了,但是主方法还会继续往下运行,在主方法中的try catch是捕获不到inException方法的异常的。(当然这里也可以通过返回值去判断内部方法是否异常了)
第二次:
public static void main(String[] args) {
for (int i = 0; i < 3; i++) {
try {
System.out.println(i + "循环");
inException();
System.out.println("我在异常后面---执行");
} catch (Exception e) {
e.printStackTrace();
System.out.println("外面抛异常");
continue;
}
}
}
private static void inException() throws TestException {
try {
int a = 0;
int b = 100 / a;
} catch (Exception e) {
throw new TestException("inException exception");
}
}
执行日志:
0循环
com.TestException: inException exception
外面抛异常
1循环
外面抛异常
2循环
外面抛异常
at com.StudentController.inException(StudentController.java:475)
at com.StudentController.main(StudentController.java:460)
com.TestException: inException exception
at com.StudentController.inException(StudentController.java:475)
at com.StudentController.main(StudentController.java:460)
com.TestException: inException exception
at com.StudentController.inException(StudentController.java:475)
at com.StudentController.main(StudentController.java:460)
可以看到都是在主方法中的catch 捕获到了异常,并且由于catch中的continue;for循环未中断,但是只执行到异常的位置,System.out.println("我在异常后面---执行");这个未打印出来。
第三次:
public static void main(String[] args) {
for (int i = 0; i < 3; i++) {
try {
inException();
inNewException();
System.out.println("我在异常后面---执行");
} catch (TestException e) {
System.out.println("外面抛异常:"+e.getMessage());
continue;
}catch (Exception e) {
System.out.println("外面抛异常:"+e.getMessage());
continue;
}
}
}
private static void inException() throws TestException {
try {
int a = 0;
int b = 100 / a;
} catch (Exception e) {
throw new TestException("inException exception");
}
}
private static void inNewException() throws TestException {
try {
int a = 0;
int b = 100 / a;
} catch (Exception e) {
throw new TestException("inNewException exception");
}
}
日志:
外面抛异常:inNewException exception
外面抛异常:inNewException exception
外面抛异常:inNewException exception
调用n个方法,每个方法抛出自定义异常,但是message 不同 ,在主方法捕获异常时,可以通过e.getMessage不同,判断具体是哪个方法出现了问题。