user10102552
user10102552

Reputation:

What sort of data structure or construct is "catch" in Try-Catch

In the context of C#, one can have code as such:

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

In other cases, the code can be:

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

My question is: What sort of data structure or construct is "catch"? From the looks of the second example, it seems to be a method (in context of the C# programming language). But is it? If so, then why are the parentheses not required in the first example (since parenthesis are not optional for methods in C#)?

Upvotes: 2

Views: 210

Answers (3)

Marco Salerno
Marco Salerno

Reputation: 5201

Try / Catch is a statement

The try...catch statement is used to catch exceptions that occur during execution of a block, and the try...finally statement is used to specify finalization code that is always executed, whether an exception occurred or not.

Source: https://learn.microsoft.com/en-us/dotnet/csharp/tour-of-csharp/statements

Catch is a clause of the Try / Catch statement

It is possible to use more than one specific catch clause in the same try-catch statement.

Source: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/try-catch

Upvotes: 0

Sushant Rawat
Sushant Rawat

Reputation: 100

C# has Statements (or statement keywords) which are nothing but program instructions. Catch is a clause in C# try-catch statement (categorized in exception handling statements category). Also, since clauses are examined in order, you should catch more specific exceptions before the less specific ones.

Source

Upvotes: 3

waldrumpus
waldrumpus

Reputation: 2590

try-catch is an example of what is called a statement in the context of the C# programming language, or other imperative programming languages. Statements are syntactic elements, part of how the language is constructed.

Have a look at the documentation of try-catch to see its definition.

Upvotes: 2

Related Questions