Page 121 - ComputerScience_Class_11
P. 121
5.5 ERRORS
Errors in Java are problems in a program that cause it to behave unexpectedly or stop working correctly. They may be
Compile-time errors or Run-time errors.
5.5.1 Compile-time Errors
Compile-time Errors occur during compilation, when the program is being checked by the Java compiler before
execution.
These errors happen due to syntax mistakes or incorrect use of language rules and the program cannot run until they
are fixed.
Common causes for Compile-time errors may be due to:
• Missing semicolon
• Undeclared variables
• Incorrect data types
• Missing brackets or parentheses
• Spelling mistakes in keywords
Example:
class Test {
public static void main(String args[]) {
int a = 10
System.out.println(a);
}
}
Error: Missing semicolon (;) after 10.
5.5.2 Run-time Errors
Run-time Errors occur while the program is running, after it has successfully compiled.
These errors happen due to illegal operations during execution.
Common causes for Run-time errors may be due to:
• Dividing a number by zero
• Accessing an array element outside its range
• Using a null object
• Invalid user input
Example:
class Test {
public static void main(String args[]) {
int a = 10;
int b = 0;
int c = a / b;
System.out.println(c);
}
}
Error: ArithmeticException: / by zero
Objects 119

