Finally block causing exception to throw twice with CryptoStream?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • TC

    Finally block causing exception to throw twice with CryptoStream?

    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 CryptographicEx ception
    ' 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.Clos e()
    End If


    Has anyone else experienced the above?

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




  • kimiraikkonen

    #2
    Re: Finally block causing exception to throw twice with CryptoStream?

    On May 18, 7:36 pm, "TC" <getmyemai...@y ahoo.comwrote:
    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 CryptographicEx ception
    ' 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.Clos e()
    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

    Comment

    • TC

      #3
      Re: Finally block causing exception to throw twice with CryptoStream?

      Hey Onur,

      So you would recommend something like the following:

      MyFunction(ByVa l encryptString as String) as String
      Try
      Using(MyCryptoS tream)
      ' Do Crypto tasks
      return MyEncryptedStri ng
      End Using
      Catch ex as CryptographicEx ception
      HandleCryptoExc eption 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


      "kimiraikko nen" <kimiraikkonen8 5@gmail.comwrot e in message
      news:e75b9596-71bd-485e-863b-9fcc301312f6@a2 3g2000hsc.googl egroups.com...
      On May 18, 7:36 pm, "TC" <getmyemai...@y ahoo.comwrote:
      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 CryptographicEx ception
      ' 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.Clos e()
      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


      Comment

      • kimiraikkonen

        #4
        Re: Finally block causing exception to throw twice with CryptoStream?

        On May 19, 1:10 pm, "TC" <getmyemai...@y ahoo.comwrote:
        Hey Onur,
        >
            So you would recommend something like the following:
        >
            MyFunction(ByVa l encryptString as String) as String
                Try
                    Using(MyCryptoS tream)
                        ' Do Crypto tasks
                        return MyEncryptedStri ng
                    End Using
                Catch ex as CryptographicEx ception
                    HandleCryptoExc eption 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
        >
        "kimiraikko nen" <kimiraikkone.. .@gmail.comwrot e in message
        >
        news:e75b9596-71bd-485e-863b-9fcc301312f6@a2 3g2000hsc.googl egroups.com...
        On May 18, 7:36 pm, "TC" <getmyemai...@y ahoo.comwrote:
        >
        >
        >
        >
        >
        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 CryptographicEx ception
            ' 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.Clos e()
            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- 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(ByVa l encryptString as String) as String

        Using(MyCryptoS tream)
        Try
        ' Do Crypto tasks
        return MyEncryptedStri ng

        Catch ex as CryptographicEx ception
        HandleCryptoExc eption ex
        ' The 'Using' block will dispose of any CryptoStream
        resources
        End Using
        End Function


        Thanks,

        Onur Güzel

        Comment

        • TC

          #5
          Re: Finally block causing exception to throw twice with CryptoStream?

          Ahhh... right... got it!

          Thanks



          "kimiraikko nen" <kimiraikkonen8 5@gmail.comwrot e in message
          news:725ff39a-f310-49da-a447-a8bd6a563b3e@r6 6g2000hsg.googl egroups.com...
          On May 19, 1:10 pm, "TC" <getmyemai...@y ahoo.comwrote:
          Hey Onur,
          >
          So you would recommend something like the following:
          >
          MyFunction(ByVa l encryptString as String) as String
          Try
          Using(MyCryptoS tream)
          ' Do Crypto tasks
          return MyEncryptedStri ng
          End Using
          Catch ex as CryptographicEx ception
          HandleCryptoExc eption 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
          >
          "kimiraikko nen" <kimiraikkone.. .@gmail.comwrot e in message
          >
          news:e75b9596-71bd-485e-863b-9fcc301312f6@a2 3g2000hsc.googl egroups.com...
          On May 18, 7:36 pm, "TC" <getmyemai...@y ahoo.comwrote:
          >
          >
          >
          >
          >
          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 CryptographicEx ception
          ' 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.Clos e()
          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- 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(ByVa l encryptString as String) as String

          Using(MyCryptoS tream)
          Try
          ' Do Crypto tasks
          return MyEncryptedStri ng

          Catch ex as CryptographicEx ception
          HandleCryptoExc eption ex
          ' The 'Using' block will dispose of any CryptoStream
          resources
          End Using
          End Function


          Thanks,

          Onur Güzel


          Comment

          • TC

            #6
            Re: Finally block causing exception to throw twice with CryptoStream?

            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

            "kimiraikko nen" <kimiraikkonen8 5@gmail.comwrot e in message
            news:725ff39a-f310-49da-a447-a8bd6a563b3e@r6 6g2000hsg.googl egroups.com...
            On May 19, 1:10 pm, "TC" <getmyemai...@y ahoo.comwrote:
            Hey Onur,
            >
            So you would recommend something like the following:
            >
            MyFunction(ByVa l encryptString as String) as String
            Try
            Using(MyCryptoS tream)
            ' Do Crypto tasks
            return MyEncryptedStri ng
            End Using
            Catch ex as CryptographicEx ception
            HandleCryptoExc eption 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
            >
            "kimiraikko nen" <kimiraikkone.. .@gmail.comwrot e in message
            >
            news:e75b9596-71bd-485e-863b-9fcc301312f6@a2 3g2000hsc.googl egroups.com...
            On May 18, 7:36 pm, "TC" <getmyemai...@y ahoo.comwrote:
            >
            >
            >
            >
            >
            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 CryptographicEx ception
            ' 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.Clos e()
            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- 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(ByVa l encryptString as String) as String

            Using(MyCryptoS tream)
            Try
            ' Do Crypto tasks
            return MyEncryptedStri ng

            Catch ex as CryptographicEx ception
            HandleCryptoExc eption ex
            ' The 'Using' block will dispose of any CryptoStream
            resources
            End Using
            End Function


            Thanks,

            Onur Güzel


            Comment

            • TC

              #7
              Re: Finally block causing exception to throw twice with CryptoStream?

              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

              "kimiraikko nen" <kimiraikkonen8 5@gmail.comwrot e in message
              news:725ff39a-f310-49da-a447-a8bd6a563b3e@r6 6g2000hsg.googl egroups.com...
              On May 19, 1:10 pm, "TC" <getmyemai...@y ahoo.comwrote:
              Hey Onur,
              >
              So you would recommend something like the following:
              >
              MyFunction(ByVa l encryptString as String) as String
              Try
              Using(MyCryptoS tream)
              ' Do Crypto tasks
              return MyEncryptedStri ng
              End Using
              Catch ex as CryptographicEx ception
              HandleCryptoExc eption 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
              >
              "kimiraikko nen" <kimiraikkone.. .@gmail.comwrot e in message
              >
              news:e75b9596-71bd-485e-863b-9fcc301312f6@a2 3g2000hsc.googl egroups.com...
              On May 18, 7:36 pm, "TC" <getmyemai...@y ahoo.comwrote:
              >
              >
              >
              >
              >
              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 CryptographicEx ception
              ' 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.Clos e()
              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- 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(ByVa l encryptString as String) as String

              Using(MyCryptoS tream)
              Try
              ' Do Crypto tasks
              return MyEncryptedStri ng

              Catch ex as CryptographicEx ception
              HandleCryptoExc eption ex
              ' The 'Using' block will dispose of any CryptoStream
              resources
              End Using
              End Function


              Thanks,

              Onur Güzel


              Comment

              • kimiraikkonen

                #8
                Re: Finally block causing exception to throw twice with CryptoStream?

                On May 20, 4:35 pm, "TC" <getmyemai...@y ahoo.comwrote:
                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
                >
                "kimiraikko nen" <kimiraikkone.. .@gmail.comwrot e in message
                >
                news:725ff39a-f310-49da-a447-a8bd6a563b3e@r6 6g2000hsg.googl egroups.com...
                On May 19, 1:10 pm, "TC" <getmyemai...@y ahoo.comwrote:
                >
                >
                >
                Hey Onur,
                >
                So you would recommend something like the following:
                >
                MyFunction(ByVa l encryptString as String) as String
                Try
                Using(MyCryptoS tream)
                ' Do Crypto tasks
                return MyEncryptedStri ng
                End Using
                Catch ex as CryptographicEx ception
                HandleCryptoExc eption 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
                >
                "kimiraikko nen" <kimiraikkone.. .@gmail.comwrot e in message
                >
                news:e75b9596-71bd-485e-863b-9fcc301312f6@a2 3g2000hsc.googl egroups.com...
                On May 18, 7:36 pm, "TC" <getmyemai...@y ahoo.comwrote:
                >
                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 CryptographicEx ception
                ' 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.Clos e()
                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- 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(ByVa l encryptString as String) as String
                >
                Using(MyCryptoS tream)
                Try
                ' Do Crypto tasks
                return MyEncryptedStri ng
                >
                Catch ex as CryptographicEx ception
                HandleCryptoExc eption 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 DisposableObjec t1
                'Statements here
                End Using

                Using block2 As New DisposableObjec t2
                'Statements here
                End Using

                and so on...

                Better ideas may exist of course.

                Thanks,

                Onur Güzel

                Comment

                • kimiraikkonen

                  #9
                  Re: Finally block causing exception to throw twice with CryptoStream?

                  On May 20, 7:36 pm, kimiraikkonen <kimiraikkone.. .@gmail.comwrot e:
                  On May 20, 4:35 pm, "TC" <getmyemai...@y ahoo.comwrote:
                  >
                  >
                  >
                  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
                  >
                  "kimiraikko nen" <kimiraikkone.. .@gmail.comwrot e in message
                  >
                  news:725ff39a-f310-49da-a447-a8bd6a563b3e@r6 6g2000hsg.googl egroups.com...
                  On May 19, 1:10 pm, "TC" <getmyemai...@y ahoo.comwrote:
                  >
                  Hey Onur,
                  >
                  So you would recommend something like the following:
                  >
                  MyFunction(ByVa l encryptString as String) as String
                  Try
                  Using(MyCryptoS tream)
                  ' Do Crypto tasks
                  return MyEncryptedStri ng
                  End Using
                  Catch ex as CryptographicEx ception
                  HandleCryptoExc eption 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
                  >
                  "kimiraikko nen" <kimiraikkone.. .@gmail.comwrot e in message
                  >
                  >news:e75b959 6-71bd-485e-863b-9fcc301312f6@a2 3g2000hsc.googl egroups.com....
                  On May 18, 7:36 pm, "TC" <getmyemai...@y ahoo.comwrote:
                  >
                  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 CryptographicEx ception
                  ' 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.Clos e()
                  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- 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(ByVa l encryptString as String) as String
                  >
                  Using(MyCryptoS tream)
                  Try
                  ' Do Crypto tasks
                  return MyEncryptedStri ng
                  >
                  Catch ex as CryptographicEx ception
                  HandleCryptoExc eption 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 DisposableObjec t1
                  'Statements here
                  End Using
                  >
                  Using block2 As New DisposableObjec t2
                  '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.CreateT ext("c:\foo1.tx t")
                  ' code here
                  w1.Write("foo1" )
                  Using w2 As IO.StreamWriter = IO.File.CreateT ext("c:\foo2.tx t")
                  w2.Write("foo2" )
                  End Using
                  End Using

                  Thanks,

                  Onur Güzel

                  Comment

                  • Jack Jackson

                    #10
                    Re: Finally block causing exception to throw twice with CryptoStream?

                    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

                    On Tue, 20 May 2008 09:35:49 -0400, "TC" <getmyemails2@y ahoo.com>
                    wrote:
                    >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
                    >
                    >"kimiraikkonen " <kimiraikkonen8 5@gmail.comwrot e in message
                    >news:725ff39 a-f310-49da-a447-a8bd6a563b3e@r6 6g2000hsg.googl egroups.com...
                    >On May 19, 1:10 pm, "TC" <getmyemai...@y ahoo.comwrote:
                    >Hey Onur,
                    >>
                    >So you would recommend something like the following:
                    >>
                    >MyFunction(ByV al encryptString as String) as String
                    >Try
                    >Using(MyCrypto Stream)
                    >' Do Crypto tasks
                    >return MyEncryptedStri ng
                    >End Using
                    >Catch ex as CryptographicEx ception
                    >HandleCryptoEx ception ex
                    >Catch ex as Exception
                    >HandleExceptio n(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
                    >>
                    >"kimiraikkonen " <kimiraikkone.. .@gmail.comwrot e in message
                    >>
                    >news:e75b959 6-71bd-485e-863b-9fcc301312f6@a2 3g2000hsc.googl egroups.com...
                    >On May 18, 7:36 pm, "TC" <getmyemai...@y ahoo.comwrote:
                    >>
                    >>
                    >>
                    >>
                    >>
                    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 CryptographicEx ception
                    ' 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.Clos e()
                    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- 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(ByV al encryptString as String) as String
                    >
                    >Using(MyCrypto Stream)
                    Try
                    ' Do Crypto tasks
                    return MyEncryptedStri ng
                    >
                    Catch ex as CryptographicEx ception
                    HandleCryptoExc eption ex
                    ' The 'Using' block will dispose of any CryptoStream
                    >resources
                    End Using
                    End Function
                    >
                    >
                    >Thanks,
                    >
                    >Onur Güzel
                    >

                    Comment

                    Working...