CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jul 2008
    Posts
    8

    Question what's the difference between these 2 catches ?

    try
    {
    ...
    }
    catch
    {
    ...
    }


    or
    try
    {
    ...
    }
    catch(Exception e)
    {
    ...
    }

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: what's the difference between these 2 catches ?

    With the empty catch clause, you have no idea of what exception occurred.

    With the Exception e param of the 2nd catch clause you catch every type of exception.

    Generally you want to catch exceptions in a specific to general order.

    for example, say I'm saving some data to a database and there is a possibility of having a null param or database problem

    Code:
    try
    {
      // code that could throw a ArgumentNullException or SqlException
    }
    catch( ArgumentNullException e )
    {
      // handle ArgumentNullException
    }
    catch( SqlException e )
    {
      // handle sql exception
    }

  3. #3

    Re: what's the difference between these 2 catches ?

    Hi 5883,
    Wen yo have catch{} alone yo can handle but without the knowledge wat kind of exception it is ,moreover if yo want to give user about exception and to correct him for good input its difficult with tis.

    Wen yo use catch(Exception e){} Exception is not but base class for all kind of exception .SO in this usage your creating a reference for that baseclass which holds the exception created.
    In that e yo have many methods and properties so that yo can handle as well as inform the user efficiently.

Tags for this Thread

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

Featured