468,290 Members | 1,854 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,290 developers. It's quick & easy.

Newby Question : Update to Sub Main in vb.net

Hi:

I'm trying to implement an overall error handler in my VB.NET windows
application.

I need to place an "On Error Goto" in "Sub Main".

I have tried to create a Sub Main and have had no luck - all the examples
I've read don't explain this to me either.

Is there some place where Sub Main is defined when you create a project -
any help would be GREATLY appreciated!

Thanks,

Fred
Nov 21 '05 #1
17 1387
"Fred Nelson" <fr**@smartybird.com> schrieb:
I'm trying to implement an overall error handler in my
VB.NET windows application.

I need to place an "On Error Goto" in "Sub Main".

I have tried to create a Sub Main and have had no luck - all the examples
I've read don't explain this to me either.


\\\
Public Module Program
Public Sub Main()
...
End Sub
End Module
///

In the project properties, set 'Sub Main' as startup object.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 21 '05 #2
Herfried:

Thanks very much for your reply! I'm very confused as to which file this
goes in. I've created a new project and I have attempted to inserte the
code:

public module program
public sub main()
end sub
end module

in my Form1 and I get lot of errors.

Should this be a class library - or where should this go?

Thanks again!

Fred

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...
"Fred Nelson" <fr**@smartybird.com> schrieb:
I'm trying to implement an overall error handler in my
VB.NET windows application.

I need to place an "On Error Goto" in "Sub Main".

I have tried to create a Sub Main and have had no luck - all the examples I've read don't explain this to me either.


\\\
Public Module Program
Public Sub Main()
...
End Sub
End Module
///

In the project properties, set 'Sub Main' as startup object.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>

Nov 21 '05 #3
Herfired:

I figured it out - I created a class and made it the startup object and then
called my form!

Thanks again for your help!

Fred

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...
"Fred Nelson" <fr**@smartybird.com> schrieb:
I'm trying to implement an overall error handler in my
VB.NET windows application.

I need to place an "On Error Goto" in "Sub Main".

I have tried to create a Sub Main and have had no luck - all the examples I've read don't explain this to me either.


\\\
Public Module Program
Public Sub Main()
...
End Sub
End Module
///

In the project properties, set 'Sub Main' as startup object.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>

Nov 21 '05 #4
it's easiest to create a module.vb file and run the form from there.

In the project you can tell it to load the module main on startup.
Nov 21 '05 #5
"Fred Nelson" <fr**@smartybird.com> schrieb:
Thanks very much for your reply! I'm very confused as to which file this
goes in. I've created a new project and I have attempted to inserte the
code:

public module program
public sub main()
end sub
end module

in my Form1 and I get lot of errors.

Should this be a class library - or where should this go?


Add an empty file called "Program.vb" to your project and enter the code I
posted previously.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 21 '05 #6
Herfried:

Thanks for your help - I have it working just fine now!

I would like to have the error handling routine be able to return the error
number, program (form) name, and the line number of the failure.

The err.number and err.description are just what I'm looking for however I
am not able to get the form that failed or the line number - erl always
returns zero and the program name function always returns the project name.

Do you have any suggestions on how I might accomplish this?

Thanks again for all your help!

Fred
"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:up**************@TK2MSFTNGP09.phx.gbl...
"Fred Nelson" <fr**@smartybird.com> schrieb:
Thanks very much for your reply! I'm very confused as to which file this goes in. I've created a new project and I have attempted to inserte the
code:

public module program
public sub main()
end sub
end module

in my Form1 and I get lot of errors.

Should this be a class library - or where should this go?


Add an empty file called "Program.vb" to your project and enter the code I
posted previously.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>

Nov 21 '05 #7
Herfried:

Thanks for your help - I have it working just fine now!

I would like to have the error handling routine be able to return the error
number, program (form) name, and the line number of the failure.

The err.number and err.description are just what I'm looking for however I
am not able to get the form that failed or the line number - erl always
returns zero and the program name function always returns the project name.

Do you have any suggestions on how I might accomplish this?

Thanks again for all your help!

Fred
"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:up**************@TK2MSFTNGP09.phx.gbl...
"Fred Nelson" <fr**@smartybird.com> schrieb:
Thanks very much for your reply! I'm very confused as to which file this goes in. I've created a new project and I have attempted to inserte the
code:

public module program
public sub main()
end sub
end module

in my Form1 and I get lot of errors.

Should this be a class library - or where should this go?


Add an empty file called "Program.vb" to your project and enter the code I
posted previously.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>

Nov 21 '05 #8
"Fred Nelson" <fr**@smartybird.com> wrote in message
news:uL**************@TK2MSFTNGP15.phx.gbl...
I'm trying to implement an overall error handler in my VB.NET
windows application.
Bad Idea, IMHO, but anyway ...
I need to place an "On Error Goto" in "Sub Main".


Well .. sort of.
Add a Module to your project. In this Module, code

Sub Main
Try
Application.Run( New Form1 )

Catch ex as System.Exception
' Error reporting code here

End Try
End Sub

(Replace Form1 with the class name of your main form).

Then change the Project properties to start with Sub Main, instead
of your form.

HTH,
Phill W.
Nov 21 '05 #9

"Phill. W" <P.A.Ward@o-p-e-n-.-a-c-.-u-k> wrote
I'm trying to implement an overall error handler in my VB.NET
windows application.
I need to place an "On Error Goto" in "Sub Main".
Add a Module to your project. In this Module, code

Sub Main
Try
Application.Run( New Form1 )

Catch ex as System.Exception
' Error reporting code here

End Try
End Sub


While that catches errors on the main thread, it will not
catch other errors that may be raised. For a more
comprehensive discussion on error handlers, see this
webcast:

http://msevents.microsoft.com/CUI/We...CountryCode=US

LFS

Nov 21 '05 #10
Hi Phil:

This program generates a bunch of form letters (envelopes/letterhead/second
sheets) so in this case I simply want the program to terminate and report
the name of the program, line number, and the error description to me.
(There is no action that the user can take if this fails except click OK).

Should an error occur I want to update an SQL database with the error
message which I will do in my error handling routine.

I have code setup exactly as you describe and it does trap all errors. My
problem now is that I am unable to identify the program (form) that failed
and the line number:

err.number tells me what happened - error number
err.source returns the main module name
err.description tells me what happend.

Now - I'll have a "home run" if I can identify the program and line number
that caused the error.

If you have any suggestions on how to do this I would GREATLY appreciate it!

Thansk very much,

Fred

"Phill. W" <P.A.Ward@o-p-e-n-.-a-c-.-u-k> wrote in message
news:cl**********@yarrow.open.ac.uk...
"Fred Nelson" <fr**@smartybird.com> wrote in message
news:uL**************@TK2MSFTNGP15.phx.gbl...
I'm trying to implement an overall error handler in my VB.NET
windows application.


Bad Idea, IMHO, but anyway ...
I need to place an "On Error Goto" in "Sub Main".


Well .. sort of.
Add a Module to your project. In this Module, code

Sub Main
Try
Application.Run( New Form1 )

Catch ex as System.Exception
' Error reporting code here

End Try
End Sub

(Replace Form1 with the class name of your main form).

Then change the Project properties to start with Sub Main, instead
of your form.

HTH,
Phill W.

Nov 21 '05 #11
Larry:

Thanks for your reply!

When I used the code you gave me I was able to access ex.stacktrace and get
EXACTLY the information that I needed!

This is GREAT!

Now I will be able to call a stored procedure to log the error in the
database!

Thanks!!!

"Larry Serflaten" <se*******@usinternet.com> wrote in message
news:uF**************@TK2MSFTNGP09.phx.gbl...

"Phill. W" <P.A.Ward@o-p-e-n-.-a-c-.-u-k> wrote
I'm trying to implement an overall error handler in my VB.NET
windows application.
I need to place an "On Error Goto" in "Sub Main".
Add a Module to your project. In this Module, code

Sub Main
Try
Application.Run( New Form1 )

Catch ex as System.Exception
' Error reporting code here

End Try
End Sub


While that catches errors on the main thread, it will not
catch other errors that may be raised. For a more
comprehensive discussion on error handlers, see this
webcast:

http://msevents.microsoft.com/CUI/We...CountryCode=US
LFS

Nov 21 '05 #12
Fred,
I'm trying to implement an overall error handler in my VB.NET windows
application. In addition to the other comments:

Depending on the type of application you are creating, .NET has three
different global exception handlers.

For ASP.NET look at:
System.Web.HttpApplication.Error event
Normally placed in your Global.asax file.

For console applications look at:
System.AppDomain.UnhandledException event
Use AddHandler in your Sub Main.

For Windows Forms look at:
System.Windows.Forms.Application.ThreadException event
Use AddHandler in your Sub Main.

It can be beneficial to combine the above global handlers in your app, as
well as wrap your Sub Main in a try catch itself.

There is an article in the June 2004 MSDN Magazine that shows how to
implement the global exception handling in .NET that explains why & when you
use multiple of the above handlers...

http://msdn.microsoft.com/msdnmag/is...T/default.aspx

For example: In my Windows Forms apps I would have a handler attached to the
Application.ThreadException event, plus a Try/Catch in my Main. The
Try/Catch in Main only catches exceptions if the constructor of the MainForm
raises an exception, the Application.ThreadException handler will catch all
uncaught exceptions from any form/control event handlers.

Hope this helps
Jay

"Fred Nelson" <fr**@smartybird.com> wrote in message
news:uL**************@TK2MSFTNGP15.phx.gbl... Hi:

I'm trying to implement an overall error handler in my VB.NET windows
application.

I need to place an "On Error Goto" in "Sub Main".

I have tried to create a Sub Main and have had no luck - all the examples
I've read don't explain this to me either.

Is there some place where Sub Main is defined when you create a project -
any help would be GREATLY appreciated!

Thanks,

Fred

Nov 21 '05 #13
Jay:

I "almost" have it working.

My program starts with a Main as follows:

--------------
Public Module main

Public Sub main()

Try

Application.Run(New letterprint)

Catch ex As Exception

** code that writes error to an sql database

-------------------

Now the catch: it works just fine if I'm in the development environment.
When I compile the program in either debug or release mode and have an error
I get a pop-up dialog box:

An unhandled Exception Error has occurred in your application. If yoiu
click continue, the application will ignore this error and attempt to
continue.

If folks continue the program returns to the next line as though no error
happened.

Is there additional code that I must put into the forms that will prevent
this from happening - I just need to write the error to my SQL database and
terminate the program.

Once again your help is GREATLY appreciated!

Fred


"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:uW**************@TK2MSFTNGP15.phx.gbl...
Fred,
I'm trying to implement an overall error handler in my VB.NET windows
application. In addition to the other comments:

Depending on the type of application you are creating, .NET has three
different global exception handlers.

For ASP.NET look at:
System.Web.HttpApplication.Error event
Normally placed in your Global.asax file.

For console applications look at:
System.AppDomain.UnhandledException event
Use AddHandler in your Sub Main.

For Windows Forms look at:
System.Windows.Forms.Application.ThreadException event
Use AddHandler in your Sub Main.

It can be beneficial to combine the above global handlers in your app, as
well as wrap your Sub Main in a try catch itself.

There is an article in the June 2004 MSDN Magazine that shows how to
implement the global exception handling in .NET that explains why & when

you use multiple of the above handlers...

http://msdn.microsoft.com/msdnmag/is...T/default.aspx

For example: In my Windows Forms apps I would have a handler attached to the Application.ThreadException event, plus a Try/Catch in my Main. The
Try/Catch in Main only catches exceptions if the constructor of the MainForm raises an exception, the Application.ThreadException handler will catch all uncaught exceptions from any form/control event handlers.

Hope this helps
Jay

"Fred Nelson" <fr**@smartybird.com> wrote in message
news:uL**************@TK2MSFTNGP15.phx.gbl...
Hi:

I'm trying to implement an overall error handler in my VB.NET windows
application.

I need to place an "On Error Goto" in "Sub Main".

I have tried to create a Sub Main and have had no luck - all the examples I've read don't explain this to me either.

Is there some place where Sub Main is defined when you create a project - any help would be GREATLY appreciated!

Thanks,

Fred


Nov 21 '05 #14
Fred,
As I stated:
For example: In my Windows Forms apps I would have a handler attached to the
Application.ThreadException event,


The link I gave goes into further details.

Something like:

Public Module MainModule

Public Sub Main
AddHandler Application.ThreadException, AddressOf
Application_ThreadException
Try
Application.Run(New LetterPrintForm)
Catch ex As Exception
LogException(ex)
End Try
End Sub

Private Sub Application_ThreadException(ByVal sender As Object,
ByVal e As System.Threading.ThreadExceptionEventArgs)
LogException(e.Exception)
End Sub

Private Sub LogException(ByVal ex As Exception) ** code that writes error to an sql database End Sub

Hope this helps
Jay

"Fred Nelson" <fr**@smartybird.com> wrote in message
news:eN****************@TK2MSFTNGP09.phx.gbl... Jay:

I "almost" have it working.

My program starts with a Main as follows:

--------------
Public Module main

Public Sub main()

Try

Application.Run(New letterprint)

Catch ex As Exception

** code that writes error to an sql database

-------------------

Now the catch: it works just fine if I'm in the development environment.
When I compile the program in either debug or release mode and have an
error
I get a pop-up dialog box:

An unhandled Exception Error has occurred in your application. If yoiu
click continue, the application will ignore this error and attempt to
continue.

If folks continue the program returns to the next line as though no error
happened.

Is there additional code that I must put into the forms that will prevent
this from happening - I just need to write the error to my SQL database
and
terminate the program.

Once again your help is GREATLY appreciated!

Fred


"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:uW**************@TK2MSFTNGP15.phx.gbl...
Fred,
> I'm trying to implement an overall error handler in my VB.NET windows
> application.

In addition to the other comments:

Depending on the type of application you are creating, .NET has three
different global exception handlers.

For ASP.NET look at:
System.Web.HttpApplication.Error event
Normally placed in your Global.asax file.

For console applications look at:
System.AppDomain.UnhandledException event
Use AddHandler in your Sub Main.

For Windows Forms look at:
System.Windows.Forms.Application.ThreadException event
Use AddHandler in your Sub Main.

It can be beneficial to combine the above global handlers in your app, as
well as wrap your Sub Main in a try catch itself.

There is an article in the June 2004 MSDN Magazine that shows how to
implement the global exception handling in .NET that explains why & when

you
use multiple of the above handlers...

http://msdn.microsoft.com/msdnmag/is...T/default.aspx

For example: In my Windows Forms apps I would have a handler attached to

the
Application.ThreadException event, plus a Try/Catch in my Main. The
Try/Catch in Main only catches exceptions if the constructor of the

MainForm
raises an exception, the Application.ThreadException handler will catch

all
uncaught exceptions from any form/control event handlers.

Hope this helps
Jay

<<snip>>
Nov 21 '05 #15
Jay:

Thanks again for your reply and your patience with me as I try to figure
this out. I have the routine working now as I had hoped - well almost.

I will have a "home run" if there is a way to get the program and line
number that caused the crash when the program is run on client machines.

I passed the entire System.Threading.ThreadExceptionEventArgs to the
logexception subroutine.

When I run the program in debug mode on my development machine I get the
program name and line numbers from
ex.Exception.StackTrace.ToString.

When I run on any other machine - even with the code in debug mode - I do
get information however it does not contain the information that I need
most - line # and form name.

Is it possible to obtain this information?

Thanks again for your help!

Fred

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:uu***************@TK2MSFTNGP11.phx.gbl...
Fred,
As I stated:
For example: In my Windows Forms apps I would have a handler attached to
the
Application.ThreadException event,
The link I gave goes into further details.

Something like:

Public Module MainModule

Public Sub Main
AddHandler Application.ThreadException, AddressOf
Application_ThreadException
Try
Application.Run(New LetterPrintForm)
Catch ex As Exception
LogException(ex)
End Try
End Sub

Private Sub Application_ThreadException(ByVal sender As Object,
ByVal e As System.Threading.ThreadExceptionEventArgs)
LogException(e.Exception)
End Sub

Private Sub LogException(ByVal ex As Exception)
> ** code that writes error to an sql database

End Sub

Hope this helps
Jay

"Fred Nelson" <fr**@smartybird.com> wrote in message
news:eN****************@TK2MSFTNGP09.phx.gbl...
Jay:

I "almost" have it working.

My program starts with a Main as follows:

--------------
Public Module main

Public Sub main()

Try

Application.Run(New letterprint)

Catch ex As Exception

** code that writes error to an sql database

-------------------

Now the catch: it works just fine if I'm in the development environment.
When I compile the program in either debug or release mode and have an
error
I get a pop-up dialog box:

An unhandled Exception Error has occurred in your application. If yoiu
click continue, the application will ignore this error and attempt to
continue.

If folks continue the program returns to the next line as though no

error happened.

Is there additional code that I must put into the forms that will prevent this from happening - I just need to write the error to my SQL database
and
terminate the program.

Once again your help is GREATLY appreciated!

Fred


"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message news:uW**************@TK2MSFTNGP15.phx.gbl...
Fred,
> I'm trying to implement an overall error handler in my VB.NET windows
> application.
In addition to the other comments:

Depending on the type of application you are creating, .NET has three
different global exception handlers.

For ASP.NET look at:
System.Web.HttpApplication.Error event
Normally placed in your Global.asax file.

For console applications look at:
System.AppDomain.UnhandledException event
Use AddHandler in your Sub Main.

For Windows Forms look at:
System.Windows.Forms.Application.ThreadException event
Use AddHandler in your Sub Main.

It can be beneficial to combine the above global handlers in your app,
as well as wrap your Sub Main in a try catch itself.

There is an article in the June 2004 MSDN Magazine that shows how to
implement the global exception handling in .NET that explains why &

when you
use multiple of the above handlers...

http://msdn.microsoft.com/msdnmag/is...T/default.aspx

For example: In my Windows Forms apps I would have a handler attached
to the
Application.ThreadException event, plus a Try/Catch in my Main. The
Try/Catch in Main only catches exceptions if the constructor of the

MainForm
raises an exception, the Application.ThreadException handler will catch

all
uncaught exceptions from any form/control event handlers.

Hope this helps
Jay

<<snip>>

Nov 21 '05 #16
Fred,
When you compile in Debug mode the "Generate debugging information" option
is normally turned on, under "Project - Properties - Configuration
Properties - Build". In Release builds this option is normally turned off.

This option creates a Program Database file (.pdb) if you copy this file to
the other machine along with your executable then you will get line numbers
& file names in your stack trace.

You can change the option for Release builds if you would like to include
the .pdb file in release builds. However! including the .pdb file can
simplify decompiling your program...

NOTE: The "form name" should be included as part of the stack trace as its
the class name.

Hope this helps
Jay

"Fred Nelson" <fr**@smartybird.com> wrote in message
news:OA**************@TK2MSFTNGP15.phx.gbl...
Jay:

Thanks again for your reply and your patience with me as I try to figure
this out. I have the routine working now as I had hoped - well almost.

I will have a "home run" if there is a way to get the program and line
number that caused the crash when the program is run on client machines.

I passed the entire System.Threading.ThreadExceptionEventArgs to the
logexception subroutine.

When I run the program in debug mode on my development machine I get the
program name and line numbers from
ex.Exception.StackTrace.ToString.

When I run on any other machine - even with the code in debug mode - I do
get information however it does not contain the information that I need
most - line # and form name.

Is it possible to obtain this information?

Thanks again for your help!

Fred

<<snip>>
Nov 21 '05 #17
Jay:

Thanks SO much for your help!

It's all working as it should - line numbers and everything. In this case
there are no worries about decompilation.

I'm sure that my program will never crash however its nice to know that
there will be a way to find out what happened if it ever did!

Thanks again,

Fred Nelson
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
Fred,
When you compile in Debug mode the "Generate debugging information" option
is normally turned on, under "Project - Properties - Configuration
Properties - Build". In Release builds this option is normally turned off.

This option creates a Program Database file (.pdb) if you copy this file to the other machine along with your executable then you will get line numbers & file names in your stack trace.

You can change the option for Release builds if you would like to include
the .pdb file in release builds. However! including the .pdb file can
simplify decompiling your program...

NOTE: The "form name" should be included as part of the stack trace as its
the class name.

Hope this helps
Jay

"Fred Nelson" <fr**@smartybird.com> wrote in message
news:OA**************@TK2MSFTNGP15.phx.gbl...
Jay:

Thanks again for your reply and your patience with me as I try to figure
this out. I have the routine working now as I had hoped - well almost.

I will have a "home run" if there is a way to get the program and line
number that caused the crash when the program is run on client machines.

I passed the entire System.Threading.ThreadExceptionEventArgs to the
logexception subroutine.

When I run the program in debug mode on my development machine I get the
program name and line numbers from
ex.Exception.StackTrace.ToString.

When I run on any other machine - even with the code in debug mode - I do get information however it does not contain the information that I need
most - line # and form name.

Is it possible to obtain this information?

Thanks again for your help!

Fred

<<snip>>

Nov 21 '05 #18

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by Damien | last post: by
20 posts views Thread by Jack Schitt | last post: by
10 posts views Thread by Fred Nelson | last post: by
4 posts views Thread by Fred Nelson | last post: by
2 posts views Thread by johnnyG | last post: by
14 posts views Thread by Darren L. Weber | last post: by
2 posts views Thread by MrBee | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.