I have a class with a method that throws some exceptions and catches them inside itself, but when i call it in my Main class they seem to not being catched.
An example about my problem:
public class Test { public static void method (int number) throws InvalidNumberException { try { if (number == 5) { throw new InvalidNumberException("Invalid number"); } } catch (InvalidNumberException inv) { System.out.println(inv); } } } public class InvalidNumberException extends Exception { public InvalidNumberException (String s) { super(s); } } public class Main { public static void main(String args[]) { Test.method(5); } } When i try to compilate the last one i get this error:
Main.java:3: error: unreported exception InvalidNumberException; must be caught or declared to be thrown Test.method(5);
Is there a way to fix it without catching the exception in the Main class?