gpt4 book ai didi

Java MalformedURLException 被捕获

转载 作者:行者123 更新时间:2023-12-01 07:38:43 24 4
gpt4 key购买 nike

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

最佳答案

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

顺便说一句,MalformedURLException 扩展了 IOException。

关于Java MalformedURLException 被捕获,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8189911/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com