Board index » cppbuilder » try & catch

try & catch


2005-07-13 01:31:03 AM
cppbuilder55
Hello,
I'm a bit confused on the functionality of the try/catch statements....
// catch infamous "Canvas does not support drawing!" error
try
{
// update status, immediately
StatusBar->Caption = "something useful";
StatusBar->Repaint();
}
// catch exception without losing program integrity
catch (...)
{
// report error
WriteErrorToFile("caught exception");
}
The catch is never being executed. The de{*word*81} lands on the "Repaint"
line after the exception is thrown "Canvas does not support drawing!".
But if I break up the statements into individual try/catch statements,
the catch executes and writes the entry to my log file. My program
continues execution as normal. This is preferred.
// catch infamous "Canvas does not support drawing!" error
try
{
// update status
StatusBar->Caption = "something useful";
}
// catch exception without losing program integrity
catch (...)
{
// report error
WriteErrorToFile("caught exception");
}
// catch infamous "Canvas does not support drawing!" error
try
{
// update GUI
StatusBar->Repaint();
}
// catch exception without losing program integrity
catch (...)
{
// report error
WriteErrorToFile("caught exception");
}
Has anyone experienced similar behavior. Do I need to put a try/catch
around every single GUI statement? My program does very sophisticated
image acquisition, processing, and analysis and keeps crashing out
because of this "Canvas does not support drawing!", resulting in
incomplete acquisition runs.
Help please... thanks so much!
Rob
 
 

Re:try & catch

Rob,
Send more of your code; I can't duplicate the error that you are seeing
and haven't seen it before.
TStatusBar->Caption is not accessible.
Using something like:
StatusBar->SimplePanel = true;
StatusBar->SimpleText = "Something Useful";
StatusBar->Refresh();
(see no try/catch)
Another approach would be to figure out what you are doing that is
creating the Exception in the first place.
 

Re:try & catch

"Robert G. Hoover" < XXXX@XXXXX.COM >wrote in message
Quote
catch (...)
Which version of BCB are you actually using? If BCB6, then '...' does not
havdle VCL exceptions as well as it did in previous versions. You should
always catch VCL exceptions by type explicitally:
catch (const Exception &)
Quote
The catch is never being executed. The de{*word*81} lands on the
"Repaint" line after the exception is thrown "Canvas does not
support drawing!".
The exception is liely being handled by the VCL internally so that your own
code never sees it. Of course, the de{*word*81} sees all exceptions regardless
of where they are thrown or how they are handled. Pressing F9 passes the
exception back to the application for normal handling.
Quote
But if I break up the statements into individual try/catch
statements, the catch executes and writes the entry to my log file.
There is no difference between having 1 try/catch block or multiple. An
exception is an exception, and if it actually reaches your code, having a
single try/catch block will handle it just as well as multiple blocks.
Quote
My program does very sophisticated image acquisition, processing,
and analysis and keeps crashing out because of this "Canvas does not
support drawing!", resulting in incomplete acquisition runs.
The best way to handle an exception is to not cause an exception to be
thrown in the first place. Are you locking the Canvas every time you draw
on it? Are you drawing on it from multiple threads at the same time?
Gambit
 

{smallsort}

Re:try & catch

Thanks for the tip. I'm using BCB6. I added a "catch (Exception
&exception)" block to my code and it is catching the exception now with
multiple lines in the "try" block.
As for finding the cause, my bet is in multiple threads trying to access
the same canvas? I have the main application thread running and an
independent thread running in the background. Only the background thread
is altering my GUI, whether it's a TForm->Canvas or a TPanel->Caption
that I'm using as a status bar. It's possible that the main app thread
is trying to process a WM_PAINT message at the same time a background
thread UpdateStatus is trying to change the GUI??? I guess that could
account for it. You know, come to think of it, I've seen this "Canvas
does not allow drawing" error from time to time in other apps where no
background thread is running. <shrug>At least the exception is being
caught now and not crashing the program. My customers will be happy the
program doesn't bomb out in the middle of a long acquisition run. ;)
Thanks for the help!
Rob
Remy Lebeau (TeamB) wrote:
Quote
"Robert G. Hoover" < XXXX@XXXXX.COM >wrote in message
news: XXXX@XXXXX.COM ...


>catch (...)


Which version of BCB are you actually using? If BCB6, then '...' does not
havdle VCL exceptions as well as it did in previous versions. You should
always catch VCL exceptions by type explicitally:

catch (const Exception &)


>The catch is never being executed. The de{*word*81} lands on the
>"Repaint" line after the exception is thrown "Canvas does not
>support drawing!".


The exception is liely being handled by the VCL internally so that your own
code never sees it. Of course, the de{*word*81} sees all exceptions regardless
of where they are thrown or how they are handled. Pressing F9 passes the
exception back to the application for normal handling.


>But if I break up the statements into individual try/catch
>statements, the catch executes and writes the entry to my log file.


There is no difference between having 1 try/catch block or multiple. An
exception is an exception, and if it actually reaches your code, having a
single try/catch block will handle it just as well as multiple blocks.


>My program does very sophisticated image acquisition, processing,
>and analysis and keeps crashing out because of this "Canvas does not
>support drawing!", resulting in incomplete acquisition runs.


The best way to handle an exception is to not cause an exception to be
thrown in the first place. Are you locking the Canvas every time you draw
on it? Are you drawing on it from multiple threads at the same time?


Gambit


 

Re:try & catch

"Robert G. Hoover" < XXXX@XXXXX.COM >wrote in message
Quote
Only the background thread is altering my GUI
It should not be doing that. Only the main thread should be accessing the
GUI. If the background threads needs to access the GUI, then it should be
doing so via the thread's Synchronize() method.
Gambit