Riccardo Neri
Riccardo Neri

Reputation: 810

Try Catch in .net

I was searching the most effective and elegant way do to the follows:

The function will just deserialize an XML object.

The main possible results are:

If I write this way, the reader won't be closed in case of corrupted file. IF I write the reader.close() inside finally block, It gaves me a warning about using read before assigning it a value.

So, how can I solve this inside the sub??

Public Function DeSerializzaXML(ByVal FileName As String, ByRef tmpObj As Object, ByVal tmpClass As Type) As Boolean
    Dim serializer As XmlSerializer
    Dim reader As StreamReader
    Dim tmpSuccess As Boolean = False
    Try
        serializer = New XmlSerializer(tmpClass)
        reader = New StreamReader(FileName)
        tmpObj = serializer.Deserialize(reader)
        reader.Close()
        tmpSuccess = True
    Catch ex As Exception
        AggiungiRigaSuFile(ErroriPath, FileName + ", " + ex.Message, Configurazione.DiagnosticaOff, True)
    Finally

    End Try

    Return tmpSuccess
End Function

Upvotes: 0

Views: 2322

Answers (7)

Nooblet
Nooblet

Reputation: 1

do this

if io.file.exist(filename) then
    return io.file.readallbytes(filename)
end if

Upvotes: 0

Sebastian Siek
Sebastian Siek

Reputation: 2075

with regards to try catch, you should be catching specific exceptions

Catch ex As XmlException

or

Catch ex As IOException

you could try and eliminate the IO exception by checking if file exists and is accessible (exception could still be raised, but it's better to avoid it).

Hope that helps.

Upvotes: 0

Branko Dimitrijevic
Branko Dimitrijevic

Reputation: 52107

reader = New StreamReader(FileName)
Try
   ...
Finally
   reader.Dispose()
End Try

Or just use Using.

Upvotes: 0

The Ed R
The Ed R

Reputation: 326

I'd suggest you explicitly check to see if the file exists rather than relying on an exception being thrown, it’s more efficient and it allows you to report a more accurate error message.

 If(File.Exists(curFile)) ...

Upvotes: 0

Jodrell
Jodrell

Reputation: 35716

Check to see if it has a value before accessing it.

If reader IsNot Nothing Then ...

Better still, use a Using block, then you won't have to worry in the finally.

Using reader = New StreamReader(FileName)
    ...
End Using

Upvotes: 2

Igarioshka
Igarioshka

Reputation: 677

try using a 'using' statement in the reader initialisation. if it's disposable, then when the Dispose method is called, the reader will be closed automatically

more from microsoft

as an alternative, you can use multiple catch statements on different exception types.

Upvotes: 5

Daniel Hilgarth
Daniel Hilgarth

Reputation: 174309

Just initialize the reader with Nothing and only dispose it in the finally block if it is not Nothing:

Dim reader As StreamReader = Nothing
Try
    ...
Finally

    If reader IsNot Nothing Then

        reader.Dispose();

    End If

End Try

As I seldom do VB.NET, I am not exactly sure about the syntax, please fix any errors yourself. The idea of the code should be clear.

Upvotes: 4

Related Questions