Thursday, November 7, 2019

Fix Common Runtime Errors in Java With Careful Debugging

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.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.