Write a Simple interest program to handle negative amount and duration and interest using user defined exception

Write a Simple interest program to handle negative amount and duration and interest using user defined exception

1.Exception Creation Program
package interest;
public class Ninterest extends Exception
{
    public Ninterest(String s)
    {
        super(s);
    }
};

2.Exception verification Program
package verifyinterest;
import interest.Ninterest;
public class Verify
{
    public void checkinterest(int a,int d,int i) throws Ninterest
    {
        if(a>0 && d>0 && i>0)
        { 
            System.out.print("valid Numbers");
        }
        else
        {
            Ninterest n=new Ninterest("Invalid Numbers");
            throw(n);
        }
    }
}

3.Main Program
import interest.Ninterest;
import verifyinterest.Verify;
import java.util.Scanner;
public class Main
{
    public static void main(String args[])
    {
        int a,i,d,si;
        Scanner s=new Scanner(System.in);
        try
        { 
            System.out.print("Enter your Amount : ");
            a=s.nextInt();
            System.out.print("Enter your Duration : ");
            d=s.nextInt();
            System.out.print("Enter your Interest : ");
            i=s.nextInt();
            Verify v=new Verify();
            v.checkinterest(a,d,i);
            si=a*d*i/100;
            System.out.print("Simple Interest is : "+si);
        }
        catch(Ninterest ne)
        {
            System.out.println("Amount and Duration and interest should not be Negative");
        }
    }
}


Output:

Enter your Amount : 2000
Enter your Duration : 2
Enter your Interest : 2
Valid Numbers
Simple Interest is : 80

Enter your Amount : -2000
Enter your Duration : -2
Enter your Interest : 2
Amount and Duration and interest should not be Negative


What is difference between the throws and throw ?
Throws is a keyword used to forward the exception to the method call whereas throw is a keyword used to throw exception to the method sign.
Throws always should be followed by method signature whereas throw keyword always should exist within method only.


Exception Handling In Java



More Questions


172 . Write a Simple interest program to handle negative amount and duration and interest using user defined exception
173 . Write a String length program
174 . Write a string charAt() method program
175 . Write a string toLowerCase() Program
176 . Write a String toUpperCase() program
177 . Write a string concat() program
178 . Write a string compareTo program
179 . Write a string compareToIgnoreCase() Program
180 . Write a string equals program
181 . Write a string equalsIgnoreCase() program
182 . Write a string startsWith() program