twc
Posts: 129
Nickname: twc
Registered: Feb, 2004
|
|
Re: Compiler vs. Run-Time Exceptions
|
Posted: Feb 11, 2004 6:27 AM
|
|
> Are there some general rules one can follow to know when > an error will be compile-time vs. run-time? Some examples > are very obvious, but others are not.
Compile-time errors mean that you typed something in that was wrong. But run-time errors usually involve code that only causes problems in certain circumstances. For example
String userName = javax.swing.JOptionPane.showInputDialog(null, "Enter Your Name");
System.out.println(userName.toUpperCase());
If I forget to write javax.swing. before JOptionPane, that will cause a compile-time error since the compiler won't be able to find the class. (Another option is to use an import statement.)
As written, the code will work fine UNLESS the user closes the dialog or presses the Cancel button. That is a run-time exception. The code wasn't typed in wrong, but it needs code that will catch errors. Often, beginner examples leave out error-checking code to avoid the extra complexity.
I hope this helps.
tom
|
|