Newby Question : Update to Sub Main in vb.net

  • Thread starter Thread starter Fred Nelson
  • Start date Start date
F

Fred Nelson

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
 
Fred Nelson said:
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:

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
 
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
 
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.
 
Fred Nelson said:
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:

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
 
Fred Nelson said:
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.
 
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
 
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!!!
 
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/issues/04/06/NET/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
 
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
 
Fred,
As I stated:

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 said:
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
<<snip>>
 
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
 
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 said:
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>>
 
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
 

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

Back
Top