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.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Jignesh TrivediRank #16 · 50.3K reputationPosted May 28, 2013, 6:52 PM EDT
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.
Raj KumarRank #25 · 44.2K reputationPosted May 28, 2013, 8:01 PM EDT
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.");
}