Errors in Java
Some errors in your code do not prevent the program from running, and in this case, the program will only crash while trying to execute a "broken" line: a line with an error called an exception. Thus, exceptions are the errors detected during the program execution (at runtime), whereas syntax errors are those detected during the program compilation into byte-code. An exception interrupts the normal execution of a program.
All exceptions are represented by the Throwable
class, which has two subclasses: Exception
and Error
. There are also two types of exceptions: checked and unchecked.
Unchecked exceptions are not validated by the compiler, so you don't have to handle them. They are represented by the RuntimeException
subclass of the Exception
class. Errors from the Error
class are also considered unchecked.
Checked exceptions have to be handled and indicated explicitly. They are located in all the other subclasses of Exception
.
Suppose, you're writing a Java program. Different errors may occur during its compilation or execution. We will divide all possible errors into two groups: compile-time errors and runtime errors.
Let's look at cases where common errors occur and how to avoid them.
Compile-time errors
Compile-time errors are errors that prevent a Java program from being compiled:
- a syntax error — common syntax errors are an incorrect keyword or a missing semicolon (
;
symbol) at the end of a statement; - a bad source code file name;
- invoking a non-existing method;
- and many others.
Consider a code example of compile-time type of error. The following program should output the string "Hello!" but it does not compile:
public class MyClass {
public ztatic void main(String[] args) {
System.out.printn("Hello!");
}
}
There are two errors in this program:
- a typo in the
static
keyword; - incorrect name of the
println
method.
If you fix these mistakes, it will be possible to compile this program.
To avoid such errors, programmers use a modern IDE (Integrated Development Environment) with a static code analyzer. This tool allows programmers to identify compile-time errors before the compilation. In addition, it is able to highlight warnings about more complex errors and weak places in your code, as well as tips on how to improve the code.
Over time, you will write code that contains fewer or even no compile-time errors.
Runtime errors
Other types of errors are called runtime errors (also known as "bugs"). Bugs are programming errors that occur when the program is running. Runtime errors will cause your program to behave unexpectedly or may even stop the execution.
There are two subtypes of common runtime errors:
- logic errors (or logical errors) — when a program produces a wrong result because the code is not correct (for example, instead of "Hello!", your program outputs "Hi!");
- unhandled exceptional events — like division by zero, file not found, and other unexpected cases.
Avoiding such runtime errors is a more difficult task than avoiding compile-time errors. If your program compiles successfully, there are no guarantees that it does not have bugs. There are different strategies to find such errors:
- to debug your program;
- to write automated tests for your program;
- to use the practice of code review as part of the development process. In general, this practice stands for a case, where one or more developers visually inspect the source code of a program.
Conclusion
- Compile-time errors happen when a program fails to compile. They include typos and incorrect method invocations.
- Static code analyzers in IDEs help to spot compile-time errors before program compilation.
- Runtime errors, or bugs, occur after compilation when a program is running. They may result in unexpected program behavior and crashes.
- Debugging is a useful instrument to identify runtime errors in your program.