2012年4月5日 星期四

out of memory exception:

Well, "outofMemoryExceptions" should not be handled, because technically, they can't.
Instead of catching these kind exceptions at low level in the code, they should be thrown up the higher layer, i.e. UI level, so that UI layer is aware of the problem, and user is informed that the app is about to exit.


public class LowLevelClass
{
public void complicatedMethod() throws OutOfMemeoryException
{
throw new OutOfMemeoryException("huh, not so good.");
}
}

public class MiddleLevelClass
{
public void doSomethingNotSoComplicated() throws OutOfMemeoryException
{
LowLevelClass low = new LowLevelClass();
try
{
low.complicatedMethod();
}
catch(OutOfMemeoryException oom)
{
doSomethingNecessary();
throw oom;
}
}
}

public class UILevelClass
{
public void showView throws OutOfMemeoryException
{
MiddleLevelClass mid = new MiddleLevelClass();
try
{
mid.doSomethingNotSoComplicated();
}
catch(OutOfMemeoryException oom)
{
App.reportException(oom);
showDialog("Not enough memory! This App is about to close. Please restart.", oom.getMessage());
}
}
}

沒有留言:

張貼留言