shahnawazm786 / kaz.learning.java

3 stars 3 forks source link

Throws Example #7

Open shahnawazm786 opened 2 years ago

shahnawazm786 commented 2 years ago

public class TestThrow {
//defining a method
public static void checkNum(int num) {
if (num < 1) {
throw new ArithmeticException("\nNumber is negative, cannot calculate square");
}
else {
System.out.println("Square of " + num + " is " + (num*num));
}
}
//main method
public static void main(String[] args) {
TestThrow obj = new TestThrow();
obj.checkNum(-3);
System.out.println("Rest of the code..");
}
}