当选择退出时,程序假定调用下面的URL http://www.google.com,所有其他按钮都工作正常,但是当我点击“退出”时,什么也没有发生。
我还得到了:
warning: unreachable catch clause
catch( IOException iox ) {
^
thrown type MalformedURLException has already been caught请帮帮忙
import java.awt.*;
import java.lang.*;
import java.applet.*;
import java.util.Vector;
import java.io.IOException;
import java.net.*;
import java.net.MalformedURLException;
import java.applet.Applet.*;
public class Stage extends Canvas implements Runnable
{
public class Stage2 extends Applet
{
public Stage2() {};
}
Stage2 stage2= new Stage2();
Graphics offGraphics = null;
Image offImage;
Thread conductor;
Ball balls[];
int numBalls;
int numBallsAllocated;
int width;
int height;
int sleepy = 5;
// ----- constructor
public Stage( int width,int height ) {
this.width = width;
this.height = height;
setBackground( Color.black );
numBalls = 0;
numBallsAllocated = 10;
balls = new Ball[numBallsAllocated];
conductor = null;
} // end of Stage constructor
//----- methods for setting and maintaining the size of the canvas
public Dimension preferredSize() {
return( new Dimension( width,height ));
} // end of preferredSize()
public Dimension minimumSize() {
return( new Dimension( width,height ));
} // end of minimumSize()
//----- methods for the Bounce applet object to call
public void start() {
if ( conductor == null ) {
conductor = new Thread(this, "Stage");
conductor.start();
}
else {
for ( int i = 0; i < numBalls; i++ ) {
balls[i].start();
}
conductor.resume();
}
} // end of start()
public void stop() {
for( int i = 0; i < numBalls; i++ ) {
balls[i].stop();
}
conductor.suspend();
} // end of stop()
public void addBall() {
Color color = chooseColor( numBalls );
Ball ball = new Ball( "Ball "+(numBalls+1),color,this,sleepy );
System.out.println( "here "+ball.toString() );
// enlarge ball array if necessary.
if ( numBalls == numBallsAllocated ) {
Ball newBalls[];
numBallsAllocated *= 2;
newBalls = new Ball[numBallsAllocated];
System.arraycopy( balls,0,newBalls,0,numBalls );
balls = newBalls;
}
balls[numBalls] = ball;
numBalls++;
ball.start();
} // end of addBall()
//----- methods for conductor thread to run
public void run() {
while ( true ) {
repaint();
try {
Thread.sleep( sleepy );
}
catch ( InterruptedException ix ) {
break;
}
}
} // end of run()
public void faster() {
if ( sleepy > 0 ) {
sleepy--;
}
for ( int i=0; i<numBalls; i++ ) {
balls[i].setSleepy( sleepy );
}
System.out.println( "faster... " + sleepy );
} // end of faster()
public void slower() {
sleepy++;
for ( int i=0; i<numBalls; i++ ) {
balls[i].setSleepy( sleepy );
}
System.out.println( "slower... " + sleepy );
} // end of slower()
public void exit()
{
try {
URL url = new URL( "http://www.google.com" );
stage2.getAppletContext().showDocument( url );
}
catch( MalformedURLException murlx ) {
}
catch( IOException iox ) {
}
} // end of exit()
// we have overridden update() instead of paint() since the
// background does not need to be cleared when doing double
// buffering.
public synchronized void update( Graphics g ) {
if ( offGraphics == null ) {
offImage = createImage( width,height );
offGraphics = offImage.getGraphics();
}
offGraphics.setColor( getBackground() );
offGraphics.fillRect( 0,0,width,height );
for (int i = 0; i < numBalls; i++) {
balls[i].paint( offGraphics );
}
g.drawImage( offImage, 0, 0, this );
} // end of update()
//----- private methods.
private Color chooseColor( int i ) {
switch (i % 5) {
case 0: return Color.white;
case 1: return Color.red;
case 2: return Color.blue;
case 3: return Color.green;
case 4: return Color.yellow;
}
// Not reached
return Color.white;
} // end of chooseColor()
} // end of Stage class发布于 2011-11-19 07:05:55
其他人已经充分解释了编译错误的原因。
我首先要做一些评论,然后对如何诊断剩余的问题提出一些建议:
1)您运行的应用程序似乎存在编译错误。有些IDE可以让你这样做,但是这样做有点危险。关闭此选项并仅运行编译的代码要安全得多。
IDE(特别是Eclipse)通过生成方法代码来处理不能编译的方法,方法代码抛出未检查的异常,表明存在编译错误。如果你从主线程调用这样的方法,你会得到一个堆栈跟踪。如果从子线程调用它,可能看不到堆栈跟踪...这取决于线程是否具有“未捕获的异常处理程序”。我怀疑这是在这里发生的!
道德:
2) MalformedURLException的catch块正在挤压异常。也就是说,它捕获该异常,对其只字不提,然后继续进行,就好像没有发生任何不好的事情一样。在这种情况下,您需要知道是否抛出了异常,因为这意味着您程序中有一个bug;即硬连接的URL是不正确的。
道德:
下面是我认为你应该做的事情:
1)修复编译错误。(我猜你已经做到了)
2)在catch子句中添加一些代码,以便(至少)向控制台发送堆栈跟踪。
如果这不起作用,那么:
3a)在调试器中运行代码
或
3b)添加一些traceprint,并临时添加catch (Throwable ex) {ex.printStackTrace();},看看是否抛出了其他未检查的异常。
你观察到的“什么都没有发生”有许多可能的原因,你需要找出哪些可能的原因才是真正的原因。
发布于 2011-11-19 06:29:46
关于警告:"unreachable子句catch( unreachable子句catch ( IOException iox ) )“try块中的任何内容都不会抛出IOException。URL构造函数抛出一个MalformedURLException,您可以捕捉到它。IOException的catch块不是必需的,并且永远不能执行(即它是不可访问的)。
顺便说一句,MalformedURLException扩展了IOException。
发布于 2011-11-19 06:33:40
第一个捕获也捕获了IOException,因为MalformedURIException只是IOException的扩展。您可以安全地删除第二个捕获物,并从那里继续。
https://stackoverflow.com/questions/8189911
复制相似问题