In my SQL data access layer in a VB 2005 class library, I have a few blocks which fit the following pattern:
Since the catch block for each exception is largely the same, I've put that logic in a subroutine which ends by throwing the new exception I've generated.
Visual Basic, however, doesn't know that my sub throws an exception, so it gives me warnings about the return statement outside the try/catch block. I can easily turn off the warnings or even put an (unreachable) return statement within the catch block, but that seems like a hack.
I'd like to know if there's a best practice for this pattern. Maybe there's some attribute I can't find that alerts the compiler that a method throws an exception? Can I suppress the warnings within this particular class?
Thanks in advance for your help.
Code:
try
execute SQL command
catch DataException
process exception (log it, etc.)
generate and throw new DataException
(with the original exception as an inner exception)
end try
return data
Since the catch block for each exception is largely the same, I've put that logic in a subroutine which ends by throwing the new exception I've generated.
Visual Basic, however, doesn't know that my sub throws an exception, so it gives me warnings about the return statement outside the try/catch block. I can easily turn off the warnings or even put an (unreachable) return statement within the catch block, but that seems like a hack.
I'd like to know if there's a best practice for this pattern. Maybe there's some attribute I can't find that alerts the compiler that a method throws an exception? Can I suppress the warnings within this particular class?
Thanks in advance for your help.