Explicitly throw an exception in VB.NET

P

Phillip Taylor

I don't like the way every time I write a bad sql query the debugger
takes me to the database access code. It's extremely stable and
reliable and the exceptions being thrown are because of SQL queries
data passed to it. I don't want to handle or supress exceptions at
that point in the application, but rather ignore them so the debugger
handles them in the correct place, further up the call stack.

In java I could normally add a "throws Exception" to the end of the
function definitiion. Does anyone know how do I do the same thing in
VB?
 
R

rowe_newsgroups

I don't like the way every time I write a bad sql query the debugger
takes me to the database access code. It's extremely stable and
reliable and the exceptions being thrown are because of SQL queries
data passed to it. I don't want to handle or supress exceptions at
that point in the application, but rather ignore them so the debugger
handles them in the correct place, further up the call stack.

In java I could normally add a "throws Exception" to the end of the
function definitiion. Does anyone know how do I do the same thing in
VB?

Exceptions will bubble up automatically unless you have the debugger
attached. With the debugger attached and unhandled exceptions will
cause execution to stop and put the debugger in step-through mode. You
can set what the debugger does with different types of exceptions
using the Exceptions window (found under the Debug menu).

If you want to see what would happen when the application doesn't have
the debugger attached you can run the app by pressing Ctrl+F5.

Thanks,

Seth Rowe
 
G

Guest

Phillip,

In .Net, if an exception occurs in a method that is not equipped to handle
it, the exception is propagated back to the calling method. If the calling
method also has no exception handler, the exception is propagated back to
that method 's caller, and so on. If no exception handler is found, an error
message is displayed and the application is terminated.

Perhaps in the IDE you are breaking whenever an exception is thrown. You can
change this behavior from the IDE's Debug menu, under the Exception item.

Kerry Moorman
 
P

Phillip Taylor

Exceptions will bubble up automatically unless you have the debugger
attached. With the debugger attached and unhandled exceptions will
cause execution to stop and put the debugger in step-through mode. You
can set what the debugger does with different types of exceptions
using the Exceptions window (found under the Debug menu).

If you want to see what would happen when the application doesn't have
the debugger attached you can run the app by pressing Ctrl+F5.

Thanks,

Seth Rowe

I understand the debugger, My question is when debugger catches the
exception, instead of me manually going to the call stack and finding
the correct place the data became corrupt, can I simply tell the
debugger to "not handle exceptions" until they esculate above that
level. Does that make sense? Basically all the code at the bottom is
trust-worthy decent code, can I exclude the debugger from stepping
into it and can I exclude the code from being stopped inside those
functions?
 
R

rowe_newsgroups

I understand the debugger, My question is when debugger catches the
exception, instead of me manually going to the call stack and finding
the correct place the data became corrupt, can I simply tell the
debugger to "not handle exceptions" until they esculate above that
level. Does that make sense? Basically all the code at the bottom is
trust-worthy decent code, can I exclude the debugger from stepping
into it and can I exclude the code from being stopped inside those
functions?

Reread my post - I said you could use the Exceptions tool under the
Debug menu to set which types of exceptions the debugger will break
on.

Thanks,

Seth Rowe
 
P

Phillip Taylor

Reread my post - I said you could use the Exceptions tool under the
Debug menu to set which types of exceptions the debugger will break
on.

Thanks,

Seth Rowe

I understand the concept of exceptions....if I don't catch it in the
"ExecuteSQLCommand" it propogates up the callstack until it finds a
Try Catch block around it. I understand I can catch and throw
exceptions anywhere. I understand if the debugger is off and there is
no exception handling code then the application will crash and
the .NET runtime will show an error message and end my application. I
understand exceptions completely. What I don' t understand is that if
this is my call stack looks like this:

button_click
- eventListener
-- custom code
--- ExecuteSQLCommand

how I can make the exception, which I don't want to catch or handle,
that could be any subclass of the exception object, will take the
debugger and instead of stopping it on the "ExecuteSQLCommand"
function, I can make it stop in the "custom code" function instead, on
the line at which it calls ExecuteSQLCommand.

In Java I can declaire "ExecuteSQLCommand" like this "public int
ExecuteSQLCommand(String sql) THROWS EXCEPTION" and the debugger moves
me to "custom code" level rather than the "ExecuteSQLStatement" level.

Can I do something similar in VB.NET? Don't take me to here with the
debugger....but take me to this level in the call stack instead?
 
R

rowe_newsgroups

I understand the concept of exceptions....if I don't catch it in the
"ExecuteSQLCommand" it propogates up the callstack until it finds a
Try Catch block around it. I understand I can catch and throw
exceptions anywhere. I understand if the debugger is off and there is
no exception handling code then the application will crash and
the .NET runtime will show an error message and end my application. I
understand exceptions completely. What I don' t understand is that if
this is my call stack looks like this:

button_click
- eventListener
-- custom code
--- ExecuteSQLCommand

how I can make the exception, which I don't want to catch or handle,
that could be any subclass of the exception object, will take the
debugger and instead of stopping it on the "ExecuteSQLCommand"
function, I can make it stop in the "custom code" function instead, on
the line at which it calls ExecuteSQLCommand.

In Java I can declaire "ExecuteSQLCommand" like this "public int
ExecuteSQLCommand(String sql) THROWS EXCEPTION" and the debugger moves
me to "custom code" level rather than the "ExecuteSQLStatement" level.

Can I do something similar in VB.NET? Don't take me to here with the
debugger....but take me to this level in the call stack instead?

How about:

Public Sub CustomCode()
ExecuteSqlCommand("Select * From WhereEver")
End Sub

Public Sub ExecuteSqlCommand(sql as String)
Try
'// Normal code here
Catch
Throw
End Try
End Sub

IIRC the debugger breaks where an exception is unhandled. The
try...catch...throw in the ExecuteSqlCommand sub should be marked as
handling the exception meaning the debugger will stop once the
exception bubbles back up to CustomCode.

I haven't tested this - so please let me know what happens either way.

Thanks,

Seth Rowe
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top