JAVA异常

一、什么是异常

(1)定义:异常指的是Java程序在运行时可能出现的错误或者非正常的情况。
(2)异常的类型


(3)Throwable类常用的方法(获取异常信息)
               String getMessage()      返回异常的消息字符串
               String toString()             返回异常的简单信息描述
               void printStackTrace()    获取异常类和异常信息以及异常出现在程序中的位置,把信息输出到控制台
(4)运行时异常与编译时异常
        定义:在Exception 类中,除了RuntimeException类以外,其他子类都是编译时异常。
        处理编译时异常有两种方式:
                1.使用try....catch语句对异常进行捕获处理
                2.使用throws关键字声明抛出异常,由调用者对异常进行处理。
(5)常见的运行时异常
如图

二、异常处理及语法


(1)异常的产生及处理
如图:

public class Yichanglei {
    public static int  suanshu(int x, int y){
        int relust = x/y;
        return relust;
    }
    public static void main(String[] args) {
        int x=6,y=0;
          try {
              int sum = suanshu(x,y);
              System.out.println(sum);
          }
          catch (Exception e){
              System.out.println(e.getMessage());
              return;
          }finally {
              System.out.println("进入finslly代码块");
          }
        System.out.println("程序继续向下运行");
    }
}

(2)try....catch语句(用于捕获异常进行处理)
         其语法格式如下:
try{
       代码块(可能出现异常的代码)
}catch(Exception e){
       代码块//对异常进行处理的代码
}
 编写try....catch语句需要注意几点:
           1.try代码块是必须的。
           2.catch代码块可以有多个,但捕获父类异常的catch代码块必须位于捕获子类异常的catch代码块后面。
           3.catch代码块必须位于try代码块后面。


  (3)finally语句
语法格式如下:
try{
    代码块
    }catch(Exception e){
       代码块
    } finally{
       代码块
}     
(4)抛出异常------需要捕获
          java提供了throws关键字和throw关键字用于抛出异常。
          throws关键字(用于有可能发生异常的方法)
          语法格式如下:
修饰符 返回值类型 方法名(参数1,参数2,...)throws异常类1,异常类2,...{
方法体
}
          throw关键字(用于方法体内)
          使用throw关键字抛出异常的语法格式如下:
          throw ExceptionInstance;

public class Throw1 {
    public static void printAge(int age) throws Exception{
        if(age<=0){
            throw new Exception("输入的年龄有误,必须是正数");
        }
        else {
            System.out.println("此人年龄为"+age);
        }
    }
    public static void main(String[] args) {
        try{
            printAge(-18);
        }catch (Exception e){
            System.out.println("捕获的异常信息为"+e.getMessage());
        }
    }
}


(5)自定义异常类(必须继承Exception类或者其子类。)
自定义异常类的实例代码如下:
class jichengException extends Exception{
         public jichengException(){
super();
}
         public jichengException(String message){

super(message);
}
}
使用自定义异常类,需要用到throw关键字。其在方法中声明异常实例对象
语法格式如下:
throw Exception 异常对象
示例:

class ExtendsException extends Exception{
    public ExtendsException(){
        super();
    }
    public ExtendsException(String message){
        super(message);
    }
}
public class Xunhuan {
    public static int jisuan(int x, int y)throws ExtendsException{
        if(y<0){
            throw new ExtendsException("除数是负数");
        }
        int result = x/y;
        return result;
    }
    public static void main(String[] args) {
          try{
              int result = jisuan(5,0);
              System.out.println(result);
          }catch (ExtendsException e){
              System.out.println("捕获的异常信息为"+e.getMessage());
          }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值