Java MalformedURLException being caught



程序假定调用以下URL http://www.google.com当选择exit时,所有其他按钮工作正常,但当我点击"退出"什么也没有发生。
我还得到:

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

关于警告:"unreachable catch clause catch(IOException iox)" try块中没有抛出IOException。URL构造函数抛出malformmedurlexception,您正在捕捉它。IOException的catch块是不需要的,并且永远不会执行(即它是不可访问的)。

作为旁注,malformmedurlexception扩展了IOException.

第一个catch也捕获IOException,因为malformmeduriexception只是IOException的扩展。您可以安全地删除第二个catch并从那里继续。

其他人已经充分解释了编译错误的原因。


我想先提出一些意见,然后对如何诊断剩下的问题提出一些建议:

1)似乎您正在运行一个有编译错误的应用程序。有些IDE允许您这样做,但是这样做有些危险。关闭此选项并只运行可编译的代码要安全得多。

IDE(特别是Eclipse)通过生成方法代码来处理无法编译的方法,这些方法代码会抛出未检查的异常,表明存在编译错误。如果您从主线程调用这样的方法,您将获得堆栈跟踪。如果你从子线程调用它,你可能看不到堆栈跟踪…取决于线程是否有"未捕获的异常处理程序"。我怀疑这就是这里发生的事!道德:

运行代码之前修复编译错误
  • 安装一个默认的未捕获异常处理程序,这样你就可以对任何由于未检查异常而死亡的线程进行堆栈跟踪。(这是一个有点先进的地方,你目前在,但试着记住它以后。)
  • 2) MalformedURLException的捕获块是压缩异常。也就是说,它捕捉到这个异常,对它只字不提,然后继续进行,就好像什么坏事都没发生过。在这种情况下,你需要知道是否抛出了异常,因为这意味着你的程序中有bug;即硬连接的URL不正确。

    道德:

      不要压制意外的异常。任何意外异常都应该至少记录。(未经检查的异常也可以被允许传播。如果你有一个你不想处理的检查异常,你可以在方法签名中声明它,或者把它包装在一个未检查的异常中,尽管前一种方法通常更好。
    • 如果你认为异常是预期的,不需要报告,在catch块中添加注释来解释发生了什么。

    我认为你应该这样做:

    1)修复编译错误(我猜你已经这样做了)

    2)在catch子句中添加一些代码(至少)向控制台发送堆栈跟踪。

    如果不成功,则:

    在调试器中运行代码

    3b)添加一些跟踪记录,并且暂时添加catch (Throwable ex) {ex.printStackTrace();},以查看是否有其他未检查的异常被抛出。

    你所观察到的"什么都没发生"有很多可能的原因,你需要找出哪一个可能的原因是真正的原因。

    最新更新