difference -catch() And Catch (exeption ex)
can anyone help me out
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Raj KumarPosted May 29, 2013, 12:01 AM
try
{
throw new InvalidOperationException("Oh no!")
} catch {
// can't do anything here.
}
try
{
throw new InvalidOperationException("Oh no!")
} catch (Exception ex) {
MessageBox.Show(ex.Message);
}
Also, you can filter catch statements for particular exceptions using the (Exception ex) version.
try
{
// do something risky
} catch (SqlException ex) {
MessageBox.Show("SQL laid an egg.");
} catch (FormatException ex) {
MessageBox.Show("Your user isn't behaving and is entering data wrong.");
} catch (Exception ex) {
MessageBox.Show("I have no idea, could be anything.");
}
There is also the third option. You can filter by the exception type, but not use the variable, in which case, you don't need to specify a variable name to use in the catch block:
try
{
// do something risky
} catch (SqlException) {
MessageBox.Show("SQL laid an egg.");
} catch (FormatException) {
MessageBox.Show("Your user isn't behaving and is entering data wrong.");
} catch (Exception) {
MessageBox.Show("I have no idea, could be anything.");
}
Jignesh TrivediPosted May 28, 2013, 10:52 PM
catch and catch (Exception e), both are equally same, other is just catch the exception and then use to rethrow or log the exception.
The catch clause can be used without arguments, in which case it catches any type of exception, and referred to as the general catch clause. It can also take an object argument derived from System.Exception, in which case it handles a specific exception.
please refer
http://msdn.microsoft.com/en-us/library/0yd65esw(v=vs.80).aspx
hope this will help you.