当前位置: 代码迷 >> Java相关 >> [求助]看下这2段程序有错没有
  详细解决方案

[求助]看下这2段程序有错没有

热度:285   发布时间:2006-06-22 18:33:29.0
[求助]看下这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();
}

}
}




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


----------------解决方案--------------------------------------------------------
报了什么错吗?

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

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


----------------解决方案--------------------------------------------------------
我也。。。
不知道错在哪里啊。。。
"or"?
能说详细点么
----------------解决方案--------------------------------------------------------
or的意思是或者
你该用
if()
{}
else{} 来表示 而不是用or

其他地方貌似是没的错 待我再看看
----------------解决方案--------------------------------------------------------

----------------解决方案--------------------------------------------------------
//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();
}

}
}


----------------解决方案--------------------------------------------------------
谢谢几位的帮忙~
----------------解决方案--------------------------------------------------------
  相关解决方案