Topic : An Exceptional Quest
Author : Sobeit Void
Page : << Previous 4  
Go to page :



Setting up a try/catch block does take some resources. In general, try to reduce the number of try/catch blocks. Exceptions are errors but errors are not necessarily exceptions. So if the error is a very predictable type of error, maybe a return code is more appropriate.

Threads
Each thread requires its own try/catch block. This would mean exceptions cannot propagate between threads. What this means is that the main thread needs some way to be notified when a spawned thread encounters an exception. I donít know, maybe your main thread needs to monitor the state of all the threads continuously, and if any thread encounters an exception, the main thread tells all other threads to end and throws an exception.

I have not found of an elegant solution with respect to threading yet. If you do have some tips, please share.

Static Objects
Since exceptions thrown require a try/catch block to handle the exception, a static object constructor should not throw. If you must throw, you probably have to set the unexpected handler in the first line of the constructor.

Again, I have not found an elegant solution to this problem.

Windows Quirks
No doubt when coding with Windows and its old C code, you will come across many situtations requiring weird hacks.

For example, in the WM_INITDIALOG message handler in a dialog wndproc, you need a try/catch block to make a call to EndDialog in case an exception occurs while creating the dialog.

Also, when you display a MessageBox in the catch handler, you cannot pass it a NULL window handle or the program will crash when exceptions are thrown from the wndproc (certain messages).

Just be on the lookout for weird stuff like this.

I hope I have presented some useful information about exceptions and how to write safe code. Now, please donít write any programs that crash my system.


Page : << Previous 4