zoukankan      html  css  js  c++  java
  • 吴裕雄--天生自然JAVA异常处理学习笔记:异常的其它概念

    class MyException extends Exception{    // 自定义异常类,继承Exception类
        public MyException(String msg){
            super(msg) ;    // 调用Exception类中有一个参数的构造方法,传递错误信息
        }
    };
    public class DefaultException{    
        public static void main(String args[]){
            try{
                throw new MyException("自定义异常。") ;     // 抛出异常
            }catch(Exception e){
                System.out.println(e) ;
            }
        }
    }
    public class RuntimeExceptionDemo01{
        public static void main(String args[]){
            String str = "123" ;    // 定义字符串,全部由数字组成
            int temp = Integer.parseInt(str) ; // 将字符串变为int类型
            System.out.println(temp * temp) ;    // 计算乘方
        }
    };
    public class Test{
        public static void main(String args[]){
            int x[] = {1,2,3} ;    // 定义数组,长度为3
            assert x.length==0 : "数组长度不为0" ;    // 此处断言数组的长度为0
        }
    };
    public class ThrowDemo01{
        public static void main(String args[]){
            try{
                throw new Exception("自己抛着玩的。") ;    // 抛出异常的实例化对象
            }catch(Exception e){
                System.out.println(e) ;
            }
        }
    };
    class Math{
        public int div(int i,int j) throws Exception{    // 定义除法操作,如果有异常,则交给被调用处处理
            System.out.println("***** 计算开始 *****") ;
            int temp = 0 ;    // 定义局部变量
            try{
                temp = i / j ;    // 计算,但是此处有可能出现异常
            }catch(Exception e){
                throw e ;
            }finally{    // 不管是否有异常,都要执行统一出口
                System.out.println("***** 计算结束 *****") ;
            }
            return temp ;
        }
    };
    public class ThrowDemo02{
        public static void main(String args[]){
            Math m = new Math() ;
            try{
                System.out.println("除法操作:" + m.div(10,0)) ;
            }catch(Exception e){
                System.out.println("异常产生:" + e) ;
            }
        }
    };
    class Math{
        public int div(int i,int j) throws Exception{    // 定义除法操作,如果有异常,则交给被调用处处理
            int temp = i / j ;    // 计算,但是此处有可能出现异常
            return temp ;
        }
    };
    public class ThrowsDemo01{
        public static void main(String args[]){
            Math m = new Math() ;        // 实例化Math类对象
            try{
                System.out.println("除法操作:" + m.div(10,2)) ;
            }catch(Exception e){
                e.printStackTrace() ;    // 打印异常
            }
        }
    };
    class Math{
        public int div(int i,int j) throws Exception{    // 定义除法操作,如果有异常,则交给被调用处处理
            int temp = i / j ;    // 计算,但是此处有可能出现异常
            return temp ;
        }
    };
    public class ThrowsDemo02{
        // 在主方法中的所有异常都可以不使用try...catch进行处理
        public static void main(String args[]) throws Exception{
            Math m = new Math() ;        // 实例化Math类对象
            System.out.println("除法操作:" + m.div(10,0)) ;
        }
    };
  • 相关阅读:
    使用shutdown命令实现局域网内远程关机、重启整蛊他人
    在foxmail和outlook中设置QQ邮箱、gmail邮箱、新浪邮箱、微软邮箱、网易邮箱等的方法
    万能驱动助理篡改主页为2345的解决办法
    巧用UserAgent来解决浏览器的各种问题
    各大浏览器保存密码的文件
    使用代理软件之后其他软件不能联网的解决方法
    windows xp/7/8/8.1/10安全模式详解和系统修复讲解
    VirtualBox更改默认路径
    Virtualbox中不能为虚拟机打开一个新任务的原因及解决方法
    xampp打开显示缺少运行库的解决方法
  • 原文地址:https://www.cnblogs.com/tszr/p/12435831.html
Copyright © 2011-2022 走看看