The present day society is increasing and becoming dependent on computer systems. Whether it is reservation and ticketing for travel by air, train or bus…etc all are controlled by computer systems. So much so that even in sports the decisions of winner or loser in split second finishes are decided by replaying the computer records made during the event. In such an environment it is absolutely necessary that the computer systems (hardware and software) handling these tasks should be highly reliable and should function as expected continuously. However, exceptions do happen either due to failure of a component of hardware system or due to failure of a portion of software/program or due to some unexpected circumstance created during running of the program.
Rarely does program run successfully at its very first attempt. It is common to make mistakes while developing as well as typing a program. A mistake might lead to an error causing the program to produce unexpected results. Errors are the wrongs that can make a program go wrong.
An error may produce an incorrect output or may terminate the execution of the program abruptly or even may cause the system to crash. It is therefore important to detect and manage properly all the possible error conditions in the program so that the program will not terminate or crash during execution.
Types of Errors
Errors may broadly be classified into two categories:
· Compile-time errors
· Run-time errors
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 .class file. It is therefore necessary that we fix all the errors before we can successfully compile and run the program.
/* This program contains an error */
class Error1
{
public static void main (String args[])
{
System.out.println(“Hi Amit!”) // Missing;
}
}
The Java compiler does a nice job of telling us where the errors are in the program. For example, if we have missed the semicolon at the end of print statement in above Program. The following message will be displayed in the screen:
Error1.java :7: ’;’ expected
System.out.println (“Hi Amit!”)
We can now go to the appropriate line, correct the error, and recompile the program. Sometimes, a single error may be the source of multiple errors later in the compilation. For example, use of an undeclared variable in a number of places will cause a series of errors of type undefined variable. After we fix such an error, we should recompile the program and look for other errors.
Most of the errors are due to typing mistakes. Typological errors are hard to find. We may have to check the code word by word, or even character by character. The most common problems are:
· Missing semicolons
· Missing (or mismatch of) brackets in classes and methods
· Misspelling of identifiers and keywords
· Missing double quotes in strings
· Use of undeclared variables
· And so on
Other errors we may encounter are related to directory paths. An error such as
javac : command not found
means that we have not set the path correctly. We must ensure that the path includes the directory where the Java executables are stored.
Runtime Errors
Sometimes, a program may compile successfully creating the .class file but may not run properly. Such programs may turn out wrong results due to wrong logic or may terminate due to errors such as task overflow. Most common run-time errors are:
· Dividing an integer by zero
· Accessing an element that is out of the bounds 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
· Converting invalid string to a number
· And many more
When such errors are encountered, Java typically generates an error messages and aborts the program. The program shown below illustrates how a run-time error causes termination of execution of the program.
class Error2
{
public static void main (String args[])
{
int a = 10;
int b = 5;
int c = 5;
int x = a/(b-c); // Division by zero
System.out.println(“x = “ + x);
int y = a/(b+c);
System.out.println(“Y = “ + y);
}
}
Here, above program is correct and therefore does not cause any problem during compilation. However, while executing, it displays the following message and stops without executing further statements.
java.lang.ArithmeticException: / by zero at Error2.main(Error2.java:10)
When Java run-time tries to execute a division by zero, it generates an error condition, which causes the program to stop after displaying an appropriate message.
Note:
This is just an introduction and the later part will be posted by tomorrow. I'd like to request you to finish this article first and then only move for next part. I hope you've enjoyed.
Resources Used:
E Balagurusamy
Compiled By: Chaudhary Amit V.
Comments
Post a Comment