Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Hi. Is it possible to catch every error that occures? I'd like to create a Error tracker, so when error occures it should send me err.number, err.description etc...

 

So one way is: In every procedure and every function I create:

Private Sub
On Error goto _error
...
Exit sub
_error:
'here I would send error's
End Sub

 

But I think that this is very old fashioned...

 

And, if I use on error, then I can not use Try...Catch statements....

 

 

the other way is this (in every sub and every function):

Private Sub
Try
...
Catch ex as Exeption
'here I would send error's
End Try
End Sub

 

 

But this is not what I want. Is there any way to do it more fluently. For ex. to catch any occured error in one place? (not in each procedure or function).

 

 

tx

 

 

matej

Posted
try
'insert code here
 try
 'insert more code here
 catch er as exception
 'insert error code
 end try
 try
 'insert more code here
 catch er as exception
 'insert error code
 end try
 try
 'insert more code here
 catch er as exception
 'insert error code
 end try
catch er as exception
'insert overall error code
end try

  • *Experts*
Posted

If this is a windows app, you can catch the event for Application.ThreadException, something like:

static void Main()
{
   Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
}

private static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e) 
{
// Handle e.Exception
}

 

-ner

"I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
Posted

Isnt the whole point of error handling to handle errors? If your going to cut out specific error handling by using a global error handler then you may aswell just let the exceptions go without being handled!

 

Using try..catch around specific functions and procedures will allow much more structured handling and give you the ability to show more detailed messages as to why an exception took place, not to mention knowing where it was the error happened.

Visit: VBSourceSeek - The VB.NET sourcecode library

 

 

"A mere friend will agree with you, but a real friend will argue."
Posted

I have often used a global error handler in my sub main. it helps filter the important information. Remember try catc statements can be nested so if you need to put in some more specific error handling then np. this however will also need to point to the error log.

 

While I agree that global error handling can be useless and in some respects more effort than its worth, especially if the wrong information is relayed in the error message it can make it harder to find errors than if you had no error handling at all! However i frrl that if done correctly it can simplify the debugging process

 

Dill

Posted
You want to just send exceptions to a central point for debugging purposes? If you want to log the exceptions (file, screen, etc) you can use a TraceListener, System.Diagnostics.TraceListener
Posted
You'll probably want a hook to the AppDomain.UnhandledException Event.

 

I checked this article, and copies vb code into empty module. Set startupform Sub Main and run.

 

But it doesn't handle second errror. So what is wrong? I do not understand what permissions I need.

 

tx in advance

 

 

 

(for lazy ones - here is the code)

 

Sub Main()
  Dim currentDomain As AppDomain = AppDomain.CurrentDomain
  AddHandler currentDomain.UnhandledException, AddressOf MyHandler
  
  Try
     Throw New Exception("1")
  Catch e As Exception
     Console.WriteLine("Catch clause caught : " + e.Message)
  End Try
  
  Throw New Exception("2")

  ' Output:
  '   Catch clause caught : 1
  '   MyHandler caught : 2
End Sub 'Main


Sub MyHandler(sender As Object, args As UnhandledExceptionEventArgs)
  Dim e As Exception = DirectCast(args.ExceptionObject, Exception)
  Console.WriteLine("MyHandler caught : " + e.Message)
End Sub 'MyUnhandledExceptionEventHandler

and remarks

Remarks

The UnhandledExceptionEventHandler delegate for this event provides default handling for uncaught exceptions. When this event is not handled, the system default handler reports the exception to the user and terminates the application.

 

This event occurs only for the application domain that is created by the system when an application is started. If an application creates additional application domains, specifying a delegate for this event in those applications domains has no effect.

 

To register an event handler for this event, you must have the permissions described in the Permissions section. If you do not have the appropriate permissions, a SecurityException occurs.

 

For more information about handling events, see Consuming Events.

 

Posted
I checked this article, and copies vb code into empty module. Set startupform Sub Main and run.

 

But it doesn't handle second errror. So what is wrong? I do not understand what permissions I need.and remarks

 

What's wrong is, your exception is being thrown in the main thread, you can only have 1 main thread, and 0 or more background threads.

When an exception is thrown, it unwinds the call stack until it is caught, if you don't catch the exception, the thread will terminate. Terminating the non-background thread, the process ends.

 

 

I don't know vb, but I made a sample in c#, and this is part of the decompiled vb.net code of the c# sample.

Private Shared Sub Main()
     AddHandler AppDomain.CurrentDomain.UnhandledException, New UnhandledExceptionEventHandler(AddressOf Class2.MyHandler)
     Dim thread1 As New Thread(New ThreadStart(AddressOf Class2.Throw))
     thread1.IsBackground = True
     thread1.Start
     Console.ReadLine
End Sub

Private Shared Sub [Throw]()
     Try 
           Throw New Exception("1")
     Catch exception2 As  Exception
           Console.WriteLine(("Catch clause caught : " & exception2.Message))
     End Try
     Throw New Exception("2")
End Sub

Public Shared Sub MyHandler(ByVal sender As Object, ByVal args As UnhandledExceptionEventArgs)
     Dim exception1 As Exception = CType(args.ExceptionObject,Exception)
     Console.WriteLine(("MyHandler caught : " & exception1.Message))
End Sub

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...