How best to add error handling in a CLR procedure call

  • I have created a VB.net dll to write text to a flat file. I created the assembly and procedures for each of the three Subs in the dll: CreateTextFile, WriteTextToFile and RenameFile.

    Now I want to add error handling. Ideally I'd want to execute like:

    declare @rtn int

    exec @rtn = KHSWriteTxt 'c:\path\file.txt', 'Text to append'

    if @rtn <> 0 select 'do something here'rather than get a .NET Framework error returned, if for example, the file or path doesn't exist.

    Here's the VB:Imports System

    Imports System.IO

    Imports System.Text

    Public Class KHSTextFile

    Public Shared Sub CreateTextFile(ByVal FileName As String)

    Dim TextFile As New StreamWriter(FileName)

    TextFile.Close()

    End Sub

    Public Shared Sub WriteTextToFile(ByVal FileName As String, ByVal Text As String)

    Dim MyWriter As System.IO.StreamWriter = System.IO.File.AppendText(FileName)

    MyWriter.Write(Text)

    MyWriter.WriteLine()

    MyWriter.Close()

    End Sub

    Public Shared Sub RenameFile(ByVal OldName As String, ByVal NewName As String)

    Dim MyFile As New FileInfo(OldName)

    MyFile.MoveTo(NewName)

    End Sub

    End Class

    Do I need change each Sub to a Function and trap and return the error code? When I go to insert a Try...Catch, I have nothing to put in the Catch. What would go in the Catch? If I made each Sub a Function I guess I could Catch Err.Number and return that. What do others do?

    Thanks all.

  • Oh well, no takers. I changed my subs to functions. Through testing I found the CreateTextFile function would overwrite an existing file, so I blocked that and returned error code 58 (File Already Exists). Also found that the WriteTextToFile function would create the file in parameter FileName if it didn't exist, so I blocked that too, returning 53 (File Not Found).

    So now I have a sp wrapper for each function in the dll that returns either 0 or the VB error code.

    declare @rtn int

    exec @rtn = KHSCreateTxt 'c:\myfile.txt'

    if @rtn <> 0 select 'I have the vb error code to help diagnose the problem'

    Here's the vb code.

    Imports System

    Imports System.IO

    Imports System.Text

    Public Class KHSTextFile

    Public Shared Function CreateTextFile(ByVal FileName As String) As Integer

    CreateTextFile = 0

    If File.Exists(FileName) Then

    CreateTextFile = 58

    Exit Function

    Else

    Try

    Dim TextFile As New StreamWriter(FileName)

    TextFile.Close()

    Catch ex As Exception

    CreateTextFile = Err.Number

    End Try

    End If

    End Function

    Public Shared Function WriteTextToFile(ByVal FileName As String, ByVal Text As String) As Integer

    WriteTextToFile = 0

    If File.Exists(FileName) <> True Then

    WriteTextToFile = 53

    Exit Function

    Else

    Try

    Dim MyWriter As System.IO.StreamWriter = System.IO.File.AppendText(FileName)

    MyWriter.Write(Text)

    MyWriter.WriteLine()

    MyWriter.Close()

    Catch ex As Exception

    WriteTextToFile = Err.Number

    End Try

    End If

    End Function

    Public Shared Function RenameFile(ByVal OldName As String, ByVal NewName As String) As Integer

    RenameFile = 0

    Try

    Dim MyFile As New FileInfo(OldName)

    MyFile.MoveTo(NewName)

    Catch ex As Exception

    RenameFile = Err.Number

    End Try

    End Function

    End Class

Viewing 2 posts - 1 through 1 (of 1 total)

You must be logged in to reply to this topic. Login to reply