An error may broadly classified into two categories: compile time errors and run time errors. An error is a mistake made by the user.
1. Compile time errors
All syntax errors will be detected and displayed by the java compiler and therefore these errors are known as compile time errors. Whenever the compiler displays an error it will not create the dot class file (bytecode), it is therefore necessary that we fix all the errors before we can successfully compile and run the programe. The most common problems that gives compile time errors are
- Missing a semicolon
- Miss match of brackets in classes and methods
- Misspelling of identifiers and keywords
- Missing a double codes in string
- Use of undeclared variables
- Use of = symbol instead of = =
Other errors we may encounter are related to directory paths such as " javac is not recognized as an internal or external command".
2. Runtime Errors
Some times a program may compile successfully and creates the dot class file but may not run properly such program may produce wrong results due to wrong logic or may terminated due to errors such as StackOverFlow. Most common runtime errors are
Dividing an integer number by zero
Accessing an element that is out of the boundaries of an array
Trying to store a value into an array of an incompatible class or type
Passing a parameter that is not in a valid range or value for a method
Trying to illegally change the state of a thread.
When such errors are encountered, java typically generates an error message and terminates the program.
The follow program show how exceptions occur
class JavaErrorDemo { public static void main(String[] args) { int a=10,b=5,c=5; int x=a/(b-c); System.out.println(x); } }
Output
In the above program, it will successfully creates the dot class file but it will not produce any output. It will generate an error message indicating an exception occurs
0 comments :
Post a Comment
Note: only a member of this blog may post a comment.