Tuesday, May 19, 2020
Fix Common Runtime Errors in Java With Careful Debugging
Consider the following segment of Java code, stored in a file called JollyMessage.java: // A jolly message is written to the screen! class Jollymessage { à à à public static void main(String[] args) { à à à à à //Write the message to the terminal window à à à à à System.out.println(Ho Ho Ho!); à à à } } At program execution, this code will produce a runtime error message. In other words, a mistake has been made somewhere, but the error wonââ¬â¢t be identified when the program is compiled, only when it is run. Debugging In the example above, notice that the class is called ââ¬Å"Jollymessageâ⬠whereas the filename is called JollyMessage.java. Java is case sensitive. The compiler wonââ¬â¢t complain because technically there is nothing wrong with the code. It will create a class file that matches the class name exactly (i.e., Jollymessage.class). When you run the program called JollyMessage, youll receive an error message because there is no file called JollyMessage.class. The error you receive when you run a program with the wrong name is: Exception in thread ââ¬Å"mainâ⬠java.lang.NoClassDefFoundError: JollyMessage (wrong name: JollyMessage).. Common Runtime-Error Solutions If your program compiles successfully but fails at execution, review your code for common mistakes: Mismatched single and double quotesMissing quotes for stringsIncorrect comparison operators (e.g., not using double equal signs to indicate assignment)Referencing objects that dont exist, or dont exist using the capitalization supplied in the codeReferencing an object that has no properties Working within integrated development environments like Eclipse can help you avoid typo-style errors. To debug productionalizedà Java programs, run your Web browsers debuggerââ¬âyou should see a hexadecimal error message that can assist in isolating the specific cause of the problem. In some situations, the problem may lie not in your code, but in your Java Virtual Machine. If the JVM is choking, it may kick out a runtime error despite the lack of a deficiency in the programs codebase. A browser debugger message will help isolate code-caused from JVM-caused errors.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.