Both throw and throws are concepts of exception handling in Java. The throws keyword is used to declare which exceptions can be thrown from a method, while the throw keyword is used to explicitly throw an exception within a method or block of code.
Program:- Calculator.java
package payilagam.institution;
import java.util.Scanner;
public class Calculator {
public static void main(String[] args)
{
PasswordException pe = new PasswordException();
Scanner sc = new Scanner(System.in);
System.out.println("Enter the Password");
int no =sc.nextInt();
try {
pe.checkPwd(no);
} catch (PasswordException e) {
// TODO Auto-generated catch block
System.out.println("Check Password");
//e.printStackTrace();
}
}
}
Program:- PasswordException.java
package payilagam.institution;
public class PasswordException extends Exception{
public static void main(String[] args)
{
}
public void checkPwd(int no) throws PasswordException
{
if(no==0)
{
PasswordException pe = new PasswordException();
throw pe;
}
}
}
Output Screenshot:-

