views:

552

answers:

5

In an webservice I see this code: Whats the point of catch the exception and just throw it again? Do I miss something?

<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

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.

+8  A: 

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.

Jack Ryan
+3  A: 

Probably a bit of code left over from debugging (you'd set a breakpoint on the throw so you could examine the exception in the debugger). I might do something like this if I wanted to log the exception and then pass it up the chain, although I'd probably wrap the exception in another one with a more meaningful (to my application) error message.

tvanfosson
+10  A: 

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.

Geoffrey Chetwood
Which is another reason why I would wrap it as an inner exception if I needed to do this for logging.
tvanfosson
Why? Just throw the original exception. Logging should not change or alter anything.
Geoffrey Chetwood
To add my own semantics to the exception. Ex. I get a SqlException because I attempt to insert a row with a duplicate primary key. In my method I know what kind of object and what key value is being inserted. I can write a better exception message yet still retain all the information.
tvanfosson
This has nothing to do with logging then. Different concept.
Geoffrey Chetwood
A: 

You may want to do it, if you want to catch an exception except a sub-class of it.

e.g.

try{
    // something stupid
}catch(RuntimeException e){
    throw e; //handle it outside
}catch(Exception e){
    // I'm dead
}
Dennis Cheung
But you still don't want to call "throw e". You want "throw" instead. Calling "throw e" drops the stack information up to that point, and it is dangerous.
Brian Genisio
WRONG WRONG WRONG. PLEASE DONT TELL PEOPLE THIS.
Geoffrey Chetwood
The throw e; is obviously wrong but aside from that this is an interesting idea. Not sure if there is a use case for it though....
Quibblesome
And you also swallowed the general Exception so nobody knows about it.
GalacticCowboy
@Quarrelsome: No, just wrong. There is no wisdom or knowledge to be gained from this.
Geoffrey Chetwood
@Rich B - I agree, but rather than just saying 'It's WRONG', why not provide some constructive feedback of why it's wrong so others can learn from it. I know a comment doesn't leave much room, but you could try...
Mikezx6r
There IS some wisdom to be gained here. It is completely reasonable to catch a specific exception subclass, in order handle it appropriately, but to generically catch the general exception, which represents cases you can't handle, but will log instead. Agreed destroying the stack is bad though.
Ben McEvoy
@Mike: No need. It should just be deleted or voted into oblivion.
Geoffrey Chetwood
n Java, It WON'T DESTROY the stack. It kept all information from the original throw.
Dennis Cheung
Oops, I didn't notice the "vb.net" tag on the question
Dennis Cheung
@ Rich B, I wish I could downvote comments because there is no wisdom or knowledge to be gained from yours.
Quibblesome
@Quarrelsome: And yours are just argumentative and trollish. Get over it.
Geoffrey Chetwood
+1  A: 

One of the architectures (design patterns) I could see this being used is where a transaction is being handled. The function does its work fails and the catch block completes the transaction to a known state (usually a roll back) then throws a user defined exception.

As it stands now, refactor that code to a more sane state.

jim