tags:

views:

83

answers:

5
+1  Q: 

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?

+7  A: 

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.

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

That should work.

MiffTheFox
+2  A: 

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
}
Pablo Santa Cruz
A: 

catch(Exception ex)

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

Woot4Moo
A: 

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
}
Secko