正在捕获Java畸形异常
当选择退出时,程序将调用以下URL, 所有其他按钮都工作正常,但当我单击“退出”时,什么也没有发生。正在捕获Java畸形异常,java,url,Java,Url,当选择退出时,程序将调用以下URL, 所有其他按钮都工作正常,但当我单击“退出”时,什么也没有发生。 我还得到: warning: unreachable catch clause catch( IOException iox ) { ^ thrown type MalformedURLException has already been caught 请帮忙 import java.awt.*; import java.lang.*; import java.applet.*
我还得到:
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
import java.awt.*;
导入java.lang.*;
导入java.applet.*;
导入java.util.Vector;
导入java.io.IOException;
导入java.net。*;
导入java.net.MalformedURLException;
导入java.applet.applet.*;
公共类阶段扩展画布实现可运行
{
公共类Stage2扩展了Applet
{
公共阶段2(){};
}
Stage2 Stage2=新Stage2();
图形=空;
图像的图像;
螺纹导体;
球[];
智力障碍;
int NUMBALSALLOCATED;
整数宽度;
内部高度;
int sleepy=5;
//----构造器
公共舞台(内部宽度、内部高度){
这个。宽度=宽度;
高度=高度;
挫折地面(颜色:黑色);
numBalls=0;
numBallsAllocated=10;
球=新球[数量已定位];
导体=零;
}//阶段结束构造函数
//-----设置和维护画布大小的方法
公共维度preferredSize(){
返回(新尺寸(宽度、高度));
}//preferredSize()的结尾
公共维度最小大小(){
返回(新尺寸(宽度、高度));
}//minimumSize()的结尾
//-----要调用的Bounce小程序对象的方法
公开作废开始(){
如果(导体==null){
导体=新螺纹(本“阶段”);
conductor.start();
}
否则{
for(int i=0;i0){
困倦——;
}
对于(int i=0;i关于警告:“unreachable catch clause catch(IOException iox)”,该try块中的任何内容都不会引发IOException。URL构造函数引发一个畸形的DurException,您正在捕获它。IOException的catch块不是必需的,并且永远无法执行(即,它是不可访问的)
作为补充说明,MalformedURLException扩展了IOException。第一个捕获也捕获IOException,因为MalformedURLException只是IOException的一个扩展。您可以安全地删除第二个捕获并从那里继续。其他人已经充分解释了编译错误的原因
我首先要提出一些意见,然后就如何诊断剩余问题提出一些建议:
1) 您运行的应用程序似乎有编译错误。某些IDE会允许您这样做,但这样做有点危险。关闭此选项并只运行编译的代码更安全
IDE(特别是Eclipse)通过生成方法代码来处理未编译的方法,该方法代码会引发未检查的异常,表示存在编译错误。如果从主线程调用此类方法,则将获得堆栈跟踪。如果从子线程调用,则可能看不到堆栈跟踪…这取决于线程是否具有“未捕获异常处理程序”。我怀疑这是在这里发生的
道德:
- 在运行代码之前修复编译错误
- 安装默认的未捕获异常处理程序,以便为因未检查异常而死亡的任何线程获取stacktrace。(这对于您当前所在的位置来说有点高级,但请稍后尝试记住它。)
2) MalformedURLException
的catch块正在挤压异常。也就是说,它正在捕获该异常,对其只字不提,然后继续处理,就好像没有发生什么不好的事情一样。在这种情况下,您需要知道是否抛出了该异常,因为它意味着您的程序中有一个bug;即,硬连接的URL是i不正确
道德:
- 不要挤压意外异常。任何意外异常至少应记录。(也可以允许未经检查的异常传播。如果您有一个不想在此时处理的已检查异常,您可以在方法签名中声明它,或者将其包装在未经检查的异常中,尽管前一种方法通常更好。)
- 如果您确定异常是预期的,不需要报告,请在catch块中添加注释以解释发生的情况
以下是我认为你应该做的事情,以向前迈进:
1) 修复编译错误。(我想你已经完成了)
2) 在catch子句中添加一些代码,以便(至少)向控制台发送stacktrace
如果这不起作用,那么:
3a)在调试器中运行代码
或
3b)添加一些traceprint,并临时添加catch(Throwable ex){ex.printStackTrace();}
以查看是否有其他未检查的异常被抛出
您观察到的“无事发生”有许多可能的原因,您需要找出哪些可能的原因是实际原因。如果我删除}catch(IOException iox){},我不会收到警告,但它仍然不起作用