Page 387 - CITS - Computer Software Application -TT
P. 387

COMPUTER SOFTWARE APPLICATION - CITS




                                          Internal Working of Java try-catch block




































           The JVM firstly checks whether the exception is handled or not. If exception is not handled, JVM provides a
           default exception handler that performs the following tasks:
           -  Prints out exception description.

           -  Prints the stack trace (Hierarchy of methods where the exception occurred).
           -  Causes the program to terminate.
           But if the application programmer handles the exception, the normal flow of the application is maintained, i.e., rest
           of the code is executed.
           Problem without exception handling
           Let’s try to understand the problem if we don’t use a try-catch block.
           Example 1
           TryCatchExample1.java
               public class TryCatchExample1 {


                   public static void main(String[] args) {

                       int data=50/0; //may throw exception


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

                   }


               }




                                                           374

                             CITS : IT&ITES - Computer Software Application - Lesson 101 - 108
   382   383   384   385   386   387   388   389   390   391   392