I think I have asked this but in case I wasn’t specific.
I am trying to get a Try Catch working for file errors.
I changed the name to test and it generates the error 101 but it doesn’t generate a Catch.
Am I missing something?
Try
ts = DecodeBase64(pArrayFile(rowcnt), Encdng )
pf = New FolderItem(ts)
If pf <> Nil Or ( pf <> Nil And pf.Exists ) Then
....
Catch err As IOException //60 lines of code later
//messageDialog appears
End Try
not sure I follow your question
in the debugger IF you have BREAK ON EXCEPTIONS turned on then if there is one you will stop where the exception is raised
but continuing to step should then take you to the catch & through that code
Would someone check this for the Try, Catch and End Try.
I am only concerned that the Catch should fire
In the debugger, pf does verify that the file doesn’t exist and there is a lasterror code of 101,
The Catch should be firing, but it doesn’t.
[code]For rowcnt = 0 to 4//inner For
try
ts = DecodeBase64(pArrayFile(rowcnt), Encdng )
pf = New FolderItem(ts)
If pf = Nil Or (pf <> Nil And pf.Exists = False ) Then
fPref = True//It will set off the dialog for continuing because a folder disappeared
ElseIf pf <> Nil And pf.Exists Then
If rowcnt < 4 Then
pf = App.fileErrorTest(pf, MnStuff.PrefTitles(rowcnt), False, True, True )
if pf <> Nil And pf.Exists Then
fPref = False
Else
fPref = True
End If
ElseIf rowcnt = 4 Then
pf = App.fileErrorTest(pf, MnStuff.PrefTitles(rowcnt), False, False, True )
if pf <> Nil And pf.Exists Then
fPref = False
Else
fPref = True
End If
End If//If rowcnt < 4 Then
End If
if fpref = False then
if rowcnt = 0 then
DocFolder = pf//rowcnt = 0
IniHerd.TDictFlder = ThColl.Child(DocFolder.name)
If IniHerd.TDictFlder = Nil Or IniHerd.TDictFlder.Exists = False Then
IniHerd.TDictFlder.CreateFolder
End If//If IniHerd.TDictFlder = Nil Or IniHerd.TDictFlder.Exists = False Then
ElseIf rowcnt = 1 then
IniHerd.WrdSndFldr = pf//rowcnt = 1
ElseIf rowcnt = 2 then
IniHerd.SentSndFldr = pf//rowcnt = 2
ElseIf rowcnt = 3 then
IniHerd.ImgFldr = pf//rowcnt = 3
ElseIf rowcnt = 4 then
GrmFileName = pf//rowcnt = 4 pf.Exists already tested
If GrmFileName <> Nil And GrmFileName.Exists Then
If GrmInst <> Nil Then GrmInst = Nil
GrmInst = New GrammWNotesCL
If GrmInst.GrmUbnd = -1 Then MnStuff.PChange = True
Else
GrmInst = Nil
End If
fPref = True
End If
End If// if fpref = True then
Catch err as IOException
If fPref Then//So the dialog doesn’t run more than once
If fPref Then fPref = False//It made it here so it’s switched
Var d as New MessageDialog //declare the MessageDialog object
Var b as MessageDialogButton //for handling the result
d.icon=MessageDialog.GraphicCaution //display warning icon
d.ActionButton.Caption = "Cancel Load"
d.CancelButton.Visible = True //show the Cancel button
d.CancelButton.Caption = "Continue"
d.Message = MnStuff.PrefTitles(rowcnt) + " Location is Invalid"
d.Explanation = "If Your File Preferences Could Be Bad" + EOL + "Then Press 'Cancel Load'."
b=d.ShowModal //display the dialog
Select Case b //determine which button was pressed.
Case d.ActionButton
//user pressed Stop Load
pArrayFile.ResizeTo(-1)
MnStuff.PChange = True
Exit For
Case d.CancelButton
MnStuff.PChange = False
fPref = True//will go to thru the Prefs
End select
End If// if fpref = True then
pf = Nil//so it can continue
Using pf.Exists won’t raise an IOException error if the file doesn’t exist and if you don’t actually try to use the FolderItem and generate the error 101 you won’t catch it
The LastErrorCode of 101 is actually created by the pf.Exists check and should be ignored as this is now deprecated. You actually need to try and use the file (e.g. Open) to raise the IOException for it to be caught.
If you’re running in debug mode the IDE will break before the catch, so you’ll need to use #Pragma BreakOnExceptions False to see how the code will run “in the wild”
Site note, just looking at your code, if the Nil check fails it never gets to the Exist so you don’t need the double check with the Or, just use:
If it helps and if you want to keep with the code flow you have above then you can always insert the following just after the first fPref = True above and it will raise an IOException and drop you into the catch.
Var e as New IOException
e.Message = "Some message" // can comment out this step as you don't use the message in the dialog anyway
e.ErrorNumber = 101 // can comment out this step as you don't use the errornumber in the dialog anyway
Raise e
or just the following if you don’t want to set a message or error number:
Thanks. Whenever I have a file that can be accessed, I run a little method “FileErrorTest” that reports of existence,etc
It goes through 100 to 105 and gives a message. If the error codes change, I can hopefully rewrite.
I hadn’t put that bit together. The code is great. That’s probably why I’m still an amateur and not professional.
I don’t think I have a case where I actually need to Raise an exception, but I’ll let it roll around.
I also haven’t actually tested my app with anyone else, but it’s soon. Then I’ll know if I have to idiot proof it more with exceptions.
[quote=491795:@Arthur Gabhart]Whenever I have a file that can be accessed, I run a little method “FileErrorTest” that reports of existence,etc
It goes through 100 to 105 and gives a message. If the error codes change, I can hopefully rewrite.[/quote]
I’m not usually getting a lot of errors when the file can be accessed
[quote=491681:@Arthur Gabhart]
I am trying to get a Try Catch working for file errors.
I changed the name to test and it generates the error 101 but it doesn’t generate a Catch.
Am I missing something?
Try
ts = DecodeBase64(pArrayFile(rowcnt), Encdng )
pf = New FolderItem(ts)
If pf <> Nil Or ( pf <> Nil And pf.Exists ) Then
....
Catch err As IOException //60 lines of code later
//messageDialog appears
End Try
[/quote]
Why so much code in the Try, given you’re just looking for an IOException? Put the code that might cause such an exception in a short Try/Catch. Further, if the exception gets raised, what is the point of executing all the other lines of code anyway? Log the error and return.