Difference Between throw and throws

Get Job-ready: Java Course with 45+ Real-time Projects! - Learn Java

Program 1

import java.util.*;
import java.io.IOException;
class Test
{
      void first(int n) throws  IOException
      {
         if(n%2==0)
           throw  new IOException(" this is error in first method");
         else
          System.out.println( "No is odd");  
      }
      void second(int n) throws  IOException
      {
           first(n);
      }
}


class TestException1
{
      public static void main(String args[]) 
      {
         try
          {   
              Test T=new Test();
               T.second(3);
          }
          catch(IOException e)  
          {
             System.out.println(e);
          }

        //      Scanner scan=new Scanner(System.in);
        //      int a,b,c;
        //      System.out.println("Enter two Number");
        //      a=scan.nextInt();
        //      b=scan.nextInt();
        //    try
        //   {   
        //     if(b==5) 
        //         throw(new ArithmeticException("Divide by 5 Error") );
        //      c=a/b;
        //      System.out.println(c);
        //   }
        //   catch(Exception e)  
        //   {
        //       System.out.println(e);
        //   }

      }
}

 

Did you like our efforts? If Yes, please give DataFlair 5 Stars on Google

follow dataflair on YouTube

Leave a Reply

Your email address will not be published. Required fields are marked *