Try Catch does not work

  • Thread starter Thread starter Tore Gylver
  • Start date Start date
T

Tore Gylver

In one of my windows forms I have the following visual
basic code:

Dim FRM As New FormOrderQuit(SetOrderID)

Try
FRM.Show()
Catch Myexception As System.ApplicationException
MsgBox(Myexception.Message)
FRM.Close()
End Try

The code tries to show a form. If certain error
conditions occur the FormOrderQuit will throw an
ApplicationException. The codeline to throw the exception
is as follows:

Throw New System.ApplicationException
(BaseFormOrder_NoOrderReturned)
BaseFormOrder_NoOrderReturned is a Const containing a
string.


When I run the try-catch code above in Visual
Studio "with debugging" the catch-block runs as expected.
If I start the application in Visual Studio "without
debugging" the code in the catch-block is not run at all.

It seems like I miss something in my code to make this
work when debugging is switched off. Any help would be
appreciated.


Regards

Tore Gylver
 
As far as I know the Try - Catch should work regardless of debugging. My
first thought is that when you are running the app, you may get an Exception
that is not of type 'ApplicationException' and is not caught. Maybe try
catching a 'System.Exception' after the 'ApplicationException' to see if it
is something else??

-Darrin
 
I have modified the try catch blocks a little bit:
Try
FRM.Show()
Catch Myexception As System.ApplicationException
MsgBox("1")
MsgBox(Myexception.Message)
FRM.Close()
Catch ex As System.SystemException
MsgBox("2")
MsgBox(ex.ToString)
End Try

When debugging is activated the code goes through the
catch-block for ApplicationException. ("1" is displayed)
When the application is started without debugging it is
not handled in any of the catch-blocks. I get the
following message:

An unhandled exception has occured in your application.If
you click continue, the application will ignore this
error and attempt to continue. If you click Quit, the
application will be shut down immediately.

When I press the details button of the messagebox I can
see that the exception in question is an
ApplicationException, and it is in fact the one I have
thrown in another form.

Regards

Tore Gylver




-----Original Message-----
As far as I know the Try - Catch should work regardless of debugging. My
first thought is that when you are running the app, you may get an Exception
that is not of type 'ApplicationException' and is not caught. Maybe try
catching a 'System.Exception' after
the 'ApplicationException' to see if it
 
Just to add to Darrin's explanation, System.Exception is "the mother of all
exceptions" so to speak. So if you define a specific TYPE of exception eg
ApplicationException your Catch will ONLY catch exceptions that are
ApplicationExceptions and not handle any other types of exceptions.
Sometimes, you really do want to deal wtih a particular type so a good way
to set that up is

Catch exA as ApplicationException
'do something for only appexceptions
Catch ex as Exception
'any other exceptions will be handled here
End Try

hth

julie lerman
 
Tore,
Please bear with me.... I hope I'm not 'barking up the wrong tree'
and wasting your time, but the error message you are getting is exaclty what
it says, 'an unhandled exception'. The last Catch you have in your example
is still another specific Exception type, not the base 'mother of all
Exceptions'.

If you're still stuck, I would suggest catching a System.Exception
instead of a System.SystemException as your last Catch block, and then work
with that error information. It is possible that the 'ApplicationException'
could be thrown after this block from a different type of InnerException????

BTW - Thanks for your input Julia, I wasn't very verbose with the reasons
for my suggestion... :-)

-Darrin
 
Darrin, thank you for your interest in my problem
I have tried with the "mother of all exceptions", but I
still have the same problem. I never enter any of the
catch blocks when debugging is disabled. Here is my last
Try Catch block:

Dim FRM As New FormOrderQuit(SetOrderID)

Try
FRM.Show()
Catch Myexception As System.ApplicationException
MsgBox(Myexception.Message)
FRM.Close()
Catch ex As System.Exception
MsgBox("2")
MsgBox(ex.ToString)
End Try

The catch of ApplicationException works perfectly when
debugging, but not without debugging.

When I press DETAILS button of the unhandled exception
the first thing I find under "Exception Text" is the
message of the ApplicatinException I have thrown in code

Tore
 
Tore Gylver said:
I have tried with the "mother of all exceptions", but I
still have the same problem. I never enter any of the
catch blocks when debugging is disabled. Here is my last
Try Catch block:

Dim FRM As New FormOrderQuit(SetOrderID)

Try
FRM.Show()
Catch Myexception As System.ApplicationException
MsgBox(Myexception.Message)
FRM.Close()
Catch ex As System.Exception
MsgBox("2")
MsgBox(ex.ToString)
End Try

The catch of ApplicationException works perfectly when
debugging, but not without debugging.

When I press DETAILS button of the unhandled exception
the first thing I find under "Exception Text" is the
message of the ApplicatinException I have thrown in code

Where in FormOrderQuit is the ApplicationException thrown? Does the problem
persist if you try the following:

Dim frm As FormOrderQuit

Try
frm = New FormOrderQuit(SetOrderID)
frm.Show()
Catch ex As ApplicationException
MessageBox.Show(ex.Message)

If Not frm Is Nothing Then frm.Close()

End Try
 
Hi Tore,

Maybe the exception was not thrown in your try block, but from your catch
block.
Can you give us a stack trace, so we can see which method threw the
exception.
You may find the strack trace output in the output pane in VS.NET ,
start with "An unhandled exception of type 'System.ApplicationException'
occurred in ..."

Thanks!


Best regards,

Ying-Shen Yu [MSFT]
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security

This posting is provided "AS IS" with no warranties and confers no rights.
You should not reply this mail directly, "Online" should be removed before
sending.
 
Your Try-catch block gave exactly the same problem as the
others.

The exception is thrown in the load event of the baseform
(FormOrderQuit inherits BaseFormOrder)

Regards

Tore Gylver
 
Hi Tore,

If you run the release build of your programming out of the VS.NET IDE,
will it cause a "Unhandled exception dialog"?
will this happen even if you comment out your throw statement?
If yes, maybe an exception other than ApplicationException and the derived
exceptions was thrown out before your throw statement.
You may try this code snippet.
<code>
Try
Dim frm As New FormOrderQuit(SetOrderID)
Try
frm.Show()
Catch ex As ApplicationException
MsgBox("Expected Exception" & ex.ToString)
frm.Close()
End Try
Catch ex As Exception
MsgBox("Unexpected Exception " & ex.ToString)
End Try
</code>
If in this we still can't catch the unhandled exception, please give me a
repro sample,
I'll look into it.
Thanks!

Best regards,

Ying-Shen Yu [MSFT]
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security

This posting is provided "AS IS" with no warranties and confers no rights.
You should not reply this mail directly, "Online" should be removed before
sending.
 
I have introduced your try catch block and I get the same
results as before. If I run with debugging on I get the
expected exception.If I run with no debugging I get my
unhandled ApplicationException

If I run a release build within VS IDE I still get the
unhandled exception using the try-catch block from your
post.

I dont get any exception when I remove the throw
statement.


I have a feeling that errorhandling is switched
completeley off when I get this unhandled exception. I
have just started to wonder if visual basic requires any
initialization of exception handling before it has a
chance to work? So far I have no other codelines
regarding exceptionhandling than those shown at the
bottom of this post.

The details of the unhandled exception is listed below.
The listing of the unhandled ApplicationException refers
to vb line 830 of BaseFormOrder. This is the line where i
Throw the exception. At the bottom of this post I have
added some code of my troublesome forms.


Details of unhandled exception:

See the end of this message for details on invoking
just-in-time (JIT) debugging instead of this dialog box.

************** Exception Text **************
System.ApplicationException: Ingen ordre ble funnet.
Ordren kan være slettet
at WinStartup.BaseFormOrder.BaseFormOrder_Load(Object
sender, EventArgs e) in C:\Documents and Settings\Tore
Gylver\My Documents\Visual Studio
Projects\Mentor\WinStartup\BaseFormOrder.vb:line 830
at System.EventHandler.Invoke(Object sender, EventArgs
e)
at System.Windows.Forms.Form.OnLoad(EventArgs e)
at System.Windows.Forms.Form.OnCreateControl()
at System.Windows.Forms.Control.CreateControl(Boolean
fIgnoreVisible)
at System.Windows.Forms.Control.CreateControl()
at System.Windows.Forms.Control.WmShowWindow(Message&
m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ScrollableControl.WndProc
(Message& m)
at System.Windows.Forms.ContainerControl.WndProc
(Message& m)
at System.Windows.Forms.Form.WmShowWindow(Message& m)
at System.Windows.Forms.Form.WndProc(Message& m)
at System.Windows.Forms.ControlNativeWindow.OnMessage
(Message& m)
at System.Windows.Forms.ControlNativeWindow.WndProc
(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr
hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)


************** Loaded Assemblies **************
mscorlib
Assembly Version: 1.0.5000.0
Win32 Version: 1.1.4322.573
CodeBase:
file:///c:/windows/microsoft.net/framework/v1.1.4322/mscor
lib.dll
----------------------------------------
WinStartup
Assembly Version: 1.0.1418.25204
Win32 Version: 1.0.1418.25204
CodeBase: file:///C:/Documents%20and%20Settings/Tore%
20Gylver/My%20Documents/Visual%20Studio%
20Projects/Mentor/WinStartup/bin/WinStartup.exe
----------------------------------------
System.Windows.Forms
Assembly Version: 1.0.5000.0
Win32 Version: 1.1.4322.573
CodeBase:
file:///c:/windows/assembly/gac/system.windows.forms/1.0.5
000.0__b77a5c561934e089/system.windows.forms.dll
----------------------------------------
System
Assembly Version: 1.0.5000.0
Win32 Version: 1.1.4322.573
CodeBase:
file:///c:/windows/assembly/gac/system/1.0.5000.0__b77a5c5
61934e089/system.dll
----------------------------------------
System.Drawing
Assembly Version: 1.0.5000.0
Win32 Version: 1.1.4322.573
CodeBase:
file:///c:/windows/assembly/gac/system.drawing/1.0.5000.0_
_b03f5f7f11d50a3a/system.drawing.dll
----------------------------------------
System.Data
Assembly Version: 1.0.5000.0
Win32 Version: 1.1.4322.573
CodeBase:
file:///c:/windows/assembly/gac/system.data/1.0.5000.0__b7
7a5c561934e089/system.data.dll
----------------------------------------
System.Xml
Assembly Version: 1.0.5000.0
Win32 Version: 1.1.4322.573
CodeBase:
file:///c:/windows/assembly/gac/system.xml/1.0.5000.0__b77
a5c561934e089/system.xml.dll
----------------------------------------
System.Web.Services
Assembly Version: 1.0.5000.0
Win32 Version: 1.1.4322.573
CodeBase:
file:///c:/windows/assembly/gac/system.web.services/1.0.50
00.0__b03f5f7f11d50a3a/system.web.services.dll
----------------------------------------
System.Web
Assembly Version: 1.0.5000.0
Win32 Version: 1.1.4322.573
CodeBase:
file:///c:/windows/assembly/gac/system.web/1.0.5000.0__b03
f5f7f11d50a3a/system.web.dll
----------------------------------------
ygdu4pbm
Assembly Version: 0.0.0.0
Win32 Version: 1.1.4322.573
CodeBase:
file:///c:/windows/assembly/gac/system/1.0.5000.0__b77a5c5
61934e089/system.dll
----------------------------------------
Microsoft.VisualBasic
Assembly Version: 7.0.5000.0
Win32 Version: 7.10.3052.4
CodeBase:
file:///c:/windows/assembly/gac/microsoft.visualbasic/7.0.
5000.0__b03f5f7f11d50a3a/microsoft.visualbasic.dll
----------------------------------------
dsovqq1y
Assembly Version: 0.0.0.0
Win32 Version: 1.1.4322.573
CodeBase:
file:///c:/windows/assembly/gac/system/1.0.5000.0__b77a5c5
61934e089/system.dll
----------------------------------------
LibTextBoxNumeric
Assembly Version: 1.0.1418.25204
Win32 Version: 1.0.1418.25204
CodeBase: file:///C:/Documents%20and%20Settings/Tore%
20Gylver/My%20Documents/Visual%20Studio%
20Projects/Mentor/WinStartup/bin/LibTextBoxNumeric.DLL
----------------------------------------
WinStartup.resources
Assembly Version: 1.0.1418.25204
Win32 Version: 1.0.1418.25204
CodeBase: file:///C:/Documents%20and%20Settings/Tore%
20Gylver/My%20Documents/Visual%20Studio%
20Projects/Mentor/WinStartup/bin/en/WinStartup.resources.D
LL
----------------------------------------
i9vjrfkt
Assembly Version: 0.0.0.0
Win32 Version: 1.1.4322.573
CodeBase:
file:///c:/windows/assembly/gac/system/1.0.5000.0__b77a5c5
61934e089/system.dll
----------------------------------------
LibButtonLocked
Assembly Version: 1.0.1418.25204
Win32 Version: 1.0.1418.25204
CodeBase: file:///C:/Documents%20and%20Settings/Tore%
20Gylver/My%20Documents/Visual%20Studio%
20Projects/Mentor/WinStartup/bin/LibButtonLocked.DLL
----------------------------------------
Accessibility
Assembly Version: 1.0.5000.0
Win32 Version: 1.1.4322.573
CodeBase:
file:///c:/windows/assembly/gac/accessibility/1.0.5000.0__
b03f5f7f11d50a3a/accessibility.dll
----------------------------------------
jp5vhjty
Assembly Version: 0.0.0.0
Win32 Version: 1.1.4322.573
CodeBase:
file:///c:/windows/assembly/gac/system/1.0.5000.0__b77a5c5
61934e089/system.dll
----------------------------------------

************** JIT Debugging **************
To enable just in time (JIT) debugging, the config file
for this
application or machine (machine.config) must have the
jitDebugging value set in the system.windows.forms
section.
The application must also be compiled with debugging
enabled.

For example:

<configuration>
<system.windows.forms jitDebugging="true" />
</configuration>

When JIT debugging is enabled, any unhandled exception
will be sent to the JIT debugger registered on the machine
rather than being handled by this dialog.






##########################################################
##########

Here is the code where I throw the exception, in
baseFormOrder:


Private Sub BaseFormOrder_Load(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
MyBase.Load
Dim WS As New WinStartup.localhost2.OrderGet

WS.Credentials =
System.Net.CredentialCache.DefaultCredentials
DS_OrderGet2.Merge(WS.OrderGet(POrderID))
If DS_OrderGet2.PSP_OrderGet.Rows.Count() = 0 And
POrderID > 0 Then

Throw New System.ApplicationException
(BaseFormOrder_NoOrderReturned)

End If



End Sub

##########################################################
########
Here is the constructor of BaseformOrder:

Public Class BaseFormOrder
Inherits System.Windows.Forms.Form


Protected Friend POrderID As New Int32

Private OrderDeleted As New Boolean 'This flag is set
when the order is deleted and then checked during the
closing event

'If OrderDeleted is set to TRUE no saving of record
shall be done during closing as this will lead to an
exception
'OrderDeleted is set to TRUE when the operator
deletes the record



Private Const BaseFormOrder_NoOrderReturned = "Ingen
ordre ble funnet. Ordren kan være slettet"



#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

'This call is required by the Windows Form
Designer.
InitializeComponent()

Me.MdiParent = FormMenu.ActiveForm
'Me.DataGridWorkflow.Cursor = Cursors.No
Me.OrderDeleted = False




'Add any initialization after the
InitializeComponent() call

End Sub

##########################################################
##########################
Here is the constructor of FormOrderQuit:

Public Class FormOrderQuit

Inherits WinStartup.BaseFormOrder



#Region " Windows Form Designer generated code "



Public Sub New(ByVal OID As Int32)


MyBase.New()

'This call is required by the Windows Form
Designer.

InitializeComponent()


Me.MdiParent = FormMenu.ActiveForm

POrderID = OID


Me.ButtonLocked1.FormLocked = True


'Add any initialization after the
InitializeComponent() call

End Sub





Me.ButtonLocked1.FormLocked = True

This codeline runs a method for an inherited control.
What it does is to disable all buttons and set all
textboxes to readonly

##########################################################
#####################################

here is the procedure with your try-catch block in my
form FormOrdersList:

Private Sub ButtonOpen_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
ButtonOpen.Click
Dim SetOrderID As Int32


myCurrencyManager = CType(Me.BindingContext
(DataView1), CurrencyManager)

If myCurrencyManager.Count() = 0 Then
Exit Sub
End If

Dim drv As DataRowView = CType
(myCurrencyManager.Current, DataRowView)

SetOrderID = CInt(drv("orderid"))


Try
Dim frm As New FormOrderQuit(SetOrderID)
Try
frm.Show()
Catch ex As ApplicationException
MsgBox("Expected Exception" & ex.ToString)
frm.Close()
End Try
Catch ex As Exception
MsgBox("Unexpected Exception " & ex.ToString)
End Try





End Sub
 
Hi Tore,

Thanks for your detail information, it's a great help to let me understand
the problem further.
The problem seems a bit strange, as I know like other .NET languages vb.NET
needn't do extra initializatio to have the exception-handling ability, nor
can we turn it off, we need do more investigation on this problem to figure
out the problem.

We may try let VS.NET help us catch the first-chance exception and check
more info about the exception, such as the exact exception name, on which
thread was it thrown out, and the call stack of that thread.
Here is the detail steps:
1.Open the Exceptions dialog, tell VS.NET break into the debugger when an
CLR exception happens, you may open the dialog by "Debug"->"Exception"
command.
select the "Common Language Runtime Exceptions" and select "Break into the
debugger".
2.run your program in release build, when exception happens, a dialog will
appear with message such as "A first chance exception of type....",
please write down the exception type name, then choose break. You may get a
warning says no source code, it's ok, click ok to close it.
3. open the Threads window to see which is the current thread, open the
Call Stack Window you may find the call stack on the current thread.
You may find the Thread window under the "Debug"->"Windows" menu.

Also it would be helpful if you can add some debug output in your code,
such as
<code>
Private Sub BaseFormOrder_Load(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
MyBase.Load
Dim WS As New WinStartup.localhost2.OrderGet
WS.Credentials = System.Net.CredentialCache.DefaultCredentials
DS_OrderGet2.Merge(WS.OrderGet(POrderID))
Console.WriteLine("Merge OK");
If DS_OrderGet2.PSP_OrderGet.Rows.Count() = 0 And
POrderID > 0 Then
Console.WriteLine("Before Throw ApplicationException")
Throw New System.ApplicationException
(BaseFormOrder_NoOrderReturned)
End If
End Sub
</code>

Another quick thought to make sure if the exception handling has effect in
release build. You may try catching the exception in the On_load event
handler to see if the catch block will be fired.

Thanks for your patience, I'll follow up with you on this issue.
If you have anything unclear on it, please reply to this thread to let me
know.
Thanks!




Best regards,

Ying-Shen Yu [MSFT]
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security

This posting is provided "AS IS" with no warranties and confers no rights.
You should not reply this mail directly, "Online" should be removed before
sending.
 
Hi Tore,

I'm glad to hear you can catch the exception now.
If you meet any problem in debuggin your program , please be free to post
it on the newsgroup.
We will be happy to help you.



Best regards,

Ying-Shen Yu [MSFT]
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security

This posting is provided "AS IS" with no warranties and confers no rights.
You should not reply this mail directly, "Online" should be removed before
sending.
 
Back
Top