zoukankan      html  css  js  c++  java
  • Java中的异常处理

     1 class YiChang{
     2     public static void main(String[] args){
     3         A a=new A();
     4         a.show();
     5     }
     6 }
     7 
     8 class A{
     9     int[] i={1,2,3};
    10     public void show(){
    11         System.out.println(i[3]);
    12     }
    13 }

    运行上面代码,会抛出这样的错误。

    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
    at A.show(YiChang.java:11)
    at YiChang.main(YiChang.java:4)

    除了让虚拟机为我们抛出异常外,我们也可以人为的控制异常。

    我们有两种方法:throws和throw。throws是申明异常,throw是抛出问题对象,下面让我们具体看看这两种是怎么实现的。

    首先我们在函数中加入throws(如下在10行函数处)

     1 class YiChang{
     2     public static void main(String[] args){
     3         A a=new A();
     4         a.show();
     5     }
     6 }
     7 
     8 class A{
     9     int[] i={1,2,3};
    10     public void show()throws Exception{ 
    11         System.out.println(i[3]);
    12     }
    13 }

    此时的运行结果为

    YiChang.java:4: 错误: 未报告的异常错误Exception; 必须对其进行捕获或声明以便抛出

    让我们再看看throw,此时会用到try(){}catch(){}finally(){},try用来放可能发生问题的代码,catch用来捕获问题,finally用来放一定会执行的语句。

     1 class YiChang{
     2     public static void main(String[] args){
     3         A a=new A();
     4         try{
     5             a.show();
     6         }
     7         catch(Exception e){       //无处不在的多态
     8             System.out.println("wrong");
     9         }
    10     }
    11 }
    12 
    13 class A{
    14     int[] i={1,2,3};
    15     public void show(){
    16         System.out.println(i[3]);
    17     }
    18 }

    输出结果为wrong

    此时我们就会想,能不能对程序中的错误实现认为的控制呢?这个时候就会用到了throw

    class YiChang{
        public static void main(String[] args){
            A a=new A();
            try{
                a.show(3);
            }
            catch(Exception e){
                System.out.println("wrong");
                System.out.println("message"+e.getMessage());
                System.out.println("toString"+e.toString());
                e.printStackTrace();
            }
        }
    }

    class A{
        int[] i={1,2,3};
        public void show(int j) throws Exception{
            if(j>=3){
                throw new Exception("j beyond the length");
            }
            System.out.println(i[j]);
        }
    }

    此时我们需要看看e里面究竟有什么方法。这里写几个常用的方法:toString()、getMessage()、printStackTrace()。下面是运行结果

    wrong
    messagej beyond the length
    toStringjava.lang.Exception: j beyond the length
    java.lang.Exception: j beyond the length
        at A.show(YiChang.java:20)
        at YiChang.main(YiChang.java:5)
  • 相关阅读:
    Simple Automated Backups for MongoDB Replica Sets
    [转] matlab获取时间日期
    Matlab与C++混合编程 编写独立外部应用程序时出现“无法定位序数3906于动态链接库LIBEAY32.dll上”错误
    Visual Studio 控制台应用程序 同时使用OpenCV和matlab mat文件操作
    [转] Matlab与C++混合编程(依赖OpenCV)
    OpenCV 64位时 应用程序无法正常启动0x000007b 问题解决
    LinkedBlockingQueue多线程测试
    rdlc报告vs2008编辑正常,在vs2012在对错误的编辑
    SD3.0四个协议解读
    链队列
  • 原文地址:https://www.cnblogs.com/Jc-zhu/p/4067960.html
Copyright © 2011-2022 走看看