When a programmer writes a program with lots of instructions, he / she may have forgotten something or written incorrect statements or some infinite loops or assigned wrong values or something else which creates serious issues in the program, those mistakes are reported as errors. There are two kind of errors - Compile Time Errors and Run Time Errors.
Compile Time Errors
These errors are reported during compilation time and also known as Syntax Errors as they are reported when there is syntactical error like uppercase / lowercase difference (Java is a case-sensitive language), missing parentheses, missing semi-colon or wrong assignment etc. These kind of errors can be spotted and rectified easily as when the program is compiled, the compiler reports the specific error and line no of error code. Here is the example of Compile Time Error -
Class Test1 (Test1.java)
public class Test1 {
public static void main (String args[]) {
int i = 10;
System.out.println("Number is " + i)<== Error (Semi-colon missing)
}
}
Error:(9, 45) java: ';' expected
Above example shows the error of missing semicolon. Error shows the coordinates (line no and column no), where the semi-colon (;) is expected by the compiler. Another example is here, when the wrong value is assigned to the variable (double value is assigned to int data type variable -
Class Test2 (Test2.java)
public class Test2 {
public static void main (String args[]) {
int i = 10.2;<== Error (Double value assigned to int data type variable)
System.out.println("Number is " + i);
}
}
Error:(7, 17) java: incompatible types: possible lossy conversion from double to int
Run Time Errors
These errors are reported during run / execution time and the program stops abruptly in the middle of program. When the program is compiled perfectly by the compiler, but at run-time when some wrong data is entered by the user (entered double value for int value) or wrong data assignment or wrong calculations (like division by 0) or going in an infinite loop (no condition to stop the loop), these errors occur and are detected by the JVM (Java Virtual Machine). These kinds of errors are also called as Exceptions and to handle these errors, the statements should be written in the Try-Catch block and act accordingly. Here is the example of Run Time Error -
Class Test3 (Test3.java)
import java.util.Scanner;
public class Test3 {
public static void main (String args[]) {
int i;
Scanner sc = new Scanner(System.in);
System.out.println("Enter Number");
i = sc.nextInt();
System.out.println("Number is " + i);
}
}
Enter Number 12.5 Exception in thread "main" java.util.InputMismatchException at java.base/java.util.Scanner.throwFor(Scanner.java:939) at java.base/java.util.Scanner.next(Scanner.java:1594) at java.base/java.util.Scanner.nextInt(Scanner.java:2258) at java.base/java.util.Scanner.nextInt(Scanner.java:2212) at Test.main(Test.java:11)
The above example has no syntactical error, so it compiles perfectly. But when it is run, and the user enters the wrong value, the exception InputMismatchException is thrown and specifies the line no from where the exception is thrown. The programmer can put the statement on the specified line, in the Try-Catch block and whenever the exception occurs, can act accordingly without stopping the program abruptly.
Note: I will discuss Exceptions and Try-Catch block in detail sooner.
No comments:
Post a Comment