Is there any difference between catch and catch(exception) in C#?

Shervan360 1,481 Reputation points
2023-09-11T01:46:31.9166667+00:00

Hi,

Is there any difference between the following codes?

using System.Collections;

namespace ConsoleApp1
{
     internal class Program
     {

          static void Main()
          {
               try
               {
                    int a = int.Parse(Console.ReadLine());
                    Console.WriteLine(a / 0);
               }
               catch (Exception)
               {
                    Console.WriteLine("Without Variable and Type");
               }
          }

     }
}


using System.Collections;

namespace ConsoleApp1
{
     internal class Program
     {

          static void Main()
          {
               try
               {
                    int a = int.Parse(Console.ReadLine());
                    Console.WriteLine(a / 0);
               }
               catch
               {
                    Console.WriteLine("Without Variable and Type");
               }
          }

     }
}


C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,648 questions
0 comments No comments
{count} votes

Accepted answer
  1. Jiale Xue - MSFT 43,046 Reputation points Microsoft Vendor
    2023-09-11T06:41:08.0266667+00:00

    Hi @Shervan360 , Welcome to Microsoft Q&A,

    There is no difference in your two sample codes.

    Check out the official explanation to find out:

    If you follow the base type of the corresponding exception in the catch. Then it can enter the corresponding catch block for processing when an exception is thrown.

    When an exception occurs, the catch clauses are checked from top to bottom in the order specified. For any exception thrown at most one catch block is executed., you can omit the declaration of the exception variable and specify only the exception type in the catch clause. No catch clause of the specified exception type matches any exception, and if one exists, it must be the last catch clause.

    For example, provide multiple catch clauses:

    try
    {
        var result = await ProcessAsync(-3, 4, cancellationToken);
        Console.WriteLine($"Processing succeeded: {result}");
    }
    catch (ArgumentException e)
    {
        Console.WriteLine($"Processing failed: {e.Message}");
    }
    catch (OperationCanceledException)
    {
        Console.WriteLine("Processing is cancelled.");
    }
    

    Best Regards,

    Jiale


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". 

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Viorel 114.7K Reputation points
    2023-09-11T11:56:13.3866667+00:00

    It seems that a simple catch without the type can be used to intercept special exceptions that are generated by modules made in other programming languages. See the Note:

    By the way, you can add an attribute and specify both of catch(Exception) and catch:

    1 person found this answer helpful.
    0 comments No comments