Types of Errors
At this point, errors are nothing new to you as a
programmer. This is not necessarily a bad thing, since
you learn a lot by debugging a program and fixing errors.
In order to understand what Exceptions are, you need to
understand the different kinds of errors that can occur
in a program and how we go about solving each one.
There are three categories of errors in programming
(these are covered in Chapter 1.10 of your textbook):
Compile-time Errors
(or just "Compile/Compilation Errors")
- Errors that are caught by the compiler when
you build your source code.
- Also often referred to as syntax errors
because they usually involve incorrect syntax.
- You should remember that when you get compile
errors after compiling a program, your .class file
is not created/updated.
- To fix these kinds of errors, you need to
read the error messages produced by the compiler
and fix the appropriate statements.
Examples of Compilation Errors
- Missing semi-colon at the end of a statement.
- Incorrectly typing a variable name or method name,
such that the compiler can't find the item you've
referred to ("Cannot find symbol!")
- Missing import statement, so the compiler can't find
a class (such as JOptionPane or Scanner).
- Forgetting to include all required arguments in a
method or giving a method the wrong number of arguments.
- Using incorrect case on a reserved word (e.g. While
instead of while or Else instead of else).
Run-time Errors
(or "Execution Errors")
- Errors that occur while a program is running.
- Usually they cause the program to "crash".
- These kinds of errors don't get noticed at compile
time, but will show up with error messages while
the program runs.
- The program might run fine for a while before it crashes.
Examples of Run-Time Errors:
- Input errors, e.g. the user types a String when your
input variable is an int or double.
- Using an incorrect format specifier or using the
wrong kind of format specifier in a printf() or format()
statement.
Logic Errors
- Errors in the logic of the program.
- These kinds of errors don't yield error messages.
- These are usually the fault of the programmer :P
- Usually they cause things like endless loops, by-passed
code, incorrect output, etc.
- Solving these kinds of errors is the most difficult,
because sometimes you only know that the program doesn't
function, but don't know exactly why or where the problem
is.
- More sophisticated editors like NetBeans and Eclipse
have useful debugging tools that help you to solve
these kinds of errors.
Examples of Logic Errors:
- A loop that never ends because you forgot the counter.
- An if-statement condition was written incorrectly, causing
the body of the if to never execute.
- Forgetting to cast a value when dividing two integers.
- Writing a calculation incorrectly.
- Adding a semi-colon to the end of an if-statement header,
such as
if (x > 3);
- Columns of values in your program output are not aligned
or formatted correctly.
Note
Stylistic errors and mistakes or omissions in
adhering to programming standards are not considered in any of the
above categories.
Exercise
For each of the following programs below:
a. Identify any errors in the program.
b. Indicate the category of each error (Compilation Error,
Run-Time error, or Logic Error)
- Example A:
public class SomeErrors {
public void main(String[] args) {
System.out.print("Hi");
System.out.printlm("Hello");
System.out.print("Bye" \n);
}
}
- Example B:
public class SomeErrors {
public static void main(String[] args) {
int number;
int value = 1;
number++;
double result = number / value;
System.out.println("Result: " result);
}
}
- Example C:
public class SomeErrors {
public static void main(String[] args) {
Scanner in = new Scanner();
System.out.println("Enter number of cats.");
number = in.nextInt();
System.out.printf("%4.2f is too many cats.", number);
}
}
[solutions]