tags:

views:

36

answers:

1

Hi,

I was looking at some of the examples from the Deitel and Deitel's book for Programming in Java, and one of the first examples is a very simple Swing display. Hence this snippet of code :

   import javax.swing.*; 

    public class cdea {     

public static void main(String args[]){
                    JOptionPane.showMessageDialog(null,"\"Welcome to Java Programming!\"");
                    System.exit(0);

          //end method main
    }
    }

I read some stuff regarding how one can get native UI look and feel by using

UIManager.setLookAndFeel(
            UIManager.getSystemLookAndFeelClassName());

to the program. However, when I just add this to my main method, it gives me a host of errors. Specifically :

Multiple markers at this line - Unhandled exception type IllegalAccessException - Unhandled exception type InstantiationException - Unhandled exception type ClassNotFoundException - Unhandled exception type UnsupportedLookAndFeelException

However when I use it as part of a try/catch exception loop (is that what it is called?) as detailed on http://stackoverflow.com/questions/1590863/getting-java-applications-to-look-native-on-windows-how , I get the program running properly.

Could anyone tell me in simple language why this is so? As in, why can't I directly get the System look and feel; why do I have to use it with exception handling? I'm new to Java, and OOP in general, so I'm sorry if I'm being too simplistic.

+1  A: 

Java has two major types of exceptions: checked exceptions and unchecked exceptions.

You are required to have code to deal with checked Exceptions. As a general rule, documentation for a method will list which checked exceptions it can throw.

In this case, UIManager.setLookAndFeel is described as this:

public static void setLookAndFeel(String className)
                           throws ClassNotFoundException,
                                  InstantiationException,
                                  IllegalAccessException,
                                  UnsupportedLookAndFeelException

You have two choices: Handle each of these exceptions in separate catch blocks, or handle them in a single catch block that catches Exception. In this case, I believe it's OK to handle it in a single block, as Swing will default back to the cross platform look and feel if it really can't change L&Fs.

try {
    UIManager.setLookAndFeel(
              UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
    // Any handling you want to do here, possibly logging
    // Optionally, you could just do... nothing.
}

Note: There is a second setLookAndFeel method that throws just one exception type, but you're not using that one.

R. Bemrose
Thank you for your lucid explanation! :) So does this basically mean I have to always see whether a particular method in a Java library has checked exceptions before I use it?
drachenfels
Unfortunately, yes. If you use an IDE, it may have a shortcut to add a try/catch block for you (in Eclipse, it's Alt-Shift-Z then the first option on the menu that pops up).
R. Bemrose