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;
}
}