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

COMPUTER SOFTWARE APPLICATION - CITS



                       //calling the function
                       validate(13);
                       System.out.println(“rest of the code...”);

                 }
               }
           Output















           The  above  code  throw  an  unchecked  exception.  Similarly,  we  can  also  throw  unchecked  and  user  defined
           exceptions.
              Note: If we throw unchecked exception from a method, it is must to handle the exception or declare
              in throws clause.
           If we throw a checked exception using throw keyword, it is must to handle the exception using catch block or the
           method must declare it using throws declaration.
           Example 2: Throwing Checked Exception
              Note: Every subclass of Error and RuntimeException is an unchecked exception in Java. A checked
              exception is everything else under the Throwable class.

           TestThrow2.java
               import java.io.*;

               public class TestThrow2 {


                   //function to check if person is eligible to vote or not
                   public static void method() throws FileNotFoundException {

                       FileReader file = new FileReader(“C:\\Users\\Anurati\\Desktop\\abc.txt”);

                       BufferedReader fileInput = new BufferedReader(file);


                       throw new FileNotFoundException();

                   }

                   //main method
                   public static void main(String args[]){
                       try
                       {




                                                           378

                             CITS : IT&ITES - Computer Software Application - Lesson 101 - 108
   386   387   388   389   390   391   392   393   394   395   396