源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

Java异常 Exception类及其子类(实例讲解)

  • 时间:2022-03-05 03:59 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:Java异常 Exception类及其子类(实例讲解)
C语言时用if...else...来控制异常,Java语言所有的异常都可以用一个类来表示,不同类型的异常对应不同的子类异常,每个异常都对应一个异常类的对象。 Java异常处理通过5个关键字try、catch、finally、throw、throws进行管理。基本过程是用try包住要监视的语句,如果在try内出现异常,则异常会被抛出,catch中捕获抛出的异常并做处理,finally一定会完成未尽事宜。 [b]练习: [/b]
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类及其子类(实例讲解)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持编程素材网。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部