Difference between Try...Catch & Try...Catch ex As Exception (VS 2013) RRS feed

  • Question

  • Hi, just wanna know what's the difference between



    Try...Catch ex As Exception

    in VS 2013.

    Friday, September 30, 2016 10:58 AM


All replies

  • The "Catch as ..." allows you to code different behavior depending on the type of Exception cast, for example



    you can also "Catch When somecondition"

    Have a look at the Try Catch msdn : https://msdn.microsoft.com/en-us/library/fk6t46tz.aspx?f=255&MSPPError=-2147217396

    The list of Exception you can try to catch is described by the Exception Class here: https://msdn.microsoft.com/en-us/library/system.exception.aspx

    Now with the Try Catch without the Exception argument you go through the code but without description of the exception.

    So in summary you can have:

    Try 'some code Catch 'Here handling all the execption but without information on it

    'Why would you not want to know which error occured..I don't know... End Try


    'Some code
    Catch ex as Exception
        'Here you can have code to display the Exception ex.Message for example
    End Try

    Friday, September 30, 2016 11:17 AM
  • Hello,

    Here is a web page that simplifies the use of try/catch.

    Before using try/catch consider using assertion e.g. before opening a text file use IO.File.Exists or rather than converting a value is a TextBox to a number use TryParse and so on.

    When using try/catch consider it's usage both in a form and in a class, in the form you might consider displaying a MessageBox while in a class you might have properties to send back information of an exception as shown in this example.

    In other words be proactive rather than simply wrapping code which might fail into a try/catch.

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Friday, September 30, 2016 11:49 AM
  • Hi, just wanna know what's the difference between



    Try...Catch ex As Exception

    in VS 2013.

    In either instance code causing an exception will not crash the app if placed in a try/catch because the try/catch allows the exception to be handled by code.

    However it is best to use code that uses logic to disallow an exception to occur in the first place if possible.

    La vida loca

    Friday, September 30, 2016 12:19 PM
  • The first allows catching both managed and unmanaged exceptions also (e.g., from a native C++ dll that causes an exception).  There is no .NET class suitable for unmanaged exceptions so you can't use 'Exception' or any other derived exception class.

    The second gives you the exception information for all managed exceptions, but will not catch an unmanaged exception.

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

    • Proposed as answer by Frank L. Smith Friday, September 30, 2016 3:51 PM
    Friday, September 30, 2016 2:29 PM
  • Hi, just wanna know what's the difference between



    Try...Catch ex As Exception

    in VS 2013.

    Whenever this comes up, I always post a link to a short video on my website. It's about three minutes long and has audio:


    In particular, pay attention to an empty catch. It's NOT a road you want to go down. Sometimes it's appropriate but usually it's very much not so.

    Curiosity is the world's most valuable asset.

    Friday, September 30, 2016 3:53 PM