Results 1 to 4 of 4

Thread: Handle different exceptions of same exception type

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2004
    Posts
    541

    Handle different exceptions of same exception type

    Hello all,

    Is there a way I can catch and handle two different exceptions within the same exception type? For example:

    Code:
    Try
    
    ' My code
    
    Catch ex As System.Net.WebException
     
    End Try
    I have 2 different exceptions that are System.Net.WebException. I want to handle them different ways. Problem is the catch block above catches them both. Is there a way I can determine if which of the two it is and handle them differently?

    Thanks,

    Strick

  2. #2
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: Handle different exceptions of same exception type

    How do you distinguish them? Use the same distinction for sorting them out in the catch block

  3. #3
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    9,017

    Re: Handle different exceptions of same exception type

    The try catch block can also take a when clause. You may use this to distinguish:-
    vbnet Code:
    1. '
    2.         Try
    3.             'Throw New ArgumentException("ARE YOU MAD????")
    4.             Throw New ArgumentException("YA U ARE MAD!!!")
    5.  
    6.         Catch ex As ArgumentException When ex.Message.Contains("ARE YOU MAD")
    7.             MsgBox("Are you mad ?")
    8.         Catch ex As ArgumentException When ex.Message.Contains("YA U")
    9.             MsgBox("You are mad")
    10.  
    11.         End Try
    You can try out the above as see. Comment/Uncomment either one and you will see the relevant catch block handle it.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2004
    Posts
    541

    Re: Handle different exceptions of same exception type

    Thanks guys! This is what I was looking for.

    Strick

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width