| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 412 人关注过本帖
标题:[求助]看下这2段程序有错没有
只看楼主 加入收藏
小斑点
Rank: 1
等 级:新手上路
帖 子:11
专家分:0
注 册:2006-6-18
收藏
 问题点数:0 回复次数:6 
[求助]看下这2段程序有错没有
/* This program contains an error.
A subclass must come before its superclass in
a series of catch statements. If not,
unreachable code will be created and a
compile-time error will result.
*/
class SuperSubCatch {
public static void main(String args[]) {
try {
int a = 0;
int b = 42 / a;
} catch(Exception e) {
System.out.println("Generic Exception catch.");
}
/* This catch is never reached because
ArithmeticException is a subclass of Exception. */
catch(ArithmeticException e) { // ERROR - unreachable
System.out.println("This is never reached.");
}
}
}



class TempClass
{
public int X(int [] a) throws TempException
{
if (a.length<>23)
throw new TempException("the number of the array is "+a.length);
or
if (y<=0)
throw new TempException("Divisor is "+y);
int Y;
Y=a.length;
System.out.println("the number of the array is "+a.length);
return Y;
}
}


class TempException extends Exception
{
public TempException(String msg)
{
super(msg);
}
}

class Test
{
public static void main(String [] args)
{

int a = args.length;
System.out.println("a = " + a);
int b = 23 / a;
int c[] = { 1 };
c[23] = 99;
}

catch(TempException e)
{
System.out.println(e.getMessage());
System.out.println("异常");
e.printStackTrace();
}

}
}




帮我看下哪里有错好么
谢谢了~

2006-06-22 18:33
水影月圆
Rank: 4
等 级:贵宾
威 望:11
帖 子:738
专家分:0
注 册:2005-8-2
收藏
得分:0 
报了什么错吗?

在第2段代码里 可以使用 "or" 吗?

[此贴子已经被作者于2006-6-22 18:50:57编辑过]


子非鱼,安知鱼之江湖?子非我,安知我之功夫 http://20681.
2006-06-22 18:49
小斑点
Rank: 1
等 级:新手上路
帖 子:11
专家分:0
注 册:2006-6-18
收藏
得分:0 
我也。。。
不知道错在哪里啊。。。
"or"?
能说详细点么

2006-06-22 19:09
水影月圆
Rank: 4
等 级:贵宾
威 望:11
帖 子:738
专家分:0
注 册:2005-8-2
收藏
得分:0 
or的意思是或者
你该用
if()
{}
else{} 来表示 而不是用or

其他地方貌似是没的错 待我再看看

子非鱼,安知鱼之江湖?子非我,安知我之功夫 http://20681.
2006-06-22 19:12
千里冰封
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:灌水之王
等 级:版主
威 望:155
帖 子:28477
专家分:59
注 册:2006-2-26
收藏
得分:0 

可惜不是你,陪我到最后
2006-06-22 19:35
private
Rank: 1
等 级:新手上路
帖 子:110
专家分:0
注 册:2006-5-1
收藏
得分:0 
//1。第一段代码有错子类异常应在父类异常前捕捉。

class SuperSubCatch {
public static void main(String args[]) {
try {
int a = 0;
int b = 42 / a;
} catch(Exception e) {
System.out.println("Generic Exception catch.");
}
/* This catch is never reached because
ArithmeticException is a subclass of Exception. */
catch(ArithmeticException e) { // ERROR - unreachable
System.out.println("This is never reached.");
}
}
}

//2。第二段代码太乱了改成

class TempClass
{
public int X(String [] a) throws TempException
{
if (a.length<23)
throw new TempException("the number of the array is "+a.length);

/*
int Y;
Y=a.length;
if (y<=0)
throw new TempException("Divisor is "+y);
System.out.println("the number of the array is "+a.length);
return Y;
*/
}
}


class TempException extends Exception
{
public TempException(String msg)
{
super(msg);
}
}

class Test
{
public static void main(String [] args)
{
try{
int a = args.length;
System.out.println("a = " + a);
int b = 23 / a;
int c[] = { 1 };
c[23] = 99;
}
catch( ArithmeticException e )
{
System.out.println("被零除了");
}
catch( ArrayIndexOutOfBoundsException e )
{
System.out.println("下标越界");
}


try{
TempClass temp =new TempClass();
temp.X(args);
}
catch(TempException e)
{
System.out.println(e.getMessage());
System.out.println("异常");
e.printStackTrace();
}

}
}


2006-06-22 19:51
小斑点
Rank: 1
等 级:新手上路
帖 子:11
专家分:0
注 册:2006-6-18
收藏
得分:0 
谢谢几位的帮忙~

2006-06-22 21:09
快速回复:[求助]看下这2段程序有错没有
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.015822 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved