Java Reference
In-Depth Information
recognizing error types
Errors are almost unavoidable when programming. Just as when you are writing an essay and
sometimes make typos or misuse words, you will make occasional mistakes when programming in
Java. These mistakes can be referred to as bugs, errors, or exceptions. Here you will find them clas-
sified into three main categories: syntax errors, runtime errors, and logical errors. In general, syntax
errors are the easiest to find and correct while logical errors are the most difficult.
identifying syntax errors
Syntax errors are the programming equivalent of spelling and grammar mistakes in natural
languages. Syntax errors include the following examples:
Misspelled class, variable, or method names
Misspelled keywords
Missing semicolons
Missing return type for methods
Out of place or mismatched parentheses and brackets
Undeclared or uninitialized variables
Incorrect format of loops, methods, or other structures
In the following code example, see how many syntax errors you can spot:
public class errors {
public static vod main(String[] args) {
age = 30;
int retirementFund = 10000;
int yearsInRetirement = 0;
String name = "David Johnson",
for (int i = age; <= 65; ++){
recalculate(retirementFund,0.1);
}
int monthlyPension = retirementFund/yearsInRetirement/12
System.out.printline(name + " will have $" + monthlyPension
+ " per month for retirement."];
}
public static recalculate(fundAmount, rate){
fundAmount = fundAmount*(1+rate);
}
}
}
You probably can spot several just by reading the code. The keyword void is misspelled, the decla-
ration of the string name should end with a semicolon instead of a comma, and the print statement
should close with a parenthesis followed by a semicolon. If you have a good eye for it, you could see
 
Search WWH ::




Custom Search