How can I test whether a given method will throw an exception or not (depending on the passed object) without invoking it?
For example:
public static boolean isAllowed(SomeObject obj)
{
try
{
myMethod(obj);
return true;
}
catch(Exception ex)
{
return false;
}
}
but the above method will perform MyMethod(), how can I achieve this in java?
EDIT:
Actually, I want to do this to validate a filename. see this: Validate a file name on Windows
As others have said, it is in general impossible to tell whether an exception will be thrown based on the input without actually computing it. Java is turing complete, so this is reducible to the halting problem. However, you can find all exceptions that a method can throw at runtime: