常用异常

异常

定义

广义:

一切不正常的情况

狭义:

程序在运行时出现的不正常情况,经过异常处理机制后,程序可以继续向下执行

异常的体系

Throwable
Error:

程序运行时出现错误,例如内存不够用,程序自身无法解决

Exception:

异常

运行时异常:

直接或间接继承了RuntimeException,在编译期间不强制要求处理

编译时(检查)异常

直接或间接继承了Exception,与RuntimeException无关,在编译期间强制要求处理

package com.ff.note.yichang;

public class Yichang {
    public static void main(String[] args) {
//        数组索引越界            ArrayIndexOutOfBoundsException
       /* int[] ints=new int[3];
        ints[3]=21;*/

//        算数异常      ArithmeticException
       /* int a=521;
        int b=0;
        System.out.println(a/b);*/

//        空指针异常     NullPointerException
        /*String str=null;
        System.out.println(str.length());*/

//        类型转换异常        ClassCastException
        /*Object o="abc";
        Integer i=(Integer)o;
        System.out.println(i);*/

//        数字格式化异常       NumberFormatException
        /*int c=new Integer("xiaoxiao");
        System.out.println(c);*/
    }
}

异常处理

try

package com.ff.note.yichang;

public class YichangChuli {
//    一个try可对应多个catch           try支持嵌套处理           异常子类在上父类在下,若父类在上则下面的异常不执行
    public static void main(String[] args) {
        try {
            int a=10;
            int b=0;
            System.out.println(a/b);

            int[] ints=new int[4];
            ints[4]=5;

            String s=null;
            s.length();


        }catch (ArrayIndexOutOfBoundsException a){
            System.out.println("数组索引越界");
        }catch (ArithmeticException b){
            System.out.println("算数异常");
        }catch (Exception c){
            System.out.println("其他异常");
        }
        System.out.println("后面的代码");
        try {
            int a=0;
            int b=12;
            System.out.println(b/a);
            try{
                int[] ints=new int[4];
                ints[8]=520;
            }catch (ArrayIndexOutOfBoundsException array){
                System.out.println("数组索引越界");//给用户进行提示
            }
        }catch (ArithmeticException a){
            System.out.println("算数异常");
        }
    }
}

给开发人员使用

package com.ff.note.yichangchuli;

import java.io.FileNotFoundException;
import java.io.PrintWriter;

public class ChuLiKaiFa {
    public static void main(String[] args) throws FileNotFoundException {
        try{
            int a=10;
            int b=0;
            System.out.println(a/b);
        }catch (ArithmeticException a){
            //System.out.println("除数为0");//给用户显示
            //a.getMessage();//打印错误原因
            //a.printStackTrace();//打印异常信息到控制台
            
//            记录日志,异常信息写在文件中
            PrintWriter printWriter=new PrintWriter("D:\\log.txt");
            a.printStackTrace(printWriter);
            printWriter.flush();
            printWriter.close();
        }
        System.out.println("后面的代码");
    }
}

finally

无论是否异常代码都能执行

依赖于try,catch

没有catch时,若try中有异常,程序会执行finally中的代码,finally中的代码执行完后程序才会终止

try,catch中有return时finally中的代码也会执行,而且是先于return执行

package com.ff.note.yichangchuli;

public class Finally {
    public static void main(String[] args) {
//        有catch
        try {
            int[] a = new int[3];
            a[3] = 3;
        } catch (ArrayIndexOutOfBoundsException a) {
            System.out.println("索引越界");
            a.printStackTrace();
        } finally {
            System.out.println("是小小丫");
        }
        System.out.println("后面的代码");

//          无catch
        /*try {
            int a=10;
            int b=0;
            System.out.println(a/b);
        }finally {
            System.out.println("就这就这");
        }
        System.out.println("啊啊啊啊");*/

//        有return时
        Finally f=new Finally();
        System.out.println(f.Chufa(12,3));
        System.out.println(f.Chufa(10,0));

    }
    public int Chufa(int a,int b){
        try {
            return a/b;
        }catch (ArrayIndexOutOfBoundsException e){
            return -1;
        }finally {
            System.out.println("dadadada");
        }
        //System.out.println("123");
    }
}

Throws

作为方法的声明,声明某方法中可能会出现某种异常

可以声明多个,一般多为编译期(检查)异常

在main中用throws无意义,故在main中只能用try/catch

此方法不处理异常,只是交给方法调用处进行处理

任何方法都可以使用包括抽象方法abstract

package com.ff.note.yichangchuli.throwsDemo;

import java.io.FileNotFoundException;
import java.io.UnsupportedEncodingException;

public class Throws {
    public static void main(String[] args) {
        Throws t=new Throws();
        try {
            t.demo1();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }
    public void demo1() throws UnsupportedEncodingException, FileNotFoundException {
        demo();
    }
    public void demo() throws UnsupportedEncodingException, FileNotFoundException {
        //"abc".getBytes("utf-8");
        try {
            "abc".getBytes("utf-8");
        }finally {
            System.out.println("qqweqw");
        }
    }
}

package com.ff.note.yichangchuli.throwsDemo;

import java.io.FileNotFoundException;

public abstract class ChongXie {
    public abstract void mestord()throws Exception;

    public abstract void mesord1()throws ArrayIndexOutOfBoundsException;
}




package com.ff.note.yichangchuli.throwsDemo;

import java.io.FileNotFoundException;

public class ChongXieSon extends ChongXie{
//      对于编译期异常,子类声明的异常类型小于、等于父类的异常类型(方法的重写中)
    @Override
    public void mestord() throws FileNotFoundException {

    }

//      对于运行期异常,子类父类不进行要求
    @Override
    public void mesord1() throws RuntimeException {

    }
}

Throw

当不满足某种条件时,在方法中显示的抛出一个异常对象

在抽象方法中不能使用

抛出的是一个异常类的实例化对象

使用throw后 要么使用try catch 要么使用throws

package com.ff.note.yichangchuli.Throw;

public class ThrowDemo {
    public static void main(String[] args) {
        ThrowDemo td=new ThrowDemo();
        try {
            td.grades(101);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public String grades(int grade) throws Exception {
        if (grade<0||grade>100){
            throw new Exception("成绩不合法");
        }
        if (grade>85){
            return "优秀";
        }else {
            return "不优秀";
        }
    }
}

自定义异常类

根据实际业务逻辑的需要,来指定一种场景下的异常类

继承自API中的标准异常类的直接或间接子类

package com.ff.note.yichangchuli.Throw;
/*
    自定义异常,当成绩不合法时(成绩大于100或者成绩小于0)会抛出此异常
 */
public class GradesException extends Exception{
    public GradesException() {

    }

    public GradesException(String message) {
        super(message);
    }
}




package com.ff.note.yichangchuli.Throw;

public class Byoneself {
    public static void main(String[] args) {
        Byoneself b=new Byoneself();
        try {
            b.grades(103);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public String grades(int grade) throws Exception {
        if (grade<0||grade>100){
            throw new GradesException("成绩不合法");
        }
        if (grade>85){
            return "优秀";
        }else {
            return "不优秀";
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值