can we throw an exception without throws?

Yes, we can throw an exception manually using throw keyword without throws. Syntax:

throw InstanceOfThrowableType;
public class Main {
 
   public int test(int n1, int n2) {
     if(n2 == 0){
        throw new ArithmeticException(); 
     } else {
         return n1/n2;
     }
   }
 
   public static void main(String[] args) {
      Main main = new Main();
      try{
          System.out.println(main.test(130, 0));
      }catch(ArithmeticException e){
          e.printStackTrace();
      }
   } 
}

Output

java.lang.ArithmeticException 
        at Main.test(Main.java:5)  
        at Main.main(Main.java:14)

Java interview questions on Exception Handling

Please follow and like us:
Content Protection by DMCA.com