Exception Catch but dont throw

M

Miro

I cant seem to find an example on how to do something, ( vb2005.express )

i have a

Try
ListeningSerialPort.Open()
TestText.Enabled = True
Catch ex As Exception
'Debug.WriteLine(ex.Message)
End Try

<morecodehere>...

statement, and what I am tryign to do is Catch the exception ( which it
does ) but i dont want to throw it anywhere.

How do I get this try catch to continue gracefully so if an exception is
caught, the "more code here" gets executed and continues on - the user never
sees that an exception was hit and they do not know the difference?

Currently when I compile / and create a setup.exe with the publish and
install it, the program hits a fatal exception -displays it to the user and
fails. If I run the same program in vb.net express, then this does not
happen, it shows the exception in the immediate window but lets me continue.
Am I missing something with the Try Catch statement ?

I hope that makes sence.

Miro
 
R

rowe_newsgroups

I cant seem to find an example on how to do something, ( vb2005.express )

i have a

Try
ListeningSerialPort.Open()
TestText.Enabled = True
Catch ex As Exception
'Debug.WriteLine(ex.Message)
End Try

<morecodehere>...

statement, and what I am tryign to do is Catch the exception ( which it
does ) but i dont want to throw it anywhere.

How do I get this try catch to continue gracefully so if an exception is
caught, the "more code here" gets executed and continues on - the user never
sees that an exception was hit and they do not know the difference?

Currently when I compile / and create a setup.exe with the publish and
install it, the program hits a fatal exception -displays it to the user and
fails. If I run the same program in vb.net express, then this does not
happen, it shows the exception in the immediate window but lets me continue.
Am I missing something with the Try Catch statement ?

I hope that makes sence.

Miro

Your current try catch should "continue gracefully" on any Exception
object - so I'm not sure why it isn't. You might try omitting the "ex
as exception" part - this will cause it to trap all but a few really
nasty (and normally non-recoverable) exceptions.

i.e.

Try
...
Catch
...
End Try

Also, are you sure the exception is from this method? Perhaps you are
hitting a security exception or something before you make it to the
above code?

Thanks,

Seth Rowe
 
M

Miro

The "Catch" on its own did not work.

I have changed it to be this and i still get the error.
====
Debug.WriteLine("b4exp")
Try
ListeningSerialPort.Close()
Catch extry As System.UnauthorizedAccessException
Debug.WriteLine("cought it")
Catch ex As Exception
Debug.WriteLine("other caught it")
End Try
Debug.WriteLine("aftb4exp")
===
my output is as follows in the immedate window
b4exp
A first chance exception of type 'System.UnauthorizedAccessException'
occurred in System.dll
cought it
aftb4exp
====

I cant seem to get around it from it throwing that exception. Any other
ideas?

What I am basically doing is i have a proximety scanner hooked up thru a usb
to my computer.
In the begining what I do is
if serial.isopen() = false then serial.open()

then what I do is i unplug the usb.
The problem is that when I unplug the usb isopen still returns True, so
what I do is I have a timer and every so many seconds and i then
Dim CurrentAvailPorts As Array = SerialPort.GetPortNames()
and compare what port names are available to the ones I THINK that are open,
and if they are actaully not anymore ( by GetPortNames() ) I try to close
them. Thats where the error is occuring.

That way, every so many seconds...when GetPortNames does return my ComPort
that was opened and is available again - I re-open it.

The Part that is failing is the "closing" of the port when the usb is
unplugged.


Any other Ideas?

Thanks

Miro
 
P

Patrice

I'm not sure what is the exact problem.

Debug messages are show in the debug console to developers (my understanding
is that you don't want to see the exception message in the debug output
???). It doesn't mean an end user would see this in the final build.

The exception is ALWAYS throw. What matter is if it is handled (in which
case you do whatever you want includuing not showing it to the end user) or
undhandled (in which case the .NET runtime will show the error) ? The
exception always happend and the dbeug outpuit always show that an exception
occured.

Not familiar with this class but always prefer a check to throwing exception
(tha is if you have a method/proerty that allows to check the port status
prefer checking the status so that you know if you can close to catching
exceptions).
 
M

Miro

I think now my exception could be something else.

I have some reading to do, but the error I am getting is this now:
===
an exception system.objectdisposedexception

However, no debuggers are registered that can debug this exception. Unable
to JIT debug
====
JIT Deubbing failed with the following error: Unspecified error.
Please check the documentation topic 'Just-in-time debugging errors' for
more information.

I might have to re-post tomorrow or friday if i dont understand fully what I
am reading / or how I can pinpoint where in the code this error is
happening.

Miro
 
M

Miro

Ok Here is the timing of the error and I have also found out that
VB2005.Express does not have a JIT debugger. So I am stumped as to how to
continue to try to solve this. Any suggestions would be welcomed.

This only happens when I do the Install of the application. Running through
vb.net express - the error does not happen.

===== here is the code on a button i created to re-create this test 100%
each time =====

Try
MessageBox.Show("will try1")
Try
ListeningSerialPort.Close()
Catch extry As System.ObjectDisposedException
MessageBox.Show("got System.OjbectDisposedException")
Catch ex As Exception
MessageBox.Show("ex -" & ex.ToString)
End Try
MessageBox.Show("button done")

Catch ex3 As Exception
MessageBox.Show("ex3 -" & ex3.ToString)
End Try

End Sub
======so here is what happens
<this is what happens in vb.express 2005>
Program runs, and I open Com6 ( in this example ). Then as com6 is open, i
unplug the usb connection. - That is mapped to Com6. The applicaiton hits
100%
I click the button, and I get my "Will Try" messagebox,
then my ex - System.UnauthorizedAccessExceptoin: Access to the port is
denied.... error message
the exe goes back down to the 0 %
and then my "Button Done" message.


<This is what happens in an installed application>
Program runs, and I open Com6 ( in this example ). Then as com6 is open, i
unplug the usb connection. - That is mapped to Com6. The applicaiton hits
100%

so then I click this button.
I get a messagebox "Will Try" so I click ok.

Then I get 2 messageboxes.
Box 1 - ( you will notice this is what my try catch caught )
ex - System.UnauthorizedAccessExceptoin: Access to the port is denied.
at System.IO.Ports.InternalResources.WinIOError(Int32 errorCode, String str)
at System.IO.Ports.SerialStream.Dispose(Boolean disposing)
at System.IO.Stream.Close()
at System.IO.Ports.SerialPort.Dispose(Boolean disposing)
at System.IO.Ports.SerialPort.Close()
at Port2Text.frmPort2Text.button1_Click(Object sender, EventArgs e)

Box 2 - Comes up with it - and has the title Just-In-Time Debugging
An Exception 'System.ObjectDisposedExceptoin' has occurred in DefaultDomain.
However, no debuggers are registered that can debug this exception. Unable
to JIT debug.

By Clicking OK on Box2
another one shows
Jit Debugging
Jit Debugging Failed with teh following error: Unspecified error
Please check teh documentation topic 'Just-in-time debugging errors' for
more information.


Any help would be appreciated.

Thanks,

Miro
 
M

Miro

I believe I have found the issue

And its mostly part of my learning curve...

I was under the impression that Catch ex As Exception catches everything,
but I also had to add a
Catch ex As System.UnauthorizedAccessException

This took care of the issue.

Without this, calling a "SerialPort.Close()" when the port is unplugged (
during while it was opend ) will cause your exe to hit 100%.

Thanks for everyone's help.

Miro
 

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