locked
Difference between Catch and catch (exception ex) RRS feed

  • Question

  • Is it right that catch(Exception Ex) will catch only .net compliance exceptions and catch() will catch all types of exception non-compliance and .net compliance.

    Can anybody give me a real example of non .net compliance exception?

    Thanks

    Sunday, July 22, 2012 4:38 AM

Answers

  • The only difference is that by using catch (Exception ex), you can do something with the exception. If you don't specify it and just use catch() you have no way to find out what the exception was.


    ~~Bonnie Berent DeWitt [C# MVP]

    geek-goddess-bonnie.blogspot.com

    Sunday, July 22, 2012 4:48 AM

All replies

  • The only difference is that by using catch (Exception ex), you can do something with the exception. If you don't specify it and just use catch() you have no way to find out what the exception was.


    ~~Bonnie Berent DeWitt [C# MVP]

    geek-goddess-bonnie.blogspot.com

    Sunday, July 22, 2012 4:48 AM
  • There is actually still one option to specify the type of an exception to catch, but not set the variable like catch(IOException), this might be helpful if you know you can handle exception of the some detailed type and don't need the exception details.

    By specifying the exception type you can catch more detailed type of an exceptions than just all possible exceptions as with just catch. Using just catch without type and variable is easy way to swallow all the possible exceptions what usually is not good practice.

    Sunday, July 22, 2012 7:07 AM
  • Good point, Sam. I missed the opportunity to expand on that with my first reply, but I'll do it now.

    NewCoder, one of the things you can do is have multiple catch statements for handling exceptions that you know how to handle (such as Sam suggested). Take a look at this example:

    try
    {
        // some code here that could possibly
        // result in any of the below exceptions
        // being thrown
    }
    catch (SqlException ex)
    {
        // code here to handle the SqlException
    }
    catch (MessageQueueException ex)
    {
        // code here to handle the MessageQueueException
    }
    catch (IOException ex)
    {
        // code here to handle the IOException
    }
    catch (Exception ex)
    {
        // code here to handle any other exceptions
    }


    ~~Bonnie Berent DeWitt [C# MVP]

    geek-goddess-bonnie.blogspot.com

    Sunday, July 22, 2012 3:28 PM
  • Just remember to put the exceptions in order of most specific to more general since only the logic of the first 'catch' to be matched will be executed.

    Convert between VB, C#, C++, & Java (http://www.tangiblesoftwaresolutions.com)
    Instant C# - VB to C# Converter
    Instant VB - C# to VB Converter

    Sunday, July 22, 2012 7:51 PM
  • A non-CLS-compliant language can throw exceptions not derived from Exception. A catch block without a type of exception will catch them.
    Monday, August 6, 2012 2:37 PM