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 r开发者_开发知识库eason..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>");
}
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());
}
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');
Your javascript quoted text is not balanced try
alert('please upload student\'s photo');
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'
The exception thrown is not of type FileNotFoundException, try catching Exception instead and see if it works
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
}
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;
}
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.
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");
}
精彩评论