I am a beginner so please don't use hard or advanced terminologies. I tried using throws in the method and then removing it and noticed that it really does not make any difference what so ever if I'm handling it in the main when invoking the method.
public Triangle(int side1, int side2, int side3) throws Exception{
if (side1 <= 0 || side2 <= 0 || side3 <= 0)
throw new IllegalArgumentException("Sides can only be positive numbers");
this.side1 = side1;
this.side2 = side2;
this.side3 = side3;}
public static void main (String args []) {
try {
Triangle obj = new Triangle (0,1,1) ;
}catch (Exception e) {
System.out.println("Caught");
}
I once even tried dividing by 0 in the method without using throws and was still able to handle the exception in the main if I used try and catch when invoking the method.
When is it necessary to use throws with a function if I want to handle an exception in someplace else (ex. main method)?