Finally block causing exception to throw twice with CryptoStream?

T

TC

Hey All,

I posted this to the Crypto users group and forgot to add the VB.Net users
group. I apologize for any confusion.

I have been testing a try / catch / finally block and purposely raising
exceptions and I've noticed that if an exception of "Length of the data to
decrypt is invalid." is raised with the CryptoStream object, later this
exception will get raised a second time and thrown to the caller when trying
to close the stream in a Finally block.

For example:

Try
' Do tasks that cause CryptoStream to throw invalid length while
decrypting

Catch ex as CryptographicException
' Handle exception here
Finally
If Not MemStream is Nothing then
' No 2nd exception thrown here
MemStream.Close()
End If

If Not CrypStream is Nothing then
' Exception thrown a 2nd time
CrypStream.Close()
End If


Has anyone else experienced the above?

If so, how to address the problem or is this a bug?
 
K

kimiraikkonen

Hey All,

I posted this to the Crypto users group and forgot to add the VB.Net users
group. I apologize for any confusion.

I have been testing a try / catch / finally block and purposely raising
exceptions and I've noticed that if an exception of "Length of the data to
decrypt is invalid." is raised with the CryptoStream object, later this
exception will get raised a second time and thrown to the caller when trying
to close the stream in a Finally block.

For example:

Try
' Do tasks that cause CryptoStream to throw invalid length while
decrypting

Catch ex as CryptographicException
' Handle exception here
Finally
If Not MemStream is Nothing then
' No 2nd exception thrown here
MemStream.Close()
End If

If Not CrypStream is Nothing then
' Exception thrown a 2nd time
CrypStream.Close()
End If

Has anyone else experienced the above?

If so, how to address the problem or is this a bug?

TC,
Do you experience the same problem when you use "Using-End Using"
statement instead of Try-Catch for CryptoStream object?

Thanks,

Onur Güzel
 
T

TC

Hey Onur,

So you would recommend something like the following:

MyFunction(ByVal encryptString as String) as String
Try
Using(MyCryptoStream)
' Do Crypto tasks
return MyEncryptedString
End Using
Catch ex as CryptographicException
HandleCryptoException ex
Catch ex as Exception
HandleException(ex)
return Nothing
Finally
If Not MyMemoryStream Is Nothing Then
MyMemoryStream.Close
End If
' Do not close CryptoStream here as it causes duplicitous
exception
' The 'Using' block will dispose of any CryptoStream resources
End Function

The above does not cause the duplicitous exception to be thrown.

Thanks,

Todd


Hey All,

I posted this to the Crypto users group and forgot to add the VB.Net users
group. I apologize for any confusion.

I have been testing a try / catch / finally block and purposely raising
exceptions and I've noticed that if an exception of "Length of the data to
decrypt is invalid." is raised with the CryptoStream object, later this
exception will get raised a second time and thrown to the caller when
trying
to close the stream in a Finally block.

For example:

Try
' Do tasks that cause CryptoStream to throw invalid length while
decrypting

Catch ex as CryptographicException
' Handle exception here
Finally
If Not MemStream is Nothing then
' No 2nd exception thrown here
MemStream.Close()
End If

If Not CrypStream is Nothing then
' Exception thrown a 2nd time
CrypStream.Close()
End If

Has anyone else experienced the above?

If so, how to address the problem or is this a bug?

TC,
Do you experience the same problem when you use "Using-End Using"
statement instead of Try-Catch for CryptoStream object?

Thanks,

Onur Güzel
 
K

kimiraikkonen

Hey Onur,

    So you would recommend something like the following:

    MyFunction(ByVal encryptString as String) as String
        Try
            Using(MyCryptoStream)
                ' Do Crypto tasks
                return MyEncryptedString
            End Using
        Catch ex as CryptographicException
            HandleCryptoException ex
        Catch ex as Exception
            HandleException(ex)
            return Nothing
        Finally
            If Not MyMemoryStream Is Nothing Then
                MyMemoryStream.Close
            End If
            ' Do not close CryptoStream here as it causes duplicitous
exception
            ' The 'Using' block will dispose of any CryptoStream resources
    End Function

The above does not cause the duplicitous exception to be thrown.

Thanks,

Todd












TC,
Do you experience the same problem when you use "Using-End Using"
statement instead of Try-Catch for CryptoStream object?

Thanks,

Onur Güzel- Hide quoted text -

- Show quoted text -

Hi,
If it solves your problem, it's OK, however you can also try to put
try-catch inside Using-End Using without "Finally" because "Using-End
Using" does all the disposal operation:


MyFunction(ByVal encryptString as String) as String

Using(MyCryptoStream)
Try
' Do Crypto tasks
return MyEncryptedString

Catch ex as CryptographicException
HandleCryptoException ex
' The 'Using' block will dispose of any CryptoStream
resources
End Using
End Function


Thanks,

Onur Güzel
 
T

TC

Ahhh... right... got it!

Thanks



Hey Onur,

So you would recommend something like the following:

MyFunction(ByVal encryptString as String) as String
Try
Using(MyCryptoStream)
' Do Crypto tasks
return MyEncryptedString
End Using
Catch ex as CryptographicException
HandleCryptoException ex
Catch ex as Exception
HandleException(ex)
return Nothing
Finally
If Not MyMemoryStream Is Nothing Then
MyMemoryStream.Close
End If
' Do not close CryptoStream here as it causes duplicitous
exception
' The 'Using' block will dispose of any CryptoStream resources
End Function

The above does not cause the duplicitous exception to be thrown.

Thanks,

Todd












TC,
Do you experience the same problem when you use "Using-End Using"
statement instead of Try-Catch for CryptoStream object?

Thanks,

Onur Güzel- Hide quoted text -

- Show quoted text -

Hi,
If it solves your problem, it's OK, however you can also try to put
try-catch inside Using-End Using without "Finally" because "Using-End
Using" does all the disposal operation:


MyFunction(ByVal encryptString as String) as String

Using(MyCryptoStream)
Try
' Do Crypto tasks
return MyEncryptedString

Catch ex as CryptographicException
HandleCryptoException ex
' The 'Using' block will dispose of any CryptoStream
resources
End Using
End Function


Thanks,

Onur Güzel
 
T

TC

Hey Onur,

Just to be clear, even though the 'Using' block is for the CryptoStream, it
will also dispose of the MemoryStream resources as well because it is within
that block (i.e. the 'Using' block not only applies to the IDisposable
interface of its calling object which, in this case, is the CryptoStream but
also the IDisposable interfaces available to objects within the block as
well)?

Thanks,

TC

Hey Onur,

So you would recommend something like the following:

MyFunction(ByVal encryptString as String) as String
Try
Using(MyCryptoStream)
' Do Crypto tasks
return MyEncryptedString
End Using
Catch ex as CryptographicException
HandleCryptoException ex
Catch ex as Exception
HandleException(ex)
return Nothing
Finally
If Not MyMemoryStream Is Nothing Then
MyMemoryStream.Close
End If
' Do not close CryptoStream here as it causes duplicitous
exception
' The 'Using' block will dispose of any CryptoStream resources
End Function

The above does not cause the duplicitous exception to be thrown.

Thanks,

Todd












TC,
Do you experience the same problem when you use "Using-End Using"
statement instead of Try-Catch for CryptoStream object?

Thanks,

Onur Güzel- Hide quoted text -

- Show quoted text -

Hi,
If it solves your problem, it's OK, however you can also try to put
try-catch inside Using-End Using without "Finally" because "Using-End
Using" does all the disposal operation:


MyFunction(ByVal encryptString as String) as String

Using(MyCryptoStream)
Try
' Do Crypto tasks
return MyEncryptedString

Catch ex as CryptographicException
HandleCryptoException ex
' The 'Using' block will dispose of any CryptoStream
resources
End Using
End Function


Thanks,

Onur Güzel
 
T

TC

Hey Onur,

Perhaps better said, in C#, one can do something like the following:

Using (object1, object2, object 3)
// Perform tasks
End Using

Is there a syntax for something similar in VB.Net or does one have to nest
the 'Using' statements like this:

Using (object1)
Using(object2)
// Perform tasks
End Using
End Using

Thanks,

TC

Hey Onur,

So you would recommend something like the following:

MyFunction(ByVal encryptString as String) as String
Try
Using(MyCryptoStream)
' Do Crypto tasks
return MyEncryptedString
End Using
Catch ex as CryptographicException
HandleCryptoException ex
Catch ex as Exception
HandleException(ex)
return Nothing
Finally
If Not MyMemoryStream Is Nothing Then
MyMemoryStream.Close
End If
' Do not close CryptoStream here as it causes duplicitous
exception
' The 'Using' block will dispose of any CryptoStream resources
End Function

The above does not cause the duplicitous exception to be thrown.

Thanks,

Todd












TC,
Do you experience the same problem when you use "Using-End Using"
statement instead of Try-Catch for CryptoStream object?

Thanks,

Onur Güzel- Hide quoted text -

- Show quoted text -

Hi,
If it solves your problem, it's OK, however you can also try to put
try-catch inside Using-End Using without "Finally" because "Using-End
Using" does all the disposal operation:


MyFunction(ByVal encryptString as String) as String

Using(MyCryptoStream)
Try
' Do Crypto tasks
return MyEncryptedString

Catch ex as CryptographicException
HandleCryptoException ex
' The 'Using' block will dispose of any CryptoStream
resources
End Using
End Function


Thanks,

Onur Güzel
 
K

kimiraikkonen

Hey Onur,

Perhaps better said, in C#, one can do something like the following:

Using (object1, object2, object 3)
// Perform tasks
End Using

Is there a syntax for something similar in VB.Net or does one have to nest
the 'Using' statements like this:

Using (object1)
Using(object2)
// Perform tasks
End Using
End Using

Thanks,

TC










Hi,
If it solves your problem, it's OK, however you can also try to put
try-catch inside Using-End Using without "Finally" because "Using-End
Using" does all the disposal operation:

MyFunction(ByVal encryptString as String) as String

Using(MyCryptoStream)
Try
' Do Crypto tasks
return MyEncryptedString

Catch ex as CryptographicException
HandleCryptoException ex
' The 'Using' block will dispose of any CryptoStream
resources
End Using
End Function

Thanks,

Onur Güzel

TC,
To make sure that all of your disposable resources are disposed
clearly by Using statement, you can also use seperate "Using"s for
each disposable object like:

Using block1 As New DisposableObject1
'Statements here
End Using

Using block2 As New DisposableObject2
'Statements here
End Using

and so on...

Better ideas may exist of course.

Thanks,

Onur Güzel
 
K

kimiraikkonen

TC,
To make sure that all of your disposable resources are disposed
clearly by Using statement, you can also use seperate "Using"s for
each disposable object like:

Using block1 As New DisposableObject1
'Statements here
End Using

Using block2 As New DisposableObject2
'Statements here
End Using

and so on...

Better ideas may exist of course.

Thanks,

Onur Güzel

However, as addition:
I tried nesting "Using"s and seems to work in VB.NET also:

Like:

Using w1 As IO.StreamWriter = IO.File.CreateText("c:\foo1.txt")
' code here
w1.Write("foo1")
Using w2 As IO.StreamWriter = IO.File.CreateText("c:\foo2.txt")
w2.Write("foo2")
End Using
End Using

Thanks,

Onur Güzel
 
J

Jack Jackson

In VB you can nest the Usings, or you can:

Using obj1 As New SomeObj1, obj2 As New SomeObj2, obj3 As New SomeObj3
...
End Using
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top