Anand
Anand

Reputation: 93

In Asp.net i'm not able to catch any exception properly?

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>");
}

Upvotes: 0

Views: 715

Answers (9)

P.Brian.Mackey
P.Brian.Mackey

Reputation: 44275

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.

Upvotes: 1

Gabriel Guimar&#227;es
Gabriel Guimar&#227;es

Reputation: 2744

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');

Upvotes: 3

josephj1989
josephj1989

Reputation: 9709

Your javascript quoted text is not balanced try

  alert('please upload student\'s photo');

Upvotes: 3

Alex Moore
Alex Moore

Reputation: 3455

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;
}

Upvotes: 1

hunter
hunter

Reputation: 63502

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'

Upvotes: 2

Ed B
Ed B

Reputation: 6054

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");
}

Upvotes: 0

Timothy S. Van Haren
Timothy S. Van Haren

Reputation: 8966

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
}

Upvotes: 1

Henri
Henri

Reputation: 5103

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

Upvotes: 1

Glennular
Glennular

Reputation: 18215

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());
}

Upvotes: 6

Related Questions