0

I have a custom error page on IIS8.5. Sometimes the error page itself throws an exception:

Object reference not set to an instance of an object.

This is part of my code behind:

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        HttpContext.Current.Response.StatusCode = 500
        'Dim CurrentException As Exception = Server.GetLastError()
        'virheettxt.text = CurrentException.Message
        Dim hostName = System.Net.Dns.GetHostName()



        Dim ctxOBJ As HttpContext
        Dim exceptionOBJ As Exception
        Dim errorInfoTXT As String

        ctxOBJ = HttpContext.Current()

        exceptionOBJ = ctxOBJ.Server.GetLastError()

        errorInfoTXT = " <br>Offending URL: " & iif(Not ctxOBJ Is Nothing, ctxOBJ.Request.Url.ToString(), "ei saatavilla") &
"<br>Source: " & iif(Not exceptionOBJ Is Nothing, exceptionOBJ.Source.ToString(), "ei saatavilla") &
"<br>Message: " & iif(Not exceptionOBJ Is Nothing, exceptionOBJ.Message.ToString(), "ei saatavilla") &
"<br>Stack trace: " & iif(Not exceptionOBJ Is Nothing, exceptionOBJ.StackTrace.ToString(), "ei saatavilla") &
"<br>Target Site: " & iif(Not exceptionOBJ Is Nothing, exceptionOBJ.TargetSite.ToString(), "ei saatavilla") &
"<br>Server: " & hostName
        Dim virheurlsc = ctxOBJ.Request.Url.ToString()



        ctxOBJ.Server.ClearError()

Error comes from line: errorInfoTXT = "
Offending URL: ......

And if there is a way to capture the error line I would really need it too on some cases...?

Andrew Morton
  • 24,203
  • 9
  • 60
  • 84
Timo77
  • 145
  • 1
  • 1
  • 19
  • Please see [Add line numbers to stack trace of ASP.NET web site that is deployed in release mode](http://stackoverflow.com/a/20069668/1115360) for your second question. – Andrew Morton May 03 '16 at 12:47
  • I think the answer I referred to tells you that. – Andrew Morton May 03 '16 at 12:50
  • If i Understod that, it is no possible to have line numbers on separate custom error page. It have to be buid inside application? What about classic asp pages? we are using some of and would really need error lines from those on aspx(vb) custom error page. – Timo77 May 04 '16 at 05:05

1 Answers1

0

The problem comes from using Iif, which evaluates both the true and false cases. Using the If() operator instead will prevent that.

You should also check if ctxOBJ is nothing before trying to use it, and if you reverse the true and false arguments in the If()s it will make it a little bit simpler:

Dim ctxOBJ As HttpContext = HttpContext.Current()
Dim exceptionOBJ As Exception = Nothing

If ctxOBJ IsNot Nothing Then
    exceptionOBJ = ctxOBJ.Server.GetLastError()
End If

Dim errorInfoTXT As String = " <br>Offending URL: " & If(ctxOBJ Is Nothing, "ei saatavilla", ctxOBJ.Request.Url.ToString()) &
"<br>Source: " & If(exceptionOBJ Is Nothing, "ei saatavilla", exceptionOBJ.Source.ToString()) &
"<br>Message: " & If(exceptionOBJ Is Nothing, "ei saatavilla", exceptionOBJ.Message.ToString()) &
"<br>Stack trace: " & If(exceptionOBJ Is Nothing, "ei saatavilla", exceptionOBJ.StackTrace.ToString()) &
"<br>Target Site: " & If(exceptionOBJ Is Nothing, "ei saatavilla", exceptionOBJ.TargetSite.ToString()) &
"<br>Server: " & hostName
Andrew Morton
  • 24,203
  • 9
  • 60
  • 84