A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 乔九 中级黑马   /  2012-9-4 22:30  /  1384 人查看  /  6 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

public class Demo
{
public static void main(String args[])
{
  int a=4;
  int b=0;
  System.out.println(a/b);//在不删了这句的情况下怎么样声明异常才能保证正常执行
  try
  {
   System.out.println(a/b);
  }
  catch(ArithmeticException e)
  {
   System.out.println("除数不可以为0");
  }
  finally
  {
   System.out.println("程序结束");
  }
}
}

评分

参与人数 1技术分 +1 收起 理由
王德升 + 1 赞一个!

查看全部评分

6 个回复

倒序浏览
public class Demo
{
public static void main(String args[])
{
  int a=4;
  int b=0;
  //可以将此语句写到一个方法当中去,对此异常可进行抛出处理或者检测捕获处理
  public static void Exception_1() throws ......
  {
          try
          {
                        System.out.println(a/b);//在不删了这句的情况下怎么样声明异常才能保证正常执行
          }
          catch (.........)
          {
                  ............
          }
        
  }

  try
  {
   System.out.println(a/b);
  }
  catch(ArithmeticException e)
  {
   System.out.println("除数不可以为0");
  }
  finally
  {
   System.out.println("程序结束");
  }
}
}
如图:

评分

参与人数 1技术分 +1 收起 理由
王德升 + 1 赞一个!

查看全部评分

回复 使用道具 举报
throw关键字通常用在方法体中,并且抛出一个异常对象。程序在执行到throw语句时立即停止,它后面的语句都不执行。通过throw抛出异常后,如果想在上一级代码中来捕获并处理异常,则需要在抛出异常的方法中使用throws关键字在方法声明中指明要跑出的异常;如果要捕捉throw抛出的异常,则必须使用try—catch语句。举例如下:
class MyException extends Exception { //创建自定义异常类
String message; //定义String类型变量
public MyException(String ErrorMessagr) {  //父类方法
       me= ErroMessagr;
}
public String getMessage(){   //覆盖getMessage()方法
  return message;
}
}
public class Captor { //创建类
static int quotient(int x,int y) throws MyException{//定义方法抛出异常
if(y < 0){  //判断参数是否小于0
         throw new MyException("除数不能是负数");//异常信息
}
return x/y;//返回值
}
public static void main(String args[]){ //主方法
try{ //try语句包含可能发生异常的语句
                int result = quotient(3,-1);//调用方法quotient()
    }catch (MyException e) { //处理自定义异常
  System.out.println(e.getMessage()); //输出异常信息
  }
    catch (ArithmeticException e) {
                   //处理ArithmeticException异常
  System.out.println("除数不能为0");//输出提示信息
  }
   catch (Exception e) { //处理其他异常
  System.out.println("程序发生了其他的异常");
                  //输出提示信息
  }
}
}

评分

参与人数 1技术分 +1 收起 理由
王德升 + 1 赞一个!

查看全部评分

回复 使用道具 举报
这种异常就是RuntimeException,称为未检查异常,可以完全不用异常处理机制,这种异常属于程序员的粗心引起的,完全可以通过编程的方式避免,
比如:
if(b!=0){
    System.out.println(a/b);
}
回复 使用道具 举报
[hr]public class Demo22
{
public static void main(String args[])//throws RuntimeException
{
   int a=4;
   int b=0;
   try
         {
             function();
        System.out.println(a/b);
         }
         catch(ArithmeticException e)
         {}//在不删了这句的情况下怎么样声明异常才能保证正常执行
   try
   {
    System.out.println(a/b);
  }
   catch(ArithmeticException e)
   {
   System.out.println("除数不可以为0");
   }
   finally
   {
   System.out.println("程序结束");

  }
}
           public static void function()//变化在这里
          {
             throw new ArithmeticException();
           }
}
如果要在不删除那句话的情况下可以将那句话抛出的异常封装成一个方法,让这个异常不能影响到那句话的运行。
回复 使用道具 举报
  1. public class Demo
  2. {
  3.         public static void main(String args[])
  4.         {
  5.                 int a=4;
  6.                 int b=0;
  7.                 //是给你显示出你的问题,不是处理, 处理的代码你是有的。
  8.                 System.out.println(a+"/"+b);       //在不删了这句的情况下怎么样声明异常才能保证正常执行
  9.                
  10.                 try
  11.                 {
  12.                         System.out.println(a/b);
  13.                 }
  14.                 catch(ArithmeticException e)
  15.                 {
  16.                         System.out.println("除数不可以为0");
  17.                         e.printStackTrace();
  18.                 }
  19.                 finally
  20.                 {
  21.                         System.out.println("程序结束");
  22.                 }
  23.         }
  24. }
复制代码
其实ArithmeticException是RuntimeException的子类,所以不需要进行声明的,  当运行到System.out.println(a/b)的时候try() catch()代码块就会扑捉到并进行处理。
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马