Using vs. Try Catch

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

    #1

    Using vs. Try Catch

    my old code

    Try
    Dim sw As New System.io.Strea mWriter(fileNam e, True)
    sw.WriteLine(st rToWrite)
    sw.Close()
    Catch
    End Try

    my new code

    Using sw As New System.IO.Strea mWriter(fileNam e, True)
    sw.WriteLine(st rToWrite)
    End Using

    If I have experience an exception in any part of the new code will using
    prevent it from crashing the program? I don't think so. Do I need to
    do this?

    Using sw As New System.IO.Strea mWriter(fileNam e, True)
    try
    sw.WriteLine(st rToWrite)
    catch
    endtry
    end using

    What happens if sw As New System.IO.Strea mWriter(fileNam e, True) throws
    an exception? Can that line throw an exception?

    P.S. Yes, I understand I'm just hiding the exception. That's how I need
    it to work.

  • rowe_newsgroups

    #2
    Re: Using vs. Try Catch

    On Jun 27, 8:45 am, cj <c...@nospam.no spamwrote:
    my old code
    >
    Try
    Dim sw As New System.io.Strea mWriter(fileNam e, True)
    sw.WriteLine(st rToWrite)
    sw.Close()
    Catch
    End Try
    >
    my new code
    >
    Using sw As New System.IO.Strea mWriter(fileNam e, True)
    sw.WriteLine(st rToWrite)
    End Using
    >
    If I have experience an exception in any part of the new code will using
    prevent it from crashing the program? I don't think so. Do I need to
    do this?
    >
    Using sw As New System.IO.Strea mWriter(fileNam e, True)
    try
    sw.WriteLine(st rToWrite)
    catch
    endtry
    end using
    >
    What happens if sw As New System.IO.Strea mWriter(fileNam e, True) throws
    an exception? Can that line throw an exception?
    >
    P.S. Yes, I understand I'm just hiding the exception. That's how I need
    it to work.
    If I have experience an exception in any part of the new code will using
    prevent it from crashing the program?
    No, using is pretty much the same as a try finally block that calls
    dispose in the finally block, so it does no error handling (but will
    still call dispose on an error)
    What happens if sw As New System.IO.Strea mWriter(fileNam e, True) throws
    an exception? Can that line throw an exception?
    Kaboom!
    Do I need to do this?
    >
    Using sw As New System.IO.Strea mWriter(fileNam e, True)
    try
    sw.WriteLine(st rToWrite)
    catch
    endtry
    end using
    I would use the following if concerned with constructor exceptions:

    Try
    Using sw As New StreamWriter(fi leName, True)
    sw.WriteLine(st rToWrite)
    End Using
    Catch
    ' Do Nothing
    End Try


    Thanks,

    Seth Rowe

    Comment

    • Omar Abid

      #3
      Re: Using vs. Try Catch

      On Jun 27, 2:45 pm, cj <c...@nospam.no spamwrote:
      my old code
      >
      Try
      Dim sw As New System.io.Strea mWriter(fileNam e, True)
      sw.WriteLine(st rToWrite)
      sw.Close()
      Catch
      End Try
      >
      my new code
      >
      Using sw As New System.IO.Strea mWriter(fileNam e, True)
      sw.WriteLine(st rToWrite)
      End Using
      >
      If I have experience an exception in any part of the new code will using
      prevent it from crashing the program? I don't think so. Do I need to
      do this?
      >
      Using sw As New System.IO.Strea mWriter(fileNam e, True)
      try
      sw.WriteLine(st rToWrite)
      catch
      endtry
      end using
      >
      What happens if sw As New System.IO.Strea mWriter(fileNam e, True) throws
      an exception? Can that line throw an exception?
      >
      P.S. Yes, I understand I'm just hiding the exception. That's how I need
      it to work.
      you code is not complete
      this is complete code :
      Using sw As New System.IO.Strea mWriter(fileNam e, True)
      try
      sw.WriteLine(st rToWrite)
      catch ex as exception
      msgbox ex.tostring()
      endtry
      end using
      You can also make a spc sub for error (exc())
      try
      sw.WriteLine(st rToWrite)
      catch ex as exception
      msgbox ex.tostring()
      exc()
      endtry
      end using

      Omar Abid

      Comment

      • Linda Liu [MSFT]

        #4
        RE: Using vs. Try Catch

        Hi Cj,

        The Using statement declares the beginning of a Using block and optionally
        acquires the system resources that the block controls.

        A Using block behaves like a Try...Finally construction in which the Try
        block uses the resources and the Finally block disposes of them. Because of
        this, the Using block guarantees disposal of the resources, no matter how
        you exit the block. This is true even in the case of an unhandled
        exception, except for a StackOverflowEx ception.

        If you need to handle an exception that might occur within the Using block,
        you can add a complete Try...Finally construction to it. For example:

        Using sw As New System.IO.Strea mWriter(fileNam e, True)
        Try
        sw.WriteLine(st rToWrite)
        sw.Close() ' I think you still need to call the Close
        method to close the stream when you're using the Using statement
        Catch ex As Exception

        End Try
        End Using

        If there is an exception when the Using statement is acquiring a resource,
        i.e. in your case, sw As New System.IO.Strea mWriter(fileNam e, True) throws
        an exception, CLR will catch this exception and show a default exception
        dialog. You could add a Try..Catch construction to the complete Using block
        to catch the exception occurring in the Using statement as well as within
        the Using block.

        Try
        Using sw As New System.IO.Strea mWriter(filenam e, True)
        sw.WriteLine(st rToWrite)
        sw.Close()
        End Using
        Catch ex As Exception

        End Try

        Hope this helps.
        If you have any question, please feel free to let me know.

        Sincerely,
        Linda Liu
        Microsoft Online Community Support

        =============== =============== =============== =====
        Get notification to my posts through email? Please refer to
        http://msdn.microsoft.com/subscripti...ult.aspx#notif
        ications.

        Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
        where an initial response from the community or a Microsoft Support
        Engineer within 1 business day is acceptable. Please note that each follow
        up response may take approximately 2 business days as the support
        professional working with you may need further investigation to reach the
        most efficient resolution. The offering is not appropriate for situations
        that require urgent, real-time or phone-based interactions or complex
        project analysis and dump analysis issues. Issues of this nature are best
        handled working with a dedicated Microsoft Support Engineer by contacting
        Microsoft Customer Support Services (CSS) at
        http://msdn.microsoft.com/subscripti...t/default.aspx.
        =============== =============== =============== =====

        This posting is provided "AS IS" with no warranties, and confers no rights.

        Comment

        • cj

          #5
          Re: Using vs. Try Catch

          Thank you.

          Linda Liu [MSFT] wrote:
          Hi Cj,
          >
          The Using statement declares the beginning of a Using block and optionally
          acquires the system resources that the block controls.
          >
          A Using block behaves like a Try...Finally construction in which the Try
          block uses the resources and the Finally block disposes of them. Because of
          this, the Using block guarantees disposal of the resources, no matter how
          you exit the block. This is true even in the case of an unhandled
          exception, except for a StackOverflowEx ception.
          >
          If you need to handle an exception that might occur within the Using block,
          you can add a complete Try...Finally construction to it. For example:
          >
          Using sw As New System.IO.Strea mWriter(fileNam e, True)
          Try
          sw.WriteLine(st rToWrite)
          sw.Close() ' I think you still need to call the Close
          method to close the stream when you're using the Using statement
          Catch ex As Exception
          >
          End Try
          End Using
          >
          If there is an exception when the Using statement is acquiring a resource,
          i.e. in your case, sw As New System.IO.Strea mWriter(fileNam e, True) throws
          an exception, CLR will catch this exception and show a default exception
          dialog. You could add a Try..Catch construction to the complete Using block
          to catch the exception occurring in the Using statement as well as within
          the Using block.
          >
          Try
          Using sw As New System.IO.Strea mWriter(filenam e, True)
          sw.WriteLine(st rToWrite)
          sw.Close()
          End Using
          Catch ex As Exception
          >
          End Try
          >
          Hope this helps.
          If you have any question, please feel free to let me know.
          >
          Sincerely,
          Linda Liu
          Microsoft Online Community Support
          >
          =============== =============== =============== =====
          Get notification to my posts through email? Please refer to
          http://msdn.microsoft.com/subscripti...ult.aspx#notif
          ications.
          >
          Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
          where an initial response from the community or a Microsoft Support
          Engineer within 1 business day is acceptable. Please note that each follow
          up response may take approximately 2 business days as the support
          professional working with you may need further investigation to reach the
          most efficient resolution. The offering is not appropriate for situations
          that require urgent, real-time or phone-based interactions or complex
          project analysis and dump analysis issues. Issues of this nature are best
          handled working with a dedicated Microsoft Support Engineer by contacting
          Microsoft Customer Support Services (CSS) at
          http://msdn.microsoft.com/subscripti...t/default.aspx.
          =============== =============== =============== =====
          >
          This posting is provided "AS IS" with no warranties, and confers no rights.
          >

          Comment

          Working...