I have the following simple code:
package test;
import javax.swing.*;
class KeyEventDemo {
static void main(String[] args) {
UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
}
}
It generates the following error message:
KeyEventDemo.java:7: unreported exception java.lang.ClassNotFoundException; must be caught or declared to be thrown
UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
^
1 error
Does anybody know what is wrong?
Actually, the message is self explaining:
UIManager.setLookAndFeelthrows a bunch of checked exceptions that thus need to be caught (with a try/catch block) or declared to be thrown (in the calling method).So either surround the call with a try/catch:
Or add a throws declaration:
If you don’t want to handle each of them in a specific way, this can be made less verbose by using the
Exceptionsupertype:Or with a throws declaration (note that this convey less information to the caller of the method but the caller being the JVM here, it doesn’t really matter in this case):