Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why catch an exception just to throw it again?

In a webservice I see this code:

<WebMethod()> _
Public Function dosomething() As Boolean
    Try
        If successful Then
            Return True
        Else
            Return False
        End If
    Catch ex As Exception
        Throw ex
    End Try
End Function

What's the point of catching the exception and just throwing it again? Do I miss something?

Edit: Thanks for the answers! I thought it was something like that, but wasn't sure if I could/would refactor those away without any implications.

like image 804
Stefan Avatar asked Dec 03 '08 11:12

Stefan


2 Answers

Don't do this.

If you absolutely need to rethrow the exception, just use throw; using throw ex; erases the stack trace and is absolutely wrong.

like image 148
GEOCHET Avatar answered Oct 18 '22 12:10

GEOCHET


I can think of no reason to do this for functionality. However, it can arise when previously there was some error handling (logging usually) that has been removed, and the developer removed the log handling but did not restructure the code to remove the redundant try/catch.

like image 33
Jack Ryan Avatar answered Oct 18 '22 13:10

Jack Ryan