欢迎光临散文网 会员登陆 & 注册

java常见异常机制

2022-07-08 17:56 作者:虚云幻仙  | 我要投稿

/**
* 异常机制
*/

public class Test01 {
   public static void main(String[] args) {
        int a = 1/0;
       /*
       Exception in thread "main" java.lang.ArithmeticException: / by zero
      at Test01.main(Test01.java:3)
      异常 线程"main"  java.lang包中的ArithmeticException算数异常类  异常描述:by 0
      在 类Test01的main() Test01.java文件第3行

      main方法会创建一个主线程
      Process finished with exit code 1
      进程已完成 随着退出代码1 代码0是正常退出 代码1是异常退出
      Exception异常类 出现异常时会生成对应异常类的对象/抛出异常 并且中止程序
      抛出异常由系统创建
        */

       try{
           int a = 1/0;
       }catch (Exception e){
           e.printStackTrace();
       }
       int a = 0;
       System.out.println(a);
       /*
       java.lang.ArithmeticException: / by zero
      at Test01.main(Test01.java:15)
       0
       Process finished with exit code 0

       try尝试{}语句块 当出现异常catch抓住异常 (e)用引用变量e指向该异常对象 执行{}语句块 语句块内默认调用异常对象e.printStackTrace()打印stack栈trace跟踪信息
       如果没有异常发生会正常执行try内的语句 出现异常会生成异常对象交给catch并执行catch内的语句
       之后继续往后执行程序
       第一个a在try语句块内声明 出了语句块失效 需要重新声明
       将错误代码包在try catch中 运行时遇到异常也会继续完成try catch后面的内容 进程正常退出exit code 0
        */

       int b=0;
       if (b!=0){
           System.out.println(1/b);
       }
       //通过if判断来规避异常 使程序能继续往下执行

       String str = "1";
       System.out.println(str.charAt(1));
       /*
       charAt(index)方法的内容如下
           public char charAt(int index) {
               if ((index < 0) || (index >= value.length)) {
                   throw new StringIndexOutOfBoundsException(index);
               }
               return value[index];
           }
           返回值为char类型 判断索引index<0或者索引超过/等于字符串的数组的长度 throw抛出 new Exception构造器new一个对象
           if(false)返回index位的值

           Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 1
          at java.lang.String.charAt(String.java:658)
          at Test01.main(Test01.java:42)
          异常 在线程"main"中 异常对象是StringIndexOfBoundsException类 对应charAt()方法内的throw new xxxx  index out of超过 range范围 :1索引传的参数是1 即str.charAt(1)
          在 java.lang包String类charAt方法(String.java文件 :658行) 658行对应throw new StringIndexOutOfBoundsException(index);
          在 Test01.main方法(Test01.java文件:42行)
        */


       Object o =null;
       System.out.println(o.toString());
       /*
           Exception in thread "main" java.lang.NullPointerException
          at Test01.main(Test01.java:63)
          异常对象为 java.lang.NullPointerException类 null空pointer指针 引用变量o为null 找不到o的.toString()方法
        */

   }
}

class Equipment{ }
class Computer extends Equipment{}
class Television extends Equipment{}
class cast{
   public static void main(String[] args) {
       Equipment c = new Computer();
       Television t = (Television)c;
       /*
       Exception in thread "main" java.lang.ClassCastException: Computer cannot be cast to Television
      at cast.main(Test01.java:78)
           class类cast强制转换exception异常 Computer不能强制转换为Television
        */

       if (c instanceof Television){
           Television te = (Television) c;
       }
       //添加判断 c是否Television的实例

       int[] array = new int[5];
       System.out.println(array[5]);
       /*
       Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
      at cast.main(Test01.java:90)
      ArrayIndexOutOf数组索引超过Bounds界限Exception: 5 这里的5是array[5]的实参5  :冒号后面为异常的描述
        */


       File f = new File("C:\\CheckedException.txt");
       // "\t\n\r\'\"\\ " 斜杠加字符 escape character转义字符 C:\CheckedException.txt 改成C:/
        f.createNewFile();
       /*
       Unhandled exception:java.io.IOException 在编译阶段产生的异常 checked exception 还没到run阶段就会报异常
       未处理异常 创建文件就会报异常 需要包裹try catch来使用
        */

       try {
           f.createNewFile();
       } catch (IOException e) {
           //抛出异常时catch io异常
           throw new RuntimeException(e);
           //抛出一个新的运行时异常
       }
   }
}

java常见异常机制的评论 (共 条)

分享到微博请遵守国家法律