Solved

Finding missing filename on "File Does Not Exist" exception

Posted on 2011-03-02
17
1,553 Views
Last Modified: 2012-05-11
I catch the exception in the Global.asax.Application_Error() function, and extract the
exception, log it and redirect to the proper page thusly: (I use MVC)

        protected void Application_Error(object sender, EventArgs e)
        {
                Exception exception = Server.GetLastError();
                LogException(exception);
                Response.Clear();
                Server.ClearError();  
                Response.Redirect(String.Format("/Error/Generic/?outputStr={0}", exception.Message));
        }

BUT I CAN NOT FIND THE FILENAME that's missing.

I have browsed Google and found this post, which was never answered completely.
http://www.codeguru.com/forum/showthread.php?t=508023

I do not want to install some program to find the filename, as was suggested. It's in either the exceptionor Server, since I saw it once while debugging. But I was to grab the filename and log that to the exception log file.

Can anyone tell me how to find it?

Thanks,
newbieweb
0
Comment
Question by:newbieweb
  • 10
  • 7
17 Comments
 

Author Comment

by:newbieweb
ID: 35018235
I tried this without success:

FileNotFoundException fnfException = exception as FileNotFoundException;
and
FileNotFoundException fnfException = (FileNotFoundException)ex;

so I could do this:
string fileNameStr = fnfException.FileName;

but got an exception in both cases when I tried to typecast to FileNotFoundException
 
Is there a proper way to do this?

newbieweb
0
 

Author Comment

by:newbieweb
ID: 35018265
and the InnerException is null.

I think I got the new exception when I ran this line with a null fnfException, not when I typecasted it, as I has said on my previous post.

string fileNameStr = fnfException.FileName;
0
 
LVL 23

Assisted Solution

by:wdosanjos
wdosanjos earned 500 total points
ID: 35018361
Try this:

string filename = null;

for (var ex = exception; ex != null; ex = e.InnerException)
{
	FileNotFoundException fex = ex as FileNotFoundException;
	if (fex != null)
	{
		filename = fex.FileName;
		break;
	}
}

Open in new window

0
 
LVL 23

Assisted Solution

by:wdosanjos
wdosanjos earned 500 total points
ID: 35018379
Sorry, I had a typo:

string filename = null;

for (var ex = exception; ex != null; ex = ex.InnerException)
{
    FileNotFoundException fex = ex as FileNotFoundException;
    if (fex != null)
    {
        filename = fex.FileName;
        break;
    }
}

Open in new window

0
 

Author Comment

by:newbieweb
ID: 35018960
Your code works fine, but the value for fex is still null. So the filename remains null. This must not be a FileNotFoundException.
0
 

Author Comment

by:newbieweb
ID: 35019079
In the code I'm using to throw the exception, it's JQuery looking for the file (which I intentionally renamed) and the Exception gets pulled out in the Application_Error() function via the following call:

Exception exception = Server.GetLastError();
0
 
LVL 23

Assisted Solution

by:wdosanjos
wdosanjos earned 500 total points
ID: 35019124
The code goes through the exception stack searching for a FileNotFoundException.  If after the loop filename is still null, it means that there is no FileNotFoundException on the exception stack.  Can you post the exception.ToString() value?
0
 

Author Comment

by:newbieweb
ID: 35019306
I added this line:
string expStr = exception.ToString();

but on the next line, I see nothing whenI put the mouse of expStr. When I put the mouse over exceptionI see

{"File does not exist."}

That tells me the expStr is null or (more likely) undefined.
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

Author Comment

by:newbieweb
ID: 35019359
We also have these available:

object sender, EventArgs e

those are the inputs into Global.asax.Application_Error(), as you can see in the original post.

newbieweb
0
 
LVL 23

Assisted Solution

by:wdosanjos
wdosanjos earned 500 total points
ID: 35019371
If you are not using 'expStr' in any other line, the compiler might optimize the code and remove the variable and the assignment.  Please the check the content of exception.ToString() under 'Quick Watch'.  If that still returns null, then check exception.GetType().Name and exception.StackTrace.
0
 

Author Comment

by:newbieweb
ID: 35019423
Undefined. See attached...
Exception.PNG
0
 
LVL 23

Assisted Solution

by:wdosanjos
wdosanjos earned 500 total points
ID: 35019742
Please add the line #2 below and check if you can see expStr's value.

string expStr = exception.ToString();
if (expStr != null) Console.WriteLine(expStr);

Open in new window

0
 

Author Comment

by:newbieweb
ID: 35020339
I got a value:

System.Web.HttpException (0x80004005): File does not exist.
   at System.Web.StaticFileHandler.GetFileInfo(String virtualPathWithPathInfo, String physicalPath, HttpResponse response)
   at System.Web.StaticFileHandler.ProcessRequestInternal(HttpContext context, String overrideVirtualPath)
   at System.Web.DefaultHttpHandler.BeginProcessRequest(HttpContext context, AsyncCallback callback, Object state)
   at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
   at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)
0
 
LVL 23

Assisted Solution

by:wdosanjos
wdosanjos earned 500 total points
ID: 35020909
I found this post addressing to your problem.  The 'File does not exist' exception is related to a missing resource (image, css, js).

     http://dotbert.loedeman.nl/httpexception-file-does-not-exist

Try changing Application_Error as follows:

protected void Application_Error(object sender, EventArgs e)
{
    HttpApplication app = sender as HttpApplication;
    Exception exception = Server.GetLastError();
    if (app != null) exception.Data.Add("Url", app.Context.Request.Url);
    LogException(exception);
    Response.Clear();
    Server.ClearError();  
    Response.Redirect(String.Format("/Error/Generic/?outputStr={0}", exception.Message));
}

Open in new window


Then your LogException could be something like this:

private void LogException(Exception exception)
{
    if (exception != null)
    {
        string url = (exception.Data.Contains("Url")) 
                   ? exception.Data["Url"].ToString() 
                   : "";
        // url now contains the URL that caused the exception
        // most likely it points to the missing resource file

Open in new window

0
 

Author Comment

by:newbieweb
ID: 35021121
This does get added:

exception.Data.Add("Url", app.Context.Request.Url);

I broke this up to see exactly what wass happening:
string url = null;
if ( exception.Data.Contains("Url"))
{
     url = exception.Data["Url"].ToString();
}

but the value for url is not defined, like outputStr was before.
0
 
LVL 23

Accepted Solution

by:
wdosanjos earned 500 total points
ID: 35021437
You need to use 'url' in another line of code.  You can try this:

string url = null;
if ( exception.Data.Contains("Url"))
{
     url = exception.Data["Url"].ToString();
     Console.WriteLine(url);
}



0
 

Author Closing Comment

by:newbieweb
ID: 35026888
THANKS! THIS WORKS!!!
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

919 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now