Try - Catch Sample Programs

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pbala
    New Member
    • Dec 2008
    • 37

    Try - Catch Sample Programs

    Hello sir,
    Please Give me any examples of Exception Handling Techniques in VB.Net 2005.

    Thanks sir.....
  • lotus18
    Contributor
    • Nov 2007
    • 865

    #2
    Originally posted by pbala
    Hello sir,
    Please Give me any examples of Exception Handling Techniques in VB.Net 2005.

    Thanks sir.....
    Here is one example on how to display help contents.
    Code:
            
    Try
       'Check if the there is Help.chm
       'Help.ShowHelp(Me, Application.StartupPath & "\Help.chm") Or
       System.Diagnostics.Process.Start(Application.StartupPath & "\Help.chm")
    Catch ex As Exception
       'Display error message if the file is not found
       MessageBox.Show(ex.Message, "Help", MessageBoxButtons.OK, MessageBoxIcon.Error)
    End Try


    Rey Sean

    Comment

    • sashi
      Recognized Expert Top Contributor
      • Jun 2006
      • 1749

      #3
      You try to catch, you catch and you throw :)

      Comment

      • dminder
        New Member
        • Dec 2008
        • 2

        #4
        Dont forget finally!!

        There is another option for Try....Catch Blocks. It is finally:

        Code:
        Try
        'Put your code in here
        Catch nex as NullReferenceException
           'This is how you can catch specific types of exceptions and handle them separately.
        Catch ex as Exception
           'If you do not specify an exception type then this will catch it.  Kind of an all 
           '  purpose catchers mitt.
        Finally
            'This is nifty, anything put into this section will run REGARDLESS of any errors that are caught.  
            'This is especially helpful for objects declared outside of the try catch block like FileStreams or data objects 
            'because you can handle and dispose of them properly here instead of having to code/recode in several areas.
        End Try

        Comment

        • MrMancunian
          Recognized Expert Contributor
          • Jul 2008
          • 569

          #5
          Originally posted by sashi
          You try to catch, you catch and you throw :)
          This explaination isn't entirely true. You don't try to catch. You should see it as a safety harnass. You jump to get to the other side, but when you fall, there is a safety harnass as a catch.

          Code:
          Try 'Jump to get to the other side
          
          Catch 'If you don't make it, this is your safety harnass
            MessageBox(ex.Message) 'Why didn't you complete the jump?
          Finally 'Whether you complete the jump or not, you can get into your car and drive home
          
          End Try 'Welcome home!

          Comment

          Working...