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

Popular posts from this blog

linux - Using a Cron Job to check if my mod_wsgi / apache server is running and restart -

actionscript 3 - TweenLite does not work with object -

jQuery Ajax Render Fragments OR Whole Page -