Java Program on throws Statement

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

Program 1

package dataflair;

import java.io.IOException;

public class MyClass 
{
    int factorial(int n) throws IOException    
    {
           int f=1;
          if(n<0)
           throw new IOException("-ive value Exception");
          else
          {
              while(n!=0)
              {
                   f=f*n;
                   n--;
              }
          }
       return f;   
    }
}

Program 2

package dataflair;

import java.io.IOException;

public class Test 
{
        void display() throws IOException
        {
        //      MyClass M=new MyClass();
         
        }
}

Program 3

package dataflair;

import java.io.IOException;

public class TestMain 
{
    public static void main(String[] args) throws IOException
    {

        MyClass M=new MyClass();
        int x;
             
        x=M.factorial(-7);
        System.out.println("Factorial is " + x);
      
//         Test T=new Test();
//         T.display();

//       try
//       {     
//        Test T=new Test();
//        T.display();
//       }
//       catch(IOException e)
//       {
//           System.out.println(e);
//       }    
    }       
                     
        
        // Test T=new Test();
        // T.display();
//      try
//      {    
//        MyClass M=new MyClass();
//        M.xyz();
//      }
//      catch(IOException e)
//      {
//          System.out.println(e);
//      }    
    
    
}

Did we exceed your expectations?
If Yes, share your valuable feedback on Google

courses

TechVidvan Team

TechVidvan Team provides high-quality content & courses on AI, ML, Data Science, Data Engineering, Data Analytics, programming, Python, DSA, Android, Flutter, full stack web dev, MERN, and many latest technology.

Leave a Reply

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