Java Reference
In-Depth Information
public void calculate(){
BMI = weight / (height*height);
}
public boolean isOverweight(){
return (BMI > 25);
}
}
programming errors
A programming error is also referred to as a bug , and the procedure for removing programming
errors is called debugging . Debugging usually has the following three steps:
1. Detect that there is an error.
2. Locate the error. This can be quite time consuming for big programs.
3. Solve the error.
Different types of programming errors exist and are explored in the following sections.
syntax/compilation errors
A syntax or compilation error refers to a grammatical mistake in the program. Examples are a
punctuation error or misspelling of a keyword. These types of errors are typically caught by the
compiler or interpreter, which will generate an error message. Consider the following Java example:
public void calculate(){
BMI = weight / (height*height),
}
The statement that calculates the BMI should end with a semicolon ( ; ) instead of a comma ( , ),
according to the Java syntax rules. Hence, a syntax error will be generated and displayed. Syntax
errors are usually easy to detect and solve.
runtime errors
A runtime error is an error that occurs during the execution of the program. Consider the following
piece of Java code to calculate the BMI:
public void calculate(){
BMI = weight / (height*height);
}
If the user enters a value of 0 for height, a division by zero occurs. This creates a runtime error
and will likely crash during execution. Another example of a runtime error is an infinite loop into
which the program enters at execution. During the design of the program, it is important to think
about possible runtime errors that might occur due to bad user input, which is where the majority
of bugs will originate. These errors should be anticipated as much as possible using appropriate
error-handling routines, as we will discuss later.
 
Search WWH ::




Custom Search