# VB.NET 404 Error Handling



## Nitrus (Dec 27, 2006)

I have this code, and on the bold line It says a "WebException is unhandled" So I put in the following "If" statement in hoping that it would handle the error.

I tried setting the 404 Status Code as a Variable, but to no avail.

Could someone help? Cheers.


```
Shared Function GetFileSize(ByVal sUrl As String) As Long
        Dim m_Request As HttpWebRequest = Nothing
        Dim m_response As HttpWebResponse = Nothing

        m_Request = HttpWebRequest.Create(sUrl)
        m_Request.Timeout = 30000

        ' Set some reasonable limits on resources used by this request
        m_Request.AllowAutoRedirect = True
        m_Request.MaximumAutomaticRedirections = 3
        m_Request.MaximumResponseHeadersLength = 4

        m_Request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 2.0.50727;)"

        [B]m_response = m_Request.GetResponse()[/B]

        If m_response.StatusCode = HttpStatusCode.NotFound Then
            m_Request.Abort()
            MsgBox("ERROR:You are trying to download Restricted Content.", MsgBoxStyle.OkOnly, "Warning!")
        End If



        '//get Source Size
        Dim m_contentLength() As String = m_response.Headers.GetValues("Content-Length")

        Return m_contentLength(0)
    End Function
```


----------



## VincentWong (Aug 30, 2006)

You need to wrap the line in bold in a try/ catch block like this (note that VB syntax is different from what i posted below):

try
{

m_response = m_Request.GetResponse()

catch (Exception)
{
MsgBox("ERROR:You are trying to download Restricted Content.", MsgBoxStyle
return;
}

The reason your message box never shows up is because it is throwing an exception as soon as you call that line in bold. Execution never makes it to the if statement.

Hope this helps.


----------

