本帖最后由 熊猫86 于 2013-12-13 00:41 编辑
- public class Demo{
- public static void main(String[] args) {
- Division d = new Division ();
-
- try {
- int x = d.div(4, 0);
- System.out.println(x);
- } catch(ArithmeticException e){
- System.out.println(e.getMessage());
- }
- }
- }
- class Division {
- int div(int a, int b) {
- try{
- return a/b;
- }catch(Exception e){
- throw new ArithmeticException("除数为零,程序出错");
- }
-
- }
- }
复制代码 输出结果为“除数为零,程序出错”,若想输出异常类型+信息,可以打印e.toString()
|