Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Finding missing filename on "File Does Not Exist" exception

Posted on 2011-03-02
17
Medium Priority
?
1,635 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 23

Assisted Solution

by:wdosanjos
wdosanjos earned 2000 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 2000 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
 

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 2000 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 2000 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 2000 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 2000 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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

721 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