黑马程序员技术交流社区

标题: 请教一下这段代码的问题 [打印本页]

作者: 包晗    时间: 2012-7-17 16:01
标题: 请教一下这段代码的问题
本帖最后由 包晗 于 2012-7-17 23:45 编辑
[/quote]
[quote]class Demo
{
int div(int a,int b) //throws Exception
{
return a/b;
}
}

class ExceptionDemo
{
public static void main(String[] args)
{
Demo d = new Demo();

try
{
int x = d.div(4,0);
System.out.println("x="+x);
}
catch (Exception e)
{
System.out.println("除零啦");
//System.out.println(e.getmessage());
}
System.out.println("over");
}
}

请问大家 这段代码哪有问题...系统报错
但是我又另外从写了一遍 通过了 ......和大家一起找找问题

QQ截图20120717155858.gif (2.15 KB, 下载次数: 63)

百思不得其姐

百思不得其姐

作者: 康大玮    时间: 2012-7-17 16:13
class Demo
{
        int div(int a,int b)throws Exception//在功能上通过throws的关键字声明了该功能有可能会出现问题。
        {
                return a/b;
        }
}
作者: 周坤    时间: 2012-7-17 16:17
这段代码看不出问题,这是问题代码?
作者: 包晗    时间: 2012-7-17 16:31
周坤 发表于 2012-7-17 16:17
这段代码看不出问题,这是问题代码?

你放到电脑里 运行一下...
作者: 芦曦    时间: 2012-7-17 16:37
这段代码编译和运行都没有问题。
作者: 周坤    时间: 2012-7-17 16:37
包晗 发表于 2012-7-17 16:31
你放到电脑里 运行一下...

我的电脑上运行没有问题。。。
作者: 黑马刘涛    时间: 2012-7-17 16:42
没出现问题
作者: 包晗    时间: 2012-7-17 18:44
-0-  奇怪了...下午运行报错 现在好了.....
奇葩了
作者: 山水游客    时间: 2012-7-17 18:59
运行无错,不解释
建议:catch (ArithmeticException e) {
                        e.printStackTrace();
                }      根据异常查找原因




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2