Java异常Exception类及其子类(实例讲解)-创新互联

C语言时用if...else...来控制异常,Java语言所有的异常都可以用一个类来表示,不同类型的异常对应不同的子类异常,每个异常都对应一个异常类的对象。

宜黄ssl适用于网站、小程序/APP、API接口等需要进行数据传输应用场景,ssl证书未来市场广阔!成为创新互联建站的ssl证书销售渠道,可以享受市场价格4-6折优惠!如果有意向欢迎电话联系或者加微信:18980820575(备注:SSL证书合作)期待与您的合作!

Java异常处理通过5个关键字try、catch、finally、throw、throws进行管理。基本过程是用try包住要监视的语句,如果在try内出现异常,则异常会被抛出,catch中捕获抛出的异常并做处理,finally一定会完成未尽事宜。

练习:

package com.swift;

public class Exception1
{
  public static void main(String args[]){
    System.out.println("=========计算开始=========");
    try{
      int i=Integer.parseInt(args[0]);
      int j=Integer.parseInt(args[1]);
      int temp=i/j;
      System.out.println("计算结果:"+ temp);
    }catch(ArithmeticException e){
      System.out.println("出现了数学异常 "+ e);
    }catch(ArrayIndexOutOfBoundsException e){
      System.out.println("出现了数组异常 "+ e);
    }catch(NumberFormatException e){
      System.out.println("出现了格式异常 "+ e);
    }catch(Exception e){
      System.out.println("其他异常 "+e);
    }finally{
      System.out.println("不管是否有异常,我都执行。");
    }
    System.out.println("=========计算结束=========");
  }
}

网站名称:Java异常Exception类及其子类(实例讲解)-创新互联
URL标题:http://scyanting.com/article/ccppji.html