Search code examples
javajavasound

Getting unreported exception Exception when trying to call function to playback a .WAV


I am trying to call the following function, which should reproduce a sound:

public static void emitirSonido() throws Exception {

URL url = new URL(
    "http://www.wavsource.com/snds_2014-05-25_4108314609264195/animals/chicken.wav");  //URL

Clip clip = AudioSystem.getClip();
// getAudioInputStream()

AudioInputStream ais = AudioSystem.
    getAudioInputStream( url );

clip.open(ais);
clip.loop(Clip.LOOP_CONTINUOUSLY);

SwingUtilities.invokeLater(new Runnable() {


    public void run() {

        JOptionPane.showMessageDialog(null, "ok!");
    }
});
}

for that matter I am using a simple function call:

emitirSonido();

But I keep getting:

error: unreported exception Exception; must be caught or declared to be thrown
            emitirSonido();
                        ^

To be honest I don't know what else to try because if I remove the throws it just gives me like 7 more errors, originally the code is from javasound.info

Any help would be great. Thanks


Solution

  • emitirSonido is declared as throwing an Exception...

    emitirSonido() throws Exception
    

    When you call this method, you must either catch it or re-throw it, for example...

    try {
        emitirSonido()
    } catch (Exception exp) {
        exp.printStackTrace();
    }
    

    As a side point, throwing Exception is not a really great idea, it's better to provide the actual exceptions that are thrown, as callers might like to pick and choose which ones they handle and which ones they re-throw

    Take a closer look at the Exceptions trail for more details