Getting build errors when using getDeclaredMethod() to invoke a public static method

13 Views Asked by At

I have the following code written, but it keeps throwing

unreported exception NoSuchMethodException; must be caught or declared to be thrown

and

unreported exception IllegalAccessException; must be caught or declared to be thrown

@UtilityClass
public class CF {

    private static Integer newFunction() {
        return 1;
    }
}

public class CFTest {

    public void testNewFunction() {
        Method method = CF.class.getDeclaredMethod("newFunction");
        method.setAccessible(true);
        Integer m1 = (Integer) method.invoke(null);
    }
}

I was wondering if I need to use try and catch statements for NoSuchMethodException and IllegalAccessException in order for this to run normally.

I tried try and catch with NoSuchMethodException and IllegalAccessException and it worked, but I was wondering if it really needs it and my code is running per normal or is it just exiting with errors.

0

There are 0 best solutions below