欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2023-12-20 22:29:58
c语言时用if...else...来控制异常,java语言所有的异常都可以用一个类来表示,不同类型的异常对应不同的子类异常,每个异常都对应一个异常类的对象。 java异常...

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

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类及其子类(实例讲解)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。

上一篇:

下一篇: