![]() |
![]() |
Java Packages | |
Java Built in packages | |
Java Package Access Specifiers |
Introduction of Errors | |
Java Exceptions | |
Java Try Block | |
Java Catch Block | |
Java Finally Block | |
Java Throw Keyword | |
Java Throws Keyword | |
Java Builtin Exceptions | |
User Defined Exceptions |
Java Multithreading | |
Creating a Thread | |
Thread Life Cycle | |
Thread Exceptions | |
Thread Synchronization |
Java Programs | |
Java Keywords Dictionary | |
Java Interview Questions |
Errors are the mistakes that can make a program to go wrong in execution. They may be a logical or typical mistakes.
An error may produce an incorrect output or may terminate the program from execution, sometimes it causes to crash the system also.
So it is very important to detect and manage the errors in the program so that system will not crash during while running on the system.
Generally errors are classified into two main categories as
Compile time errors are the syntax errors. These errors are mainly occurs due to the missing statements or writting wrong program without considering it's syntax.
The main reasons for this type of errors are poor understanding of the language.
Compile time errors must be fixed before running the program. Java doesn't create .class file if the program contains errors.
Following program contains errors which are said to be compile time.
class CompileErrors { public static void main(String args[]) { String str = "Hello Androidberry" ; // missing double quote System.out.println(str) // missing ; } }
Output
In above program there is a missing double quote error. We must fix above error to proceed the further compilation process.Thats why we will not get missing semicolon error.
Run time errors are the logical errors. Sometimes the program successfully compiles and creates the .class file but displayes the incorrect output which is not expected by programmer.
These type of error are occure due to the incorrect logic or poor understanding of the problem statement.
Sometimes the wrong logical program may crash the system by producing mistakes like overflowing of stack, infinite running of loops, allocating resources etc
Following example displayes the addition of array elements
class RuntimeError { public static void main(String args[]) { int array []={1,2,3,4,5}; int add=0; for(int i=1;i<5;i++) // wrong start of index { add = add + array[i]; } System.out.println("Addition : "+add) ; } }
Output
In above example we are getting output as 14 but expected is 15. The indexing of array always starts from 0 hence it results in the output but doesn't give any error.