JAVA 自定义异常使用Exception
复制收展Javapackage com.algorithm.demo;
/**
* @Desc 累行客
* @Author luolei
* @Web http://www.leixingke.com/
* @Date 2020/11/17 15:05
*/
class BaseException extends Exception{}
class MyException extends BaseException {}
public class ExceptionTest1 {
public static void main(String[] args) throws Exception {
try {
try {
throw new MyException();
}
catch (MyException e) {
System.out.println("Caught MyException1");
BaseException newException = (BaseException)e;
throw newException;
}
}
catch (MyException e) {
System.out.println("Caught MyException2");
return;
}
catch (BaseException e) {
System.out.println("Caught BaseException3");
throw e;
}
finally {
System.out.println("finally");
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35