Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations dencom on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

When to use Server.GetLastError() and GetBaseException()? 1

Status
Not open for further replies.

ericnet

Programmer
Mar 29, 2006
106
I know that "Server.GetLastError()" returns an object of type Exception, and it is (more or less) the whole wrapped package of the last exception. While GetBaseException() returns the exception that is the root cause of one or more subsequent exceptions, so the original exception that caused the problem. But..

Which is the difference between detecting, logging and reporting exception information using "Server.GetLastError()" or without it? If I don’ t use it I will not get the last error?

So, for example, which is the difference between this way?:
Code:
[b]Dim ex As Exception = Server.GetLastError().GetBaseException()[/b]

EventLog.WriteEntry("Test Web", "Message: " & ex.Message & _
     "\nSOURCE: " & ex.Source & "\nTARGETSITE: " & ex.TargetSite & _
     "\nSTACKTRACE: " & ex.StackTrace, _ etc,..
And this way?:
Code:
[b]Dim ex As Exception[/b]

EventLog.WriteEntry("Test Web", "Message: " & ex.Message & _
     "\nSOURCE: " & ex.Source & "\nTARGETSITE: " & ex.TargetSite & _
     "\nSTACKTRACE: " & ex.StackTrace, _ etc,..

Is the first way, using Server.GetLastError(), for use it only in Application_Error Subroutine in global.asax file? And what about GetBaseException()? When to use it?

Thanks
 
It depends on how you've set up the relevant functions you are working on. For example, if you call a function that returns an untrapped error which in turn causes another error, simply using your second method you may not get the real error. On the other hand, if you have a simply Try/Catch on a page load for one method, you will be able to get the error fine using the second method.

As you've said, the first method is useful for catching Page or Application errors, but in other scenario's it maybe isn't needed.


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Hello,

On the other hand, if you have a simply Try/Catch on a page load for one method, you will be able to get the error fine using the second method.

Ok but, if in a simply Try/Catch block is produced an error that in turn causes another error, and so on, then if I don’t use GetLastError() in the catch section I only will get the first error produced? If so, perhaps it is better always use GetLastError()... And to know all the errors produced use Exception.InnerException property..

Perhaps the difference between using ‘Server.GetLastError()’ or only ‘Exception’ is the first one returns the whole wrapped package of the last exception, while the second one only returns the very last error. If so, again, perhaps it is better always use GetLastError()...

I’m a little confused about this

And what about GetBaseException()?

Thanks
 
Ca8msm,
Indeed my real doubt is this:

It’s clear that for managing unhandled exceptions in global.asax file (detecting, logging and reporting) it is better to use Server.GetLastError(), in the way we said:

(global.asax file)
Code:
<script language="VB" runat="server">

 Sub Application_Error(Sender As Object, E As EventArgs)

  Dim ex As Exception = Server.GetLastError().GetBaseException()

  EventLog.WriteEntry("Test Web", "Message: " & ex.Message & _
     "\nSOURCE: " & ex.Source & "\nTARGETSITE: " & ex.TargetSite & _
     "\nSTACKTRACE: " & ex.StackTrace, _ etc,..

  ‘Here send e-mail notification to administrator with same information logged above

 End Sub

</script>
Agreed?

But if I want to log and e-mail an exception in the same way but from a Try/Catch block on a page load, Would you do it in this way?:
Code:
Sub Page_Load(Sender As Object, E As EventArgs)
 Try

  ‘Code here 

 Catch ex As Exception 

  EventLog.WriteEntry("Test Web", "Message: " & ex.Message & _
      "\nSOURCE: " & ex.Source & "\nTARGETSITE: " & ex.TargetSite & _
      "\nSTACKTRACE: " & ex.StackTrace, _ etc,..

   ‘Here send e-mail notification to administrator with same information logged above
  
 End Try
End Sub

Or it would be better do something like this?:
Code:
Sub Page_Load(Sender As Object, E As EventArgs)
 Try

  ‘Code here 

 Catch ex As Exception 
   Dim wholeEx As Exception = ex
   wholeEx = Server.GetLastError().GetBaseException() 

  EventLog.WriteEntry("Test Web", "Message: " & wholeEx.Message & _
      "\nSOURCE: " & wholeEx.Source & "\nTARGETSITE: " & wholeEx.TargetSite & _
      "\nSTACKTRACE: " & wholeEx.StackTrace, _ etc,..

   ‘Here send e-mail notification to administrator with same information logged above
  
 End Try
End Sub

Thanks!
 
Yes agreed [smile]

As for your second example, I'd go with the first method. The Try/Catch block should catch any unexpected errors and if it does, it will be called when the first error occurs and therefore the exception will hold the cause of the error.


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top