Page 1 of 1

handle an exception

PostPosted: Tue Dec 18, 2018 12:59 pm
by mark_c
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);
        }
}

Re: handle an exception

PostPosted: Tue Dec 18, 2018 2:49 pm
by rlebeau
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;
    }
}

Re: handle an exception

PostPosted: Wed Dec 19, 2018 10:24 am
by mark_c
thanks Remy