小编Nat*_*ahn的帖子

为什么,当调用一个方法时,它是否说我需要在我已经捕获它时捕获异常?

我有一个循环一些音频的类:

public class PlayGameMusic {
    public static void main(String[] args) throws Exception {
        try{
            AudioInputStream inputStream = AudioSystem.getAudioInputStream(new File("\\Users\\natal\\Desktop\\programs\\APCS\\Fill the Boxes.wav"));
            Clip clip = AudioSystem.getClip();
            clip.open(inputStream);
            clip.loop(Clip.LOOP_CONTINUOUSLY);
            Thread.sleep(10000);
        }
        catch(IOException error){System.out.println("IO Exception Error");}
        catch(InterruptedException error){System.out.println("InterruptedException");}
        catch(Exception error){System.out.print("System.out.println("Exception");");}
     }
}
Run Code Online (Sandbox Code Playgroud)

我可以编译这个方法,并且编译器不会报告任何错误(我已经使用print语句对其进行了测试).但是,当我尝试PlayGameMusic在另一个类中调用上面的class()的main方法时......

public class RunGame
{
    public static void main(String[] args)
    {
       PlayGameMusic.main(null);
    }
}
Run Code Online (Sandbox Code Playgroud)

...我得到这个编译器错误:

unreported exception java.lang.Exception; must be caught or declared to be thrown
Run Code Online (Sandbox Code Playgroud)

我正在捕捉可能的异常,并且PlayGameMusic该类在单独运行时有效.为什么我不能从另一个班级打电话呢?

java exception javasound audioinputstream

0
推荐指数
1
解决办法
53
查看次数

标签 统计

audioinputstream ×1

exception ×1

java ×1

javasound ×1