handle an exception

This is the forum for miscellaneous technical/programming questions.

Moderator: 2ffat

handle an exception

Postby mark_c » Tue Dec 18, 2018 12:59 pm

Hello,
I have a simple program which tests a series of jpegs and if a faulty one is found, it must signal the problem and continue.
The problem is that once the exception has been raised the program does not continue and stops.
I admit I do not remember how exceptions are handled in the presence of loops

Thank you

Code: Select all
void __fastcall TForm1::Button4Click(TObject *Sender)
{
        for(int i=1;i<FileListBox1->Items->Count;i++)
        {
                TJPEGImage *jpeg = new TJPEGImage;
                try
                {
                        jpeg->LoadFromFile(FileListBox1->Items->Strings[i]);
                }
                catch(EFOpenError *Error) {
                        ShowMessage("error");
                }

                delete jpeg;

                Form1->StatusBar1->Panels->Items[0]->Text=IntToStr(FileListBox1->Items->Count-i);
        }
}
mark_c
BCBJ Guru
BCBJ Guru
 
Posts: 163
Joined: Thu Jun 21, 2012 1:13 am

Re: handle an exception

Postby rlebeau » Tue Dec 18, 2018 2:49 pm

mark_c wrote:The problem is that once the exception has been raised the program does not continue and stops.


Is the app running inside the debugger when that happens, or is it running outside of the debugger?

The former is to be expected, since the debugger stops on exceptions unless you tell it not to. When it does stop, simply tell it to continue executing the app normally and move on.

The latter would be an issue to worry about. That should not happen. Even if you don't catch the correct exception (see below), the VCL would and display its own error message. Your loop would have ended, but your app as a whole would not.

mark_c wrote:I admit I do not remember how exceptions are handled in the presence of loops


Loops have no effect on exception handling.

One issue I do see is that you are simply not catching the exception correctly. You need to catch it by (preferably const) reference, not by pointer.

Also, EFOpenError is not the only possible exception that can be raised by LoadFromFile(), so you should catch the base Sysutils::Exception class instead to handle all possible exceptions. You can still catch EFOpenError specifically, if you want to handle that particular error differently, eg:

Code: Select all
void __fastcall TForm1::Button4Click(TObject *Sender)
{
    for(int i = 1; i < FileListBox1->Items->Count; ++i) // <-- why 1 and not 0?
    {
        String FileName = FileListBox1->Items->Strings[i];

        TJPEGImage *jpeg = new TJPEGImage;
        try
        {
            try
            {
                jpeg->LoadFromFile(FileName);
            }
            catch(const EFOpenError &Error) {
                ShowMessage("Cannot open the file:\n\n" + FileName + "\n\nTry again");
            }
            catch(const Exception &Error) {
                ShowMessage("Error loading the file:\n\n" + FileName + "\n\n[" + String(Error.ClassName()) + "]" + Error.Message);
            }
        }
        __finally
        {
            delete jpeg;
        }

        StatusBar1->Panels->Items[0]->Text = FileListBox1->Items->Count - i;
    }
}
Remy Lebeau (TeamB)
Lebeau Software
User avatar
rlebeau
BCBJ Author
BCBJ Author
 
Posts: 1603
Joined: Wed Jun 01, 2005 3:21 am
Location: California, USA

Re: handle an exception

Postby mark_c » Wed Dec 19, 2018 10:24 am

thanks Remy
mark_c
BCBJ Guru
BCBJ Guru
 
Posts: 163
Joined: Thu Jun 21, 2012 1:13 am


Return to Technical

Who is online

Users browsing this forum: No registered users and 20 guests