Forum > General
How to Capture a File Not Found Exception?
StephenMilner:
I've got this snippet of code in a private class procedure which is called by the Class constructor.
When I deliberately pass a file that doesn't exist....
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} --- try try AssignFile(Settings, SettingsFile); Reset(Settings); // This generates the exception //Statements except on e: EFileNotFoundException do // This code check fails and the else part is executed writeln (DateToSTR(Date),':', TimeToSTR(Time),':', 'File Not Found :'+SettingsFile); else Raise; // This executes end; finally try // I'm not sure this is the correct approach.... Close(Settings); except on E: Exception do writeln (DateToSTR(Date),':', TimeToSTR(Time),':', 'File Not Closed :'+SettingsFile); // This executes and writes to the console. end;
The exception
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---EFileNotFoundException doesn't appear to be the correct one.
The final exception raised is:
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---EInOutError: File not found
Should I use
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---EInOutError instead of
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---EFileNotFoundException?
How do I differentiate between other kinds of input/output errors in my exception handling?
I want code execution to terminate when the file isn't found.
balazsszekely:
Old way:
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} --- AssignFile(Settings, SettingsFile); {$I-} Reset(f); {$I+} if IOResult <> 0 then Raise EFileNotFoundException.Create('File not found!'); //..
Modern way:
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} --- if not FileExists(SettingsFile) then Raise EFileNotFoundException.Create('File not found!'); //..
StephenMilner:
Thanks.
Thanks that should simplify things
Thaddy:
Why not call FileExists() and do away with EFileNotFoundException completely?
Exceptions should be just what they are:exceptions. They should be avoided in all situations where an exceptionless codepath is possible.
In this case EFileNotFoundException is even a bit silly since FileExists() exists.. ;D
A good programmer should never burden his/hers code with superfluous exceptions.
Kays:
--- Quote from: GetMem on January 14, 2022, 05:20:43 pm ---Old way: […]
--- End quote ---
I don’t think the “old way” would use exceptions at all. Besides, IOResult does have many other results. I think the expected functionality of software is to produce reasonable and at least somewhat understandable error messages.
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---program nonExistentFileDemo(input, output, stdErr);uses sysUtils; { ❗ automagically converts all RTEs to exceptions }const FN = '/tmp/blabla';resourceString errorInvalidPath = 'Error: some component in `%0:s` inaccessible';var FD: text;begin assign(FD, FN); { `assignFile` only in objFPC mode (←objPas unit) } {$push} {$IOChecks off} reset(FD); {$pop} case IOResult of 0: begin { no error } end; 2: begin writeLn(stdErr, format(errorInvalidPath, [FN])); halt(-1); end; otherwise begin halt(-1); end; end;end.
--- Quote from: Thaddy on January 14, 2022, 06:27:51 pm ---Why not call FileExists() and do away with EFileNotFoundException completely?
--- End quote ---
TOCTOU: The file gets deleted after the successful check, but just before you actually open it for reading.
Navigation
[0] Message Index
[#] Next page