0

Recently I had an interesting conversation with a fellow developer who told me that every time you write a "try" it is mandatory to provide a "catch". He could not explain why this rule. He told me that it was a principle of good programming. Why this rule?

For your information I'm not agree with him. I think that sometimes you could write a "try" block with only a "finally" block. But it's true that I think if you write a "catch" you must do something in your catch. Never just re-throw the error.

Sednus
  • 2,095
  • 1
  • 18
  • 35
Bastien Vandamme
  • 17,659
  • 30
  • 118
  • 200
  • 2
    Related: http://stackoverflow.com/questions/128818/why-is-try-finally-good-try-catch-bad (but is also offtopic by now according to [FAQ](http://stackoverflow.com/faq)) – Tim Schmelter Sep 11 '12 at 12:12
  • I would disagree with your colleague, and so would Anders Hejlsberg, the creator of C# - see [this interview](http://www.artima.com/intv/handcuffsP.html) to quote: > In a well-written application there's a ratio of ten to one, in my opinion, of try finally to try catch. Or in C#, using statements, which are like try finally – codebox Sep 11 '12 at 12:12
  • I agree with your query regarding just sometimes using `try/finally`. `try/finally` can be useful when cleanup is always required, but exceptions will be dealt with at a higher level (or logged). So useful, in fact, that there's built in support (in C#) for generating them ([`using`](http://msdn.microsoft.com/en-us/library/yh598w02.aspx) and [`IDisposable`](http://msdn.microsoft.com/en-us/library/system.idisposable.aspx)) – Damien_The_Unbeliever Sep 11 '12 at 12:11
  • Yes. You should add catch block ONLY if you are intend to handle the error. – A Developer Sep 11 '12 at 12:10
  • Nope - you might want to have cleanup code that will always run, then gives bubbles the error up the stack. – RQDQ Sep 11 '12 at 12:23
  • the catch statement or finally statement is mandatory in some language syntax in java and java script if you don't write them it'll not compile. and main value is to catch a different exception types and do what is must do either the statement fails or execute successfully. – Prog Mania Sep 11 '12 at 12:12
  • Of course either `catch` or `finally` are required... one or the other. the OP was asking if `catch` was always required, regardless of the presence of `finally`. – Andrew Barber Sep 11 '12 at 12:18
  • 1
    How can this be "not constructive" ? That's a basic question, with definitive and non ambiguous answers, regarding one of the most basic constructs of many languages. – Denys Séguret Sep 11 '12 at 13:41

3 Answers3

9

You're right : you don't need to write a catch clause if you don't know what to do with the exception and just want to ensure your finally clause is executed.

It's bad practice to add a catch clause just to rethrow the exception.

As an aside, to illustrate that catch and finally are in fact related to two different (admittedly not foreign) problems, note that some languages use a different construct for the catching of exception and to ensure some code (usually resource release) is executed. Go use defer for example.

Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
6

In most applications try/finally constructs heavily outnumber try/catch constructs.

Because it's much more common to have resources to clean up than it is to receive an exception you know how to handle.

However try/finally is nearly always replaceable by using in C#, so in C# your developer might have a point in that case; but it most definitely isn't a "a principle of good programming".

Joe
  • 122,218
  • 32
  • 205
  • 338
2
try
{
    ...
}
finally
{
    ...
}

Gives you the opportunity to execute code in the finally block that would otherwise get missed if an exception were thrown in the try block. You only need to add a catch block if you have something specific to do when an exception occurs.

Justin Harvey
  • 14,446
  • 2
  • 27
  • 30