The paint() Method Swallows RuntimeExceptions
If a RuntimeException is thrown within the paint() method of a Field, the RuntimeException is swallowed! Nothing is ever printed, and there is no indication that an exception was ever thrown (except that the code after the exception does not run).
I have confirmed this on the 4.7.0.75-9530 simulator using the following code:
protected void paint( Graphics graphics ) { if ( true ) { throw new RuntimeException( "This text will never show up anywhere." ); } super.paint( graphics ); }
Symptoms
Sections of the Screen or possibly an entire Field might not paint correctly whereas the rest of the Screen may paint just fine. This is a common behavior when an Exception is thrown and consumed by the framework so the app does not crash.
Workaround
Adding a try/catch block can be expensive, and is probably a bad idea within the paint() method. To work around this problem, we should rely more on null checking than on RuntimeExceptions (such as IllegalArgumentException). If a RuntimeException must be thrown, it is important to print an error message before throwing the exception. Otherwise no record of the exception will be displayed in the log.

Comments (2)
Sep 21, 2009
Kurt Zettel says:
Does this cause the simulator to break if you have break on Exceptions enabled?Does this cause the simulator to break if you have break on Exceptions enabled?
Sep 21, 2009
Mark McEver says:
Yes, it breaks. However, my 4.7.0.75-9530 simulator crashes if you try to step ...Yes, it breaks. However, my 4.7.0.75-9530 simulator crashes if you try to step or resume.