Java Programming
About Lesson

Types of Errors occur during program development

It is common to occur any error during program development due to the ignorance of developer, program or some external resource.  Basically, there are two types of errors occur during any program development.  They are Compile time errors and Run-time errors.  As their name saying, compile time errors occur at the time of compiling the source code while run-time errors occur at the time of executing the program. 

Compile-time errors are two types, they are

Syntax Errors: occur when sentence construction rules are avoided.

Semantic Errors: occur when some useless code is founded.

Run-time Errors are also two types, they are

Logical Errors: occur due to ignorance or negligence of developer.

unExpected Errors: occur due to lack of or failure of some external resource.

Exception Handling of java is a mechanism to handle unexpected runtime errors only (the last one), but not any other types of errors discussed above.  Exception can be defined as an unwanted situation during the execution of the program.  Exception causes the abnormal termination of the program.  This situation is not suitable for the following examples real-time scenarios

  1. ATM program is closed due to wrong password entry by a user.
  2. A network application is closed due to some mistake done by an user on a particular node.

Handling of exception is the concept of avoiding abnormal termination of the program due to exception so that the program would continue its execution by taking necessary steps regarding exception.  The handling of exception is not providing solution for the exception but it is providing an alternative way to run the rest of the program which is not dependent on the code in which exception occurred. 


//example code with run-time error

public class NoHandling{ 

  public static void main(String args[]){ 

      int data=50/0; 

      System.out.println(“rest of the code…”); 


The output of the above code would be like below:

Exception in thread “main” java.lang.ArithmeticException: / by zero

            at packageName. main(ClassName.java:LineNumber)


The exception raised in the above code is divisible by zero in the line “int data=50/0”.  You can observe there is an output statement that is displaying a string “rest of the code…” is not executing even though it is not at all dependent on the code line in which exception occurred.

This is also known as abnormal termination or automatic exception handling handed over to JVM.


//same program with exception handling

public class WithHandling{  

  public static void main(String args[]){  

   try{ 

      int data=50/0; 

   }

   catch(ArithmeticException e){

System.out.println(e);

   System.out.println(“rest of the code…”); 

}  

}


The output of the above program is :

java.lang.ArithmeticException: / by zero

rest of the code…


The difference you can get from the second example is, it continues with the printing of the string “rest of the code…” by showing respecting exception message.  This is known as manual exception handling due to which the program would be terminated normally.

You cannot copy content of this page