Computer Applications
What is a Runtime error? Explain with an example.
Input in Java
69 Likes
Answer
Errors that occur during the execution of the program primarily due to the state of the program which can only be resolved at runtime are called Runtime errors.
Consider the below example:
import java.util.Scanner;
class RunTimeError
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter a number: ");
int n = in.nextInt();
int result = 100 / n;
System.out.println("Result = " + result);
}
}
This program will work fine for all non-zero values of n entered by the user. When the user enters zero, a run-time error will occur as the program is trying to perform an illegal mathematical operation of division by 0. When we are compiling the program, we cannot say if division by 0 error will occur or not. It entirely depends on the state of the program at run-time.
Answered By
38 Likes