What Is a Runtime Error

Runtime is the final stage of program life cycle. Runtime is the phase where the output is produced. One can say that the span of execution of any program is known as runtime. Any bug in the program logic or coding has its impact on the output of the program and such unexpected output is termed as the runtime error. Runtime error is usually produced due to logical errors and certain exceptions.

We all know errors are of two types, compile time error and runtime error. Compile time error basically deals with the error occurred during compiling which takes place before runtime phase. Compile time errors is the by product of mistakes in syntax or programming language. On the other hand, runtime error is the by product of programming logic.

Understanding runtime error

Lack of logic or implementation of incorrect logic will not produced the expected output. For an instance, if you want a program to produce remainder of two integers, then you need to use the modules operator, ‘ % ‘, and not division operator, ‘ / ‘. If you use the the division operator then you will get the quotient as the output instead of remainder. Other examples are wrong use of operator and their precedence and incorrect of any mathematical formula.

For example,

the code in c language

 int a, b,c;

printf(“enter the numbers:n”);

scanf(“%d%d”, &a, &b);

c= a+b/b*a;

printf(“the output: %dn”, c);

If a programmer wants to produce a division with the output of the addition of a and b in the numerator and multiplication of c and a then the above code will face runtime error. This reason is simple, operator precedence and associativity. According to C language, the division operator has higher precedence and associativity than multiplication, addition, subtraction. In order to get the expected output you add parenthesis, “()”, to the numerator and the denominator expression.

The correct code is: c = ((a+b)/(b*a));

This changes the dynamics of the operator precedence and associativity.

Other exception such as array out of bounds and stack over flow are common examples of runtime error.

 

How to solve runtime errors?

 

  1. Check your logic– It is very important to check your logic as most runtime error occur because of logical errors. One should check the implementation of his or her logic especially in the case of arithmetic expressions.

  2. Exception handling– Exception such stack overflow and array out of bounds are basic runtime errors. So, a programmer should always add extra code to avoid such exceptions. For an instance, code in c language

    int a[10], i, n;

    printf(“enter the number of elements you want in your array:n”);

    scanf(“%d”, &n);

    for(i=0;i

    scanf(“%d”, a[i]);

    The above code will produce an error if user enter 11 or more than 10 as the value of stored in variable n. This will produce an exception such as array out of bounds. So, before asking for array index a programmer must specify the bound such as

 

             printf(“enter the number of elements you want in your array, any number between 1-10 :n”);

Hence, proper understanding of any programming language is needed to avoid runtime errors.

 

admin