栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Day15 异常的捕获和处理

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Day15 异常的捕获和处理

9.1.1异常概述 1、认识异常

异常是指程序运行过程中发生的不正常事件,它会中断程序的运行。

2、java异常体系结构

9.1.2java异常处理机制 1、异常处理。

2、使用throws声明抛出异常。
public class Person {
    private String name = "";   // 姓名
    private int age = 0;   // 年龄
    private String sex = "男";  // 性别
    public void setSex(String sex) throws Exception {
        if ("男".equals(sex) || "女".equals(sex))
            this.sex = sex;
        else {
            //自定义抛出异常
            throw new Exception("性别必须是男或者女!");
        }
    }
}
3、使用throw抛出异常。
public void setAge(int age)throws wrongAgeException {
        if(age<0||age>120){
            throw new wrongAgeException("年龄异常");
    }
        this.age = age;
    }
4、使用try-catch处理异常。
public static void main(String[] args) {
        Exception1 s=new Exception1();
        try {
            s.setName("马化腾");
        } catch (wrongNameException e) {
            e.printStackTrace();
        }

e.printStackTrace() ;      //表示在命令行打印异常信息在程序中出错的位置及原因。

5、使用try-catch-finally块处理异常。

6、使用多重catch
class MultiCatch {
    public static void main(String args[]) {
        try {
            int a = args.length;
            System.out.println("a = " + a);
            int b = 42 / a;
            int c[] = { 1 };
        } catch(ArithmeticException e) {
            System.out.println("Divide by 0: " + e);
        } catch(ArrayIndexOutOfBoundsException e) {
            System.out.println("Array index oob: " + e);
        }
        System.out.println("After try/catch blocks.");
    }
}
7、自定义异常。

public class wrongAgeException extends Exception {
    public wrongAgeException(String message){
        super(message);
    }
}
public class wrongNameException extends Exception {
    public wrongNameException(String message){
        super(message);
    }
}
public class Exception1 {
    private String name;
    private int age;
    public String getName() {
        return name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age)throws wrongAgeException {
        if(age<0||age>120){
            throw new wrongAgeException("年龄异常");
    }
        this.age = age;
    }
    public void setName(String name)throws wrongNameException {
        if(name.equals("马化腾")){
            throw new wrongNameException("马化腾正在提刀来的路上");
        }
        this.name = name;
    }
    public static void main(String[] args) {
        Exception1 s=new Exception1();
        try {
            s.setName("马化腾");
        } catch (wrongNameException e) {
            e.printStackTrace();
        }
        try {
            s.setAge(-1);
        } catch (wrongAgeException e) {
            e.printStackTrace();
        }
        System.out.println(s.getAge());
    }
}
8、异常链。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/424927.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号