Search |
|||
Lorenzo Puccetti's blogJava back in the 1930s.Posted by unoinpiu on May 15, 2009 at 5:48 AM PDT
Consider the following interface:
public interface X
{
public void doSomething()
}
and two implementations:
public class Good implements X
{
public void doSomething() { ; }
}
public class Bad implements X
{
public void doSomething() { throw new RuntimeException(""); }
}
Let's have a convenience class ExcpnChecker with just one static method to check whether a given implementation of X is going to throw an exception or not.
public final class ExcpnChecker
{
public static boolean isThrowingException(X x)
{
try
{
x.doSomething();
return false;
}
catch (Exception e)
{
return true;
}
}
}
such that: ExcpnChecker.isThrowingException(new Good()); // returns false ExcpnChecker.isThrowingException(new Bad()); // returns true Consider now a third implementation of the X interface:
public class Pun implements X
{
public void doSomething()
{
if (ExcpnChecker.isThrowingException(this)) return;
else throw new RuntimeException();
}
}
can you tell what we get from ExcpnChecker.isThrowingException(new Pun()); Now, think...
public class Forever implements X
{
public void doSomething() { while(true) { ; } }
}
If I were to pass an instance of Forever to 'isThrowingException' You can try very hard to fix the 'isThrowingException' but you won't succeed. The method "isThrowingException for an arbitrary X implementation" is not computable. see:
»
Related Topics >>
Programming Comments
Comments are listed in date ascending order (oldest first)
Submitted by kebernet on Fri, 2009-05-15 11:11.
Actually, I think you are missing something here. Because Pun is, in effect, recursive,
ExcpnChecker.isThrowingException(new Pun());
will return false, because you will get a StackOverflowException after some amount of time at which point, the parent method would get "true" and therefore return without error and all the calls farther up the tree would get no exception, making the top level call return false.
You are correct about the Forever implementation, however.
|
CategoriesRecent Entries |
||
|
|
Attempting to fix it by catching "Throwable" would return 'true' as you say but that would be incorrect since Pun's code says: "if I throw an exception I return peacefully, If don't throw an exception let me throw one"
The problem is that such a program can not be coded. The reason people might think they can write it is based on the assumption (wrong) that ExcpnChecker can be written.