views:

154

answers:

9

In Asp.net (c#),i'm not able to catch exception(FileNotFoundException) properly... i don't know the reason..Actually File s not there..But catch statement fails to catch the exception.. here is the code..

try
{
System.Drawing.Image imgg1 = System.Drawing.Image.FromFile(Server.MapPath("").ToString() + "\\images\\img1.jpg");
}
catch (FileNotFoundException)
{
    Response.Write("<script>alert('Please Select and upload Student's Photo');</script>");
}
+6  A: 

you can find out what type is being thrown

try
{
System.Drawing.Image imgg1 = System.Drawing.Image.FromFile(Server.MapPath("").ToString() + "\\images\\img1.jpg");
 }
 catch (FileNotFoundException)
{
 Response.Write("<script>alert('Please Select and upload Student's Photo');</script>");
  }
catch(Exception ex)
{
   Response.Write("Ex: " + ex.GetType().ToString());
}
Glennular
FileNotFoundException is thrown..
Anand
but does it catch properly in the suplied code?
Gabriel Guimarães
No.. that s the problem...
Anand
it is just ignoring the code... nothing more than that..
Anand
can you rebuild the code (Clean + Build) ?
Gabriel Guimarães
Perhaps you have a name clash??
Henri
thanks alot... its working...
Anand
+1  A: 

The exception thrown is not of type FileNotFoundException, try catching Exception instead and see if it works

Henri
that too not working ??!!
Anand
thanks alot... its working...
Anand
+3  A: 

Are you sure that's the exception your getting?

You should try to replace the FileNotFoundException to just Exception and check what exception is being trown.

EDIT: Q1: In the debug mode, is the code actually entering the catch session?

Could you rebuild (Ctrl+Shift+B in Visual Studio) that code?

Your actually writing a code that will fail there's an ending quote in here:

alert('Please Select and upload Student's Photo');

See in the sintax highlighter replace for this

alert('Please Select and upload Student\'s Photo');
Gabriel Guimarães
thanks alot... its working...
Anand
+1 Good catch on the quote.
Nelson
Not that Hunter, I saw that on @josephj1989 answer, and after voting him up for noticing that I edited my answer to also contain that part of the solution. Sorry to disapoint you, but I also voted you up afterward
Gabriel Guimarães
+1  A: 

Try stepping through your code in the debugger and see if the exception is truly not being caught. It may also help to include a specific variable to hold your FileNotFoundException, and to include a fallback catch of a general exception, like so:

try
{
    System.Drawing.Image imgg1 = System.Drawing.Image.FromFile(Server.MapPath("").ToString() + "\\images\\img1.jpg");
}
catch (FileNotFoundException fnfe)
{
   Response.Write("<script>alert('Please Select and upload Student's Photo');</script>");
}
catch (Exception ex)
{
    // do something with the exception
}
Tim S. Van Haren
catch section is not working ?
Anand
@Anard: If catch is not working at all why did you tell Glennular that the output was FileNotFoundException? Are you sure it's catch that's broken and not response.write?
diadem
thanks alot... its working...
Anand
+2  A: 

Check if it exists rather than catch that exception.

string path = Server.MapPath("~/images/img1.jpg");
if (System.IO.File.Exists(path))
{
    System.Drawing.Image imgg1 = System.Drawing.Image.FromFile(path);
}
else
{
    Page.ClientScript.RegisterStartupScript(Page.GetType(), "notfound", "alert(\"Please Select and upload Student's Photo\");", true);
}

You are also escaping your javascript message too early

'Please Select and upload Student's Photo'
hunter
thanks alot... its working...
Anand
A: 

Your exception is being thrown, but you aren't seeing your alert because you're not writing out JavaScript. Try this:

try
{
    System.Drawing.Image imgg1 = System.Drawing.Image.FromFile(Server.MapPath("").ToString() + @"\images\img1.jpg");
}
catch (FileNotFoundException)
{
    Page.RegisterClientScriptBlock("myScript", "<script language=javascript>alert('Please Select and upload Student's Photo');</script");
}
Ed B
LoL, if that's it. Anand should have told us that the Exception was being caught, but nothing would appear on the screen after the code is executed.
Gabriel Guimarães
Exception was not caught!!!
Anand
I ran his code..and that's what happened. Actually "alert('Please Select and upload Student's Photo'); " is displayed as HTML on the screen..maybe he didn't notice it because he was expecting a popup.
Ed B
+1  A: 

If (in the original example) you are trying to write a javascript alert out to the page you have to surround your alert() it with <script></script> tags.

BUT why are you using try-catch blocks like that when you could use System.IO.File.Exists(path), and an error label ?

using System.IO;
using System.Drawing;
...

String filePath = Server.MapPath("").ToString() + "\images\img1.jpg";
if(File.Exists(filePath))
{
    Image imgg1 = Image.FromFile(filePath);
}
else
{
    lblError.Text = "Please upload a picture for this student";
    lblError.Visible = true;
}
Alex Moore
+3  A: 

Your javascript quoted text is not balanced try

  alert('please upload student\'s photo');
josephj1989
+1  A: 

The problem is not related to the catch block. It's the way your using C# to create the JavaScript. Response.Write will pile the output prior to the rendering of the page. So it wont be recognized by the browser. Do this instead.

catch (FileNotFoundException)
{
   String csname1 = "Popup";

   if (!IsClientScriptBlockRegistered(csname1))
   {
      String cstext1 = "<script type=\"text/javascript\">" + "alert('Please Select and upload Student\\'s Photo');</" + "script>";
      RegisterStartupScript(csname1, cstext1);
   }
}

If you still don't believe me just do this to prove it to yourself.

catch(FileNotFoundException)
{
  Response.Write("its working")
}

And don't just look at the rendered page which is going to be browser dependant, right click and view source so you can see what's really going on.

P.Brian.Mackey