Zeek_78_Types of Errors in Java
Types of Errors in Java
Java, there are three types of error that can occur:
- 1- Syntax Errors
- 2- Logical Errors
- 3- Runtime Errors
Syntax errors
A syntax error occurs when the code given does not follow the syntax rules of the programming language.
Examples include:
- --> Misspelling a statement, eg writing
pintinstead ofprint - --> Using a variable before it has been declared
- --> Missing brackets, eg opening a bracket, but not closing it
A program cannot run if it has syntax errors.
Any such errors must be fixed first.
A good integrated development environment (IDE) usually points out any syntax errors to the programmer.
- Logical Errors
A logic error is an error in the way a program works.
The program can run but does not do what it is expected to do.
Examples include:
- --> Incorrectly using logical operators, eg expecting a program to stop when the value of a variable reaches 5, but using <5 instead of <=5.
- --> Incorrectly using Boolean operators
- --> Unintentionally creating a situation where an infinite loop may occur
- --> Incorrectly using brackets in calculations
- --> Unintentionally using the same variable name at different points in the program for different purposes
- --> Using incorrect program design
Unlike a syntax error, a logic error does not usually stop a program from running.
The program will run, but not function as expected.
NOTE- A program with a syntax error will not run.
A program with a logic error will run but it will not perform as expected.
Runtime errors
A Runtime Error is an error that takes place during the running of a program.
An example is
Writing a program that tries to access the sixth item in an array that only contains five items.
A runtime error is likely to crash the program.
Comments
Post a Comment