PeeHaa
PeeHaa

Reputation: 72709

Is this try-catch block valid?

Newby question...

Is it valid to do:

try
{
    // code which may fail
}
catch
{
    Console.Writeline("Some message");
}

Or do I always have to use:

try
{
    // code which may fail
}
catch (Exception e)
{
    Console.Writeline("Some message");
}

Upvotes: 2

Views: 263

Answers (10)

Dubs
Dubs

Reputation: 124

Don't forget that you can chain catch your exceptions. This will allow you to handle different scenarios based upon the exception(s) the code may throw.

try
{
    //Your code.
}
catch(SpecificException specificException)
{
    //Handle the SpecificException
}
catch(AnotherSpecificException anotherSpecificException)
{
    //Handle AnotherSpecificException
}
catch(Exception exception)
{
    //Handle any Exception
}

Upvotes: 1

Oded
Oded

Reputation: 499302

Both blocks are valid.

The first will not have an exception variable.

If you are not going to do anything with the exception variable but still want to catch specific exceptions, you can also do:

try 
{
   // your code here
}
catch(SpecificException)
{
   // do something - perhaps you know the exception is benign
}

However, for readability I would go with the second option and use the exception variable. One of the worst things to do with exceptions is swallow them silently - at the minimum, log the exception.

Upvotes: 5

Adriano Cavalari
Adriano Cavalari

Reputation: 9

catch (Exception e)
{
    Console.Writeline("Some message");
}

In this block you can use SqlException, etc..

catch (SqlException e)
{
    Console.Writeline("Some message");
}

For this use the "(SqlException e)"

If you will use a generic menssage, use this:

catch
{
    Console.Writeline("Some message");
}

or

catch (Exception)
{
    Console.Writeline("Some message");
}

Upvotes: 1

Marco
Marco

Reputation: 57593

As @David answered this is valid.
You could use second syntax if you want to get more infos or catch a specific exception. E.g.

catch (Exception e) 
{
  Debug.Print(e.Message);
}

Upvotes: 1

sll
sll

Reputation: 62544

Yep, absolutely, such a catch block called general catch clause, see more interesting details in the C# Language Specification 4.0, 8.10 The try statement:

A catch clause that specifies neither an exception type nor an exception variable name is called a general catch clause. A try statement can only have one general catch clause, and if one is present it must be the last catch clause

Upvotes: 3

croisharp
croisharp

Reputation: 1986

Of course it is valid, you specify catch(Exception e) when you want to output the error message ex.Message, or to catch a custom or a concrete Exception. Use catch in your situation.

Upvotes: 1

mhttk
mhttk

Reputation: 1696

Yes it is valid.

you can always refer to this article:

Best Practices for Handling Exceptions on MSDN

Upvotes: 1

user925777
user925777

Reputation:

First one is valid, and it acts just like the second one.

http://msdn.microsoft.com/en-us/library/0yd65esw%28v=vs.80%29.aspx

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.

Upvotes: 1

anon
anon

Reputation:

It is. It will catch all the exception. So the two code examples do the same.

Upvotes: 1

David Heffernan
David Heffernan

Reputation: 613461

Yes, your first block of code valid. It will catch all exceptions.

Upvotes: 2

Related Questions