Types of errors in program.

ERROR:

*An error is an action which is inaccurate or incorrect.

*In some usages, an error is synonymous with a mistake.

*The word "error" refers to the difference between the value which has been computed and the correct value.

*Programming errors often remain undetected until the program is compiled or executed.

*Some of the errors inhibit the program from getting compiled or executed.


Sources of errors are:

  • Common sources of error include instrumental, environmental, procedural, and human.
  • All of these errors can be either random or systematic depending on how they affect the results.
  • Instrumental error happens when the instruments being used are inaccurate, such as a balance that does not work.
  • we can also make errors with lack of intrest,lack of knowledge about particular topic,lack of concentration.

TYPES OF ERRORS ARE IN PROGRAMMING ARE:

*compile time error

*runtime error

*syntax error

*logical  error

These are errors where the compiler finds something wrong with your program, and you can't even try to execute it.

​For example:you may have incorrect punctuation, or may be trying to use a variable that hasn't been declared.

*RUNTIME ERROR:

  •  Run Time errors  detected during the execution of the program.
  • Sometimes these are discovered when the user enters an invalid data or data which is not relevant.
  • Runtime errors occur when a program does not contain any syntax errors but asks the computer to do something that the computer is unable to reliably do.
  • During compilation, the compiler has no technique to detect these kinds of errors.
  • It is the JVM (Java Virtual Machine) which detects it while the program is running.
  • To handle the error during the run time we can put our error code inside the try block and catch the error inside the catch block.

EXAMPLE:        Runtime error occurs when the number is divide by zero.Here in the below program we get runtime error.

class DivisionByZero { public static void main(String args[])
 {
int a= 7; int b = 89; int c = 0; 
int res1 =a /b;
 // This statement causes a runtime error, 
// as 15 is getting divided by 0
 here int res2 =a / c;
 System.out.println( "Division of a" + " by b is: " +res1); 
System.out.println( "Division of a" + " by c is: " +res2);
 }
 }

*COMPILE TIME  ERROR:

  •  Compile Time Errors are those errors which prevent the code from running because of an incorrect syntax such as a missing semicolon at the end of a statement or a missing bracket, class not found, etc.
  • These errors are detected by the java compiler and an error message is displayed onto the screen while compiling.
  • Compile Time Errors are sometimes also referred to as Syntax errors.
  • These kind of errors are easy to spot and rectify because the java compiler finds them for you.
  • The compiler will tell you which piece of code in the program got in trouble and its best guess as to what you did wrong.
  • Usually, the compiler indicates the exact line where the error is, or sometimes the line just before it, however, if the problem is with incorrectly nested braces, the actual error may be at the beginning of the block.
  • In effect, syntax errors represent grammatical errors in the use of the programming language.

​Here in the example we have compile time error:

class  wrongspelledVar { 
    public static void main(String args[]) 
    { 
        int a = 9, b = 5; 
  
        // Declared variable Sum with small s
        int sum = a + b; 
  
        // Trying to call variable Sum 
        // with a capital s; i.e. sum 
        System.out.println(   "Sum of variables is " + Sum); 
    } 
} 

LOGICAL ERROR:

  • A logic error is a mistake in a program's source code that results in incorrect or unexpected behavior.
  • It is a type of runtime error that may simply produce the wrong output or may cause a program to crash while running.
  •  For example:

                  *Assigning a value to the wrong variable may cause a series of unexpected program errors.

                 *Multiplying two numbers instead of adding them together may also produce unwanted results.

                  *Even small typos that do not produce syntax errors may cause logic errors.

                  * In the PHP code example below, the if statement may cause a logic error since the single equal sign (=) should be a                       double equal sign (==).

EXAMPLE :

Incorrect: if ($i=1) { ... }

Correct: if ($i==1) { ... }

SYNTAX ERROR:

*A syntax error is an error in the source code of a program.

*Since computer programs must follow strict syntax to compile correctly, any aspects of the code that do not conform to the syntax of the programming language will produce a syntax error.

EXAMPLE:

*syntax errors are missing a comma or a quotation mark.

Posted on by