Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Developer
Posts: 163
Registered: ‎04-16-2009
My Device: Not Specified

Re: What are people using for assert?

C++ has exception handling built in.  I can see your point about handling exceptions but I am developing a video game.  For video games it's common practice to have asserts through out the code and then they compile out in the final release.  For performance reasons you can't afford the final build to have the extra overhead. 

Developer
Posts: 168
Registered: ‎11-22-2009
My Device: 9000 Bold, 8300 Curve

Re: What are people using for assert?

For video games and other real-time type environments I could see the value in using asserts, but I wouldn't use that as a substitute for catching and handling any exceptions that can come your way.

Developer
Posts: 163
Registered: ‎04-16-2009
My Device: Not Specified

Re: What are people using for assert?

True enough.  I do feel dirty the way it seems common practice to use exceptions as flow control.  I guess it's the Java way. 

 

Developer
Posts: 562
Registered: ‎09-30-2009
My Device: Not Specified

Re: What are people using for assert?

What's wrong with if(!condition) throw new UncatchableException(message); ?

Developer
Posts: 163
Registered: ‎04-16-2009
My Device: Not Specified

Re: What are people using for assert?

There is nothing wrong with it.  That part of it is fine.  It's the code above that that isn't so hot.  If I am calling a function, I don't want to have to surround it with these little try/catch code blocks just to determine whether is succeeded or not.  It really bloats the code and make it harder to read IMHO.  I guess I don't consider the fact that say a file didn't manage to open as an exceptional condition Smiley Surprised  Just return me an object and I will check if it's null and we can all move on...

 

Developer
Posts: 562
Registered: ‎09-30-2009
My Device: Not Specified

Re: What are people using for assert?

You don't catch the exception, you intend to throw it all the way up the stack so your application dies when the assert fails (isn't that what asserts are designed to do?)

 

This, sadly, requires you to declare every method as "throws Exception", which is irritating.

Developer
Posts: 163
Registered: ‎04-16-2009
My Device: Not Specified

Re: What are people using for assert?

Sorry.  I misunderstood.  I thought you were referring to the comment I made about how Java tends to use exceptions for flow control. 

 

To use what you're suggesting, wouldn't you still have to make sure that you catch this exception at various points?  What do you do if you're in a callback from the OS and you don't own the code above?  Wouldn't you have to put try/catch blocks in all your entry points that the OS calls?  Or are you suggesting that if I just let the exception happen, that the OS will show a message?  I thought I tried this but perhaps I am mistaken.

 

 

Developer
Posts: 562
Registered: ‎09-30-2009
My Device: Not Specified

Re: What are people using for assert?

There are some tricky situations involved with this, you're right, I think.

 

But what you want is the application to get killed hard if an assert fails, and receiving an uncaught exception will do that. I think the OS might produce a dialog in this instance with the error message, but one would need to test this; regardless, you're interested in seeing debug or log output when this happens, and this is what it should produce.

Developer
Posts: 168
Registered: ‎11-22-2009
My Device: 9000 Bold, 8300 Curve

Re: What are people using for assert?

[ Edited ]

Ideally you will wrap everything at the top level with a try-catch and then do a system exit. Assert won't help you when it's a runtime image.

 

Incidentally, it's amazing how many "commercial" BB apps that I've got NullPointerExceptions or IOExceptions on through normal usage.

Highlighted
New Contributor
Posts: 3
Registered: ‎01-19-2013
My Device: BlackBerry Curve 9300
My Carrier: Virgin

Re: What are people using for assert?

It's best not to confuse the purpose of assertions and exception handling; the distinction is between programming errors which should not happen and which assert is made to find and runtime errors where a condition occurs which the program has no control over, e.g. a file is missing, a network packet is corrupt, etc.  For example, if you have an algorithm that has a loop, you can assert that progress is made on each iteration.  If you have another example where it is not possible to delete a file then an exception should be thrown.  In this way your program's error handling is split conceptually into program defects which you expect never to occur and runtime errors which may or may not occur. 

 

Assertions are also used to document contracts between methods, check post conditions and object invariants.