To be clear, you are not doing any exception handling. You are exception ostriching... putting your head in the sand and hoping the exceptions don't happen, or go away.
@tim has the right suggestions but has not gone quite far enough.
Yes, you should report exceptions for debugging, and to inform the user, but, you can do it a whole lot better than what you have. Java supports what's called 'multi-catch', which can simplify your code to:
public void music(String song) {
try {
File soundFile = new File(song);
AudioInputStream sound = AudioSystem.getAudioInputStream(soundFile);
// load the sound into memory (a Clip)
DataLine.Info info = new DataLine.Info(Clip.class, sound.getFormat());
Clip clip = (Clip) AudioSystem.getLine(info);
clip.open(sound);
// play the sound clip
clip.start();
} catch (IOException
| LineUnavailableException
| UnsupportedAudioFileException e) {
Logger.error("Unable to read clip. See exception", e);
}
}
Note that your code has other problems as well, though. You are creating multiple instances of classes that have resources that should be closed:
- AudioInputStream is a resource that should be closed, but you don't close it.
- Clip also should be closed.
As a result of your poor handling of those resources you could end up in a situation where resources are 'hanging around' longer than they are needed, leading to poor memory use, and other complications. Your system unfortunately delegates-and-forgets about those resources, so fixing the situation is not easily possible with your current code.