正在捕获Java MalformedURLException

时间:2011-11-18 22:24:12

标签: java url

程序假设调用以下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

3 个答案:

答案 0 :(得分:6)

关于警告:“无法访问的catch子句catch(IOException iox)”该try块中没有任何内容抛出IOException。 URL构造函数抛出MalformedURLException并且您正在捕获它。 IOException的catch块不是必需的,永远不能执行(即无法访问)。

作为旁注,MalformedURLException扩展了IOException。

答案 1 :(得分:1)

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

答案 2 :(得分:0)

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


我先做一些评论,然后就如何诊断剩下的问题提出一些建议:

1)您似乎正在运行具有编译错误的应用程序。有些IDE会让你这样做,但这样做有点危险。关闭此选项并仅运行编译的代码会更安全。

IDE(特别是Eclipse)处理不能通过生成方法代码进行编译的方法,该方法代码抛出未经检查的异常,表示存在编译错误。如果从主线程调用这样的方法,您将获得堆栈跟踪。如果从子线程调用它,则可能看不到堆栈跟踪...取决于线程是否具有“未捕获的异常处理程序”。我怀疑这发生在这里!

道德:

  • 在运行代码之前修复编译错误
  • 安装默认的未捕获异常处理程序,以便为因未经检查的异常而死亡的任何Thread获取堆栈跟踪。 (对于您目前所处的位置,这有点高级,但请稍后再尝试记住它。)

2)MalformedURLException的catch块正在挤压异常。也就是说,它正在捕获该异常,一言不发,然后继续进行,好像什么都没发生。在这种情况下,你需要知道是否抛出该异常,因为这意味着你的程序中有一个错误;即硬连线的URL不正确。

道德:

  • 不要压制意外的异常。任何意外的异常都应至少记录 。 (也可以允许传播未经检查的异常。如果您有一个在此时不想处理的已检查异常,您可以在方法签名中声明它,或者将其包装在未经检查的异常中,尽管前一种方法通常更好。)
  • 如果您确定异常预期并且不需要报告,请在catch块中添加注释以解释发生的情况。

以下是我认为你应该做的事情:

1)修复编译错误。 (你这样做我收集了)

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

如果那不起作用,那么:

3a)在调试器中运行代码

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

您观察到的“没有任何反应”有很多可能的原因,您需要弄清楚哪些可能的原因是实际原因。