CSharp - What is the output: Variations of the catch Clause?

Question

Will the code compile?

//some code before
catch { }
catch (Exception)
{
 Console.WriteLine("Encountered an Exception");
}
//some code after


Click to view the answer

No.

Note

A catch clause that does not name an exception class can handle any exception.

There are some exceptions that do not derive from System.Exception.

We will take note that instead of catch(Exceptionname exceptionReference), you can use catch(Exceptionname) or catch{}.

catch (Exception)
{
    Console.WriteLine("Encountered an Exception");
}

Here is another variation of the catch clause.

catch ()
{
      Console.WriteLine("Encountered an Exception");
}

Related Quiz