Java认证辅导:java中的异常处理
来源:优易学  2011-11-15 18:43:52   【优易学:中国教育考试门户网】   资料下载   IT书店

  //除零异常,不做处理

  class test{

  int devide(int x,int y)

  {return x/y;}

  }

  class TestException{

  public static void main(String arg[])

  {

  new test().devide(3,0);

  System.out.println("the end!!");

  }

  }

  //除零异常,进行捕获

  class test{

  int devide(int x,int y)

  {return x/y;}

  }

  class CatchException{

  public static void main(String arg[])

  {

  try{

  new test().devide(3,0);

  }

  catch(ArithmeticException l)//exception类 声名 对象l

  {//catch中可以执行其他语句输出

  // l.printStackTrace() ;//printStackTrace

  // public void printStackTrace(PrintWriter s)

  // 将此 throwable 及其追踪输出到指定的 PrintWriter。

  //参数:

  //s - 用于输出的 PrintWriter

  //System.out.println(l.getMessage());//getMessage

  // public String getMessage()

  //返回此 throwable 的详细消息字符串。

  //返回:

  //此 Throwable 实例(可以为 null)的详细消息字符串。

  System.out.println("出错拉!!类型是:ArithmeticException");

  }

  System.out.println("the end!!");

  }

  }

  //除零异常,抛出异常

  class test {

  int devide(int x,int y)throws Exception//方法抛出异常!1抛给主调方法

  {return x/y;}

  }

  class TestException {

  public static void main(String arg[])throws Exception//主调方法不愿处理,又抛给了处理机!!

  //如想处理的话,

  //请参照CatchException中主函数的异常捕获!!

  {

  new test().devide(3,0);

  System.out.println("the end!!");

  }

  }

责任编辑:小草

文章搜索:
 相关文章
热点资讯
热门课程培训