zoukankan      html  css  js  c++  java
  • java 08 异常

    Exception

    java.lang.Object 所有类的超类

    Throwable:可抛出

                       Error                  //错误

                      Exception           //异常

                      throw                  //抛出异常的语句(用着函数内,后面跟异常对象)

                      throws                //声明抛出异常时使用的关键字(用在函数上,后面跟异常类名)

    try

    {

    需要检测的代码;

    }

    catch(异常类 变量)

    {

    异常处理的代码

    }

    finally

    {

    一定会执行的代码

    }

    finally代码块只有一种情况下不会被执行,就是之前执行了System.exit(0)

    ----------------------------------------------------------------------------

    class ThrowableDemo{
    public static void main(String[] agrs){
    float r = dvide(4,0);
    System.out.println(r);
    //int [] arr ={121,1245,45};
    //int [] arr =new int[4];
    //异常数据
    int [] arr = null;
    System.out.println(getlength(arr));
    }

    public static float dvide(int a ,int b){
    return (float)a / b;
    }
    //计算数字的长度

    public static int getlength(int[] arr ){
    //return arr.length;
    int len = 0;
    try {
    //执行到return 说明没有问题,如果有问题不会执行到return,直接进catch
    len = arr.length;
    return len;
    }
    catch(Exception e){
    //e.getMessage()打印出错的信息
    System.out.println("出错了" + e.getMessage());
    return len;
    }
    //如果try出问题,进catch ,如果么有问题最后进finally
    finally {
    System.out.println("程序执行完毕");

    }

    }

    }

    error

    -----------------------------------------------------------------

    class ThrowableDemo2{
    public static void main(String[] agrs){
    try{
    sayHello();
    }
    // Error 错误体系
    //Exception 异常体系
    catch(Error e){
    System.out.println("报错了"+e.getMessage());

    }

    }

    public static void sayHello(){
    System.out.println("hello word");
    sayHello();
    }

    }

    异常类

    --------------------------------------------

    class Throwableclass{
    public static void main(String[] agrs){
    Person p = new Person();
    try{
    p.setAge(50);
    }

    catch(Exception e){
    ((Agetoobigexception) e).printerror();
    }

    }

    }


    class Person{
    private int age;
    public int getAge(){
    return age;
    }


    //在方法上声明一下抛出异常
    public void setAge(int age) throws Agetoobigexception {
    if(age >200){
    //如果>200抛出异常
    throw new Agetoobigexception();
    }
    this.age =age;
    }

    }



    //异常类
    class Agetoobigexception extends Exception{
    //定义异常返回消息
    private String info;
    //构造函数
    public Agetoobigexception(String info){
    this.info =info;
    }
    //重载构造函数,无参数
    public Agetoobigexception(){
    this("年龄太大");
    }

    //打印错误信息
    public void printerror (){
    System.out.println(info);

    }

    }

  • 相关阅读:
    中国剩余定理(CRT) & 扩展中国剩余定理(ExCRT)总结
    各种求逆元
    A*(A_star)搜索总结
    线段树总结
    C++的STL
    Unable to make the session state request to the session state server处理方法
    判断UserAgent是否来自微信
    VS2010 EntityFramework Database First
    VS2010类似Eclipse文件查找功能-定位到
    Newtonsoft.Json随手记
  • 原文地址:https://www.cnblogs.com/simly/p/10256106.html
Copyright © 2011-2022 走看看