JLopez
JLopez

Reputation: 1749

Exception Handling

Is there a way in C# to catch any kind of exception? Like in C++ to catch any kind of exception the format is like

try{
//Statements
}
catch(...){
// Some more statements
}

But this format in c# fails. Help?

Upvotes: 1

Views: 360

Answers (5)

Secko
Secko

Reputation: 7716

The .NET framework provides a mechanism to detect/handle run time errors. C# uses three keywords in exception handling: try, catch, finally. The try block contains the statement that can cause an exception. The catch block handles the exception, and the finally block is used for cleaning up.

try
{
//statements that can cause an exception
}
catch(Type x)
{
//statements for handling an exception
}
finally
{
//cleanup code
}

Upvotes: 0

Woot4Moo
Woot4Moo

Reputation: 24316

catch(Exception ex)

or catch() <-- i believe the second one works

Upvotes: 0

Pablo Santa Cruz
Pablo Santa Cruz

Reputation: 181280

Check this link out. It's all about exceptions.

What you are trying to do is use a parameter-less catch like this:

try {
   // your code
} catch {
   // any exception
}

Upvotes: 2

MiffTheFox
MiffTheFox

Reputation: 21565

try {
    // Statements
} catch (Exception ex) {
    // Do stuff with ex
}

That should work.

Upvotes: 0

Andomar
Andomar

Reputation: 238086

You can catch anything like :

catch {}

From .NET 2 and further, this is equivalent to:

catch(Exception ex) {}

Because every exception (even a Windows SEH exception) is guaranteed to be derived from System.Exception.

Upvotes: 7

Related Questions