java - Test a method if it would throw an exception or not without invoking it -
how can test whether given method throw exception or not (depending on passed object) without invoking it?
for example:
public static boolean isallowed(someobject obj) { try { mymethod(obj); return true; } catch(exception ex) { return false; } }
but above method perform mymethod()
, how can achieve in java?
edit:
actually, want validate filename. see this: validate file name on windows
as others have said, in general impossible tell whether exception thrown based on input without computing it. java turing complete, reducible halting problem. however, can find exceptions method can throw @ runtime:
try { method mymethod = this.getclass().getmethod("mymethod"); system.out.println(arrays.tostring(mymethod.getexceptiontypes()); } catch (nosuchmethodexception e) {}
Comments
Post a Comment