Question! Err.raise 
Author Message
 Question! Err.raise

Hello All
I have question about the way Err object works in VB. It's like this:
I have a procedure called ReplaceData, which open a file thru. a Class
oFso's OpenFile command. Now oFso has a procedure called BackupFile()
that is called if the OpenFile command was called with a ToWrite
parameter. Now if the directory where the BackupFile() operates on has
only Read access, then it should pass the Err to the calling routine
which is OpenFile which should then pass it to ReplaceData.
However it's not doing that and I can't trap it in ReplaceData() since
it's being shown as a runtime error in OpenFile.

Could someone please tell me what I'm missing here.

thanks
Sunit

Public Function ReplaceData(ByVal sNewNode As String, ByVal sNewDrive
As String, ByVal blnReplaceFirstLine As Boolean) As String

    oFso.OpenFile ToWrite
    oFso.WriteString sTemp
    oFso.CloseFile

    ReplaceData = "Replaced all data in " & sFileName & "."
    Exit Function

ErrorHandler:
    Err.Raise Err.Number, MODULE_NAME & ".ReplaceData" &
Err.Description

End Function

'From oFso's Class
Public Sub OpenFile(Optional OpMode As OpenMode = ToRead, _
                    Optional CreateIfNotExist As Boolean = False)

   On Error GoTo ErrorHandler

   If OpMode <> ToRead Then
      BackupFile       'Backup if file is not Opened for Reading
      Set oTxt = oFso.OpenTextFile(sFileName, OpMode,
CreateIfNotExist)
   Else
      fnum = FreeFile()
      Open sFileName For Input As #fnum
      isOpen = True
   End If
   Exit Sub

ErrorHandler:
   If isOpen Then Close #fnum
   If Err.Number <> 0 Then Err.Raise Err.Number, MODULE_NAME &
".OpenFile", Err.Description  'It stops here instead of passing it to
ReplaceData()

End Sub

Private Sub BackupFile()
   Dim sBackupFile, oTrace As New CTracer
   On Error GoTo ErrorHandler

   sBackupFile = sFileName & ".bk"
   oTrace.Enter "Backupfile->" & sBackupFile
   oFso.CopyFile sFileName, sBackupFile

ErrorHandler:
   If Err.Number <> 0 Then Err.Raise Err.Number, MODULE_NAME &
".Backup", Err.Description

End Sub



Mon, 08 Mar 2004 02:13:42 GMT  
 Question! Err.raise
You should learn the new error handling techniques. Here is a start...

Try
    oFso.OpenFile ToWrite
Catch MyError as Exception
    'do whatever you want with the error

    'If you want, you can pass the exception up the chain
    Throw New Exception( "This is my message", MyError)
    'Notice that I passed the old exception into the constructor for the new
exception. That way the programmer can see both my message and the original
one.

End Try

--
Jonathan Allen


Quote:
> Hello All
> I have question about the way Err object works in VB. It's like this:
> I have a procedure called ReplaceData, which open a file thru. a Class
> oFso's OpenFile command. Now oFso has a procedure called BackupFile()
> that is called if the OpenFile command was called with a ToWrite
> parameter. Now if the directory where the BackupFile() operates on has
> only Read access, then it should pass the Err to the calling routine
> which is OpenFile which should then pass it to ReplaceData.
> However it's not doing that and I can't trap it in ReplaceData() since
> it's being shown as a runtime error in OpenFile.

> Could someone please tell me what I'm missing here.

> thanks
> Sunit

> Public Function ReplaceData(ByVal sNewNode As String, ByVal sNewDrive
> As String, ByVal blnReplaceFirstLine As Boolean) As String

>     oFso.OpenFile ToWrite
>     oFso.WriteString sTemp
>     oFso.CloseFile

>     ReplaceData = "Replaced all data in " & sFileName & "."
>     Exit Function

> ErrorHandler:
>     Err.Raise Err.Number, MODULE_NAME & ".ReplaceData" &
> Err.Description

> End Function

> 'From oFso's Class
> Public Sub OpenFile(Optional OpMode As OpenMode = ToRead, _
>                     Optional CreateIfNotExist As Boolean = False)

>    On Error GoTo ErrorHandler

>    If OpMode <> ToRead Then
>       BackupFile       'Backup if file is not Opened for Reading
>       Set oTxt = oFso.OpenTextFile(sFileName, OpMode,
> CreateIfNotExist)
>    Else
>       fnum = FreeFile()
>       Open sFileName For Input As #fnum
>       isOpen = True
>    End If
>    Exit Sub

> ErrorHandler:
>    If isOpen Then Close #fnum
>    If Err.Number <> 0 Then Err.Raise Err.Number, MODULE_NAME &
> ".OpenFile", Err.Description  'It stops here instead of passing it to
> ReplaceData()

> End Sub

> Private Sub BackupFile()
>    Dim sBackupFile, oTrace As New CTracer
>    On Error GoTo ErrorHandler

>    sBackupFile = sFileName & ".bk"
>    oTrace.Enter "Backupfile->" & sBackupFile
>    oFso.CopyFile sFileName, sBackupFile

> ErrorHandler:
>    If Err.Number <> 0 Then Err.Raise Err.Number, MODULE_NAME &
> ".Backup", Err.Description

> End Sub



Mon, 08 Mar 2004 03:40:06 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Err.Raise in OLE Server EXE not raising error in calling program

2. Err.Raise - error not being re-raised all the way to top of the call stack

3. raise.err question

4. Err.Raise Question

5. Err.Raise Causes Automation Error

6. Raising Err in Class Module

7. Err.Raise Number starting point

8. err.raise not trappable in class module

9. Err.Raise causes Error

10. Problems when using Err.Raise in a public method of an activex

11. Err.Raise : Automation Error

12. Getting mad with Casading Err.raise

 

 
Powered by phpBB® Forum Software