-
[RESOLVED] HttpException not handled
I seem to be having problems loading my ErrorPage. I received the error message: "HttpException not handled". I then added a catch statement expecting a specified message to be written on my screen. Instead I again received the same message but this time my HttpException error mssage was highlighted. I would like to know the following:
1. I handled HttpExceptions so why do I still receive an error message instead of my own error message text appearing on the screen?
2. My error page is called ErrorPage.aspx and is not in any particular folder and has the same namespace as the other pages so why doesnt it work?
3. Could there be any errors in the method below which determines the error message type?
Code:
public void DisplayErrorMsg()
{
// In case of error redirect to error page
for (int i = 0; i < listError.Count; i++)
{
if (listError[i] == currentError)
{
try
{
Response.Redirect("ErrorPage.aspx", true); //ORIGINAL ERROR
}
catch (HttpException)
{
Response.Write("Fehlerseite wurde nicht gefunden."); //LATER ERROR HERE
}
catch (Exception)
{
Response.Write("Fehlerseite enthalt Fehler");
}
}
}
// Determine error message
if (currentError == "401")
{
lblErrorCode.Text = "Fehlercode: 401";
lblErrorMsg.Text = "Zugang zur Seite nicht erlaubt.";
lblErrorCode.Visible = true;
lblErrorMsg.Visible = true;
}
else if (currentError == "404")
{
lblErrorCode.Text = "Fehlercode: 404";
lblErrorMsg.Text = "Seite nicht gefunden.";
lblErrorCode.Visible = true;
lblErrorMsg.Visible = true;
}
(ETC)
else
{
lblErrorCode.Text = "Ein Fehler ist aufgetreten.";
lblErrorCode.Visible = true;
lblErrorMsg.Visible = false;
}
}
-
Re: HttpException not handled
Hi there,
Quote:
Instead I again received the same message but this time my HttpException error mssage was highlighted.
I handled HttpExceptions so why do I still receive an error message instead of my own error message text appearing on the screen?
I don't understand what you are trying to say. Is your exception handler working? Do you actually catch the exception? If so how do you handle it? If you do catch the exception you can step through the code and see what is happening.
-
Re: HttpException not handled
The problem is that I dont seem to be able to catch the HttpException...
I discovered that my ErrorPage doesnt load because the NullReferenceException isnt caught. I however did handle it in my code....
Code:
listError.Add("NullReferenceException");
(...)
else if (currentError == "NullReferenceException")
{
lblErrorCode.Text = "Fehler: NullReferenceException";
lblErrorMsg.Text = "Der Objektverweis wurde nicht auf eine Objektinstanz festgelegt.";
lblErrorCode.Visible = true;
lblErrorMsg.Visible = true;
}
else
{
lblErrorCode.Text = "Fehler!";
lblErrorMsg.Text = "Exception";
lblErrorCode.Visible = true;
lblErrorMsg.Visible = true;
}
The problem seems to be that some of my exception are not caught even though I tried to handle them.
-
Re: HttpException not handled
Where are you trying to catch the application? Is it at the page level or application level? I would suggest handling the exception at the application level. You can do that in the 'global.ascx.cs'. This link has the details and it also includes re-directing to a custom error page.
-
Re: HttpException not handled
Brilliant, now my code looks much neater! I now deal with errors both on page level (try/catch blocks) and also in application level. I still however receive an error msg saying that my NullReferenceException isnt handled and highlighting that section in my ErrorPage!
Code:
public partial class ErrorPage : System.Web.UI.Page
{
/// <summary>
/// Redirects to ErrorPage and provides corresponding error msg.
/// </summary>
protected void Page_Load(object sender, EventArgs e)
{
}
#region Display Error Msg
public void DisplayErrorMsg(string currentError)
{
// Determine Error Message
if (currentError == "401")
{
lblErrorMsg.Text = "Zugang zur Seite nicht erlaubt.";
}
else if (currentError == "404")
{
lblErrorMsg.Text = "Seite nicht gefunden.";
}
else if (currentError == "408")
{
lblErrorMsg.Text = "Sitzungsperiode fertig.";
}
else if (currentError == "HttpException")
{
lblErrorMsg.Text = "Zugang zur Seite nicht erlaubt.";
}
else if (currentError == "UnauthorizedAccessException")
{
lblErrorMsg.Text = "Zugang nicht erlaubt.";
}
else if (currentError == "IndexOutOfRangeException")
{
lblErrorMsg.Text = "Die Angegebene ID existiert nicht.";
}
else if (currentError == "NullReferenceException") // PROBLEM HERE!!!
{
lblErrorMsg.Text = "Der Objektverweis wurde nicht auf eine Objektinstanz festgelegt.";
//lblErrorMsg.Text = "Ein nicht vorhandenes Objekt wird (zu) verweisen.";
}
else if (currentError == "FileNotFoundException")
{
lblErrorMsg.Text = "Die Datei wurde nicht gefunden.";
}
else if (currentError == "IOException")
{
lblErrorMsg.Text = "Ein E/A-Fehler ist auftritten.";
}
else if (currentError == "SOAPException")
{
lblErrorMsg.Text = "Ein Fehler ist bei die Aufruf einer XML-Webdienstmethode über SOAP aufgetritten.";
}
else if (currentError == "HttpException")
{
lblErrorMsg.Text = "Ein Fehler ist bei die Aufruf einer Seite aufgetritten.";
}
else
{
lblErrorMsg.Text = "Ein Fehler ist aufgetreten.";
lblErrorCode.Visible = false;
}
// Set error code and msg
lblErrorCode.Text = "Fehler: " + currentError;
lblErrorCode.Visible = true;
lblErrorMsg.Visible = true;
try
{
// Redirect to error page:
Response.Redirect("ErrorPage.aspx", true);
}
catch (Exception)
{
Response.Write("Ein Fehler ist bei die bei Aufruf der Fehlerseite aufgetritten.");
}
}
-
Re: HttpException not handled
Ive discovered the error. I am calling up the method DisplayMessage() on the ErrorPage but this is only loading the C# code behind the ErrorPage.aspx and not the actual .aspx page containing my web controls. When debugging, lblErrorMsg returns value null! How do I ensure an instance of the actual page is also created? I cant properly fiddle around with the code because I call up an instance of the ErrorPage exactly 234 times!!
This is how I currently call up the ErrorPage, and it's appropriate error message, from other classes:
Code:
Azubiportal.ErrorPage errorPage = new ErrorPage();
errorPage.DisplayErrorMsg("IndexOutOfRangeException");
-
Re: HttpException not handled
I don't think that is the best to do it. You should redirect the ErrorPage and pass in any arguments through the query string. What you are doing is creating an instance of the class. The web page has to loaded and go through the full page life cycle.
Code:
// Define a class to hide the implementation details
// of the error handling. This way you will be able to change it
// in the future without changing a lot of code.
using System.Web;
public class ErrorUtility
{
public static void HandleError(string errorMessage)
{
Server.Transfer(String.Format("ErrorPage.aspx?ErrorMessage={0}", errorMessage));
}
}
// You will have to change the class to
// deal with the query string.
public class ErrorPage : Page
{
// This is one way of doing it.
protected void Page_PreRender(object sender, EventArgs e)
{
// Call the method that is already there to output the message.
DisplayErrorMsg(QueryString["ErrorMessage"]);
}
}
On another note...if the DisplayErrorMsg is used 234 times I think it would be a good idea to review how you are doing things. Unless you have 234 different web pages it shouldn't be called that many times...
-
Re: HttpException not handled
While waiting for a reply to my post I changed my code such that my try/catch blocks now only contain one Exception Handler (decreasing my amount of code enormously). Instead of calling the DisplayErrorMsg method in the catch block I now simply redirect to the ErrorPage. The ErrorPage's Page_Load method creates a new instance of a C# class called ErrorClass and calls it's DisplayErrorMsg method with the parameter "sCurrentErr". This method is very similar to yours with the exception that instead of passing the error as a query string (good idea!), I call the latest Server error. Your method seems better than mine and I will try to implement it but I am still interested in knowing why after my code STILL doesnt work (Error: error loading page)!!
This is what my ErrorPage looks like:
Code:
public ErrorClass errorClass = new ErrorClass();
public string sCurrentErr;
public string sMsgErr;
public void Page_Load(object sender, EventArgs e)
{
// Return last error
sCurrentErr = Convert.ToString(Server.GetLastError());
// Determine error msg
sMsgErr = errorClass.GetErrorMsg(sCurrentErr);
// Display error msg on .aspx page
DisplayErrorMsg();
}
protected void DisplayErrorMsg()
{
// Set error code and msg
lblErrorCode.Text = "Fehler: " + sCurrentErr;
lblErrorMsg.Text = sMsgErr;
lblErrorCode.Visible = true;
lblErrorMsg.Visible = true;
}
And this is what my ErrorClass class looks like:
Code:
public class ErrorClass
{
/// <summary>
/// Provides corresponding error msg
/// to be used in ErrorPage
/// </summary>
private string sMsgErr;
public ErrorClass()
{
}
#region Get Error Msg
public string GetErrorMsg(string currentError)
{
// Determine Error Message
if (currentError == "401")
{
sMsgErr = "Zugang zur Seite nicht erlaubt.";
}
else if (currentError == "404")
{
sMsgErr = "Seite nicht gefunden.";
}
else if (currentError == "408")
{
sMsgErr = "Sitzungsperiode fertig.";
}
else if (currentError == "HttpException")
{
sMsgErr = "Zugang zur Seite nicht erlaubt.";
}
else if (currentError == "UnauthorizedAccessException")
{
sMsgErr = "Zugang nicht erlaubt.";
}
else if (currentError == "IndexOutOfRangeException")
{
sMsgErr = "Die Angegebene ID existiert nicht.";
}
else if (currentError == "NullReferenceException")
{
sMsgErr = "Der Objektverweis wurde nicht auf eine Objektinstanz festgelegt.";
//errorMsg = "Ein nicht vorhandenes Objekt wird verweisen.";
}
else if (currentError == "FileNotFoundException")
{
sMsgErr = "Die Datei wurde nicht gefunden.";
}
else if (currentError == "IOException")
{
sMsgErr = "Ein E/A-Fehler ist auftritten.";
}
else if (currentError == "SOAPException")
{
sMsgErr = "Ein Fehler ist bei die Aufruf einer XML-Webdienstmethode über SOAP aufgetritten.";
}
else if (currentError == "HttpException")
{
sMsgErr = "Ein Fehler ist bei die Aufruf einer Seite aufgetritten.";
}
else
{
sMsgErr = "Ein Fehler ist aufgetreten.";
}
return sMsgErr;
}
#endregion
-
Re: HttpException not handled
Another thing, how did you make Server.Transfer work in a static method? I have never been able to get either Response.Redirect or Server.Transfer to work in a static method...
-
Re: HttpException not handled
You have to give more details about the error you are experiencing now. There is likely to be an exception associated with it. You may want to run it in debug mode to get more information. You can also check the Event Log you may get ASP.NET error details there. Without the detail there's not much anyone can do with 'error loading page!' :)
-
Re: HttpException not handled
Quote:
Originally Posted by
stephsh
Another thing, how did you make Server.Transfer work in a static method? I have never been able to get either Response.Redirect or Server.Transfer to work in a static method...
Good question. The simple answer is I didn't. I just typed the code in codeguru. You are right. 'Server' is an instance property of the 'Page'. This would have worked...
Code:
// Define a class to hide the implementation details
// of the error handling. This way you will be able to change it
// in the future without changing a lot of code.
using System.Web;
public class ErrorUtility
{
public static void HandleError(string errorMessage)
{
HttpContext.Current.Server.Transfer(String.Format("ErrorPage.aspx?ErrorMessage={0}", errorMessage));
}
}
Here I'm using the Server property of the current http context.
-
Re: HttpException not handled
my bad :P Basically I am loading a certain page (other than the ErrorPage) but due to errors on that page it redirects me to the ErrorPage. This then calls the Server.GetLastError method but this returns null... The rest of the methods can therefore not be executed properly...
-
Re: HttpException not handled
I tried your method but when I pass the data as query string I receive the error that there are line breaks in the actual exception string! Here is an example:
Code:
protected void GetTrainee()
{
try
{
// Try accessing the connection class to get the ID of the current trainee
trainee = WebServiceConnection.webService.getTrainee(Azubiportal.system.BaseWebPage.selectedTraineeID);
}
catch (Exception excep)
{
Response.Redirect("ErrorPage.aspx?qstr=" + excep);
// Method 2:
// string temp = Convert.ToString(excep);
// Response.Redirect("ErrorPage.aspx?qstr=" + temp);
// Method 3: excep.Message instead of temp
}
}
-
Re: HttpException not handled
Quote:
Originally Posted by
stephsh
I tried your method but when I pass the data as query string I receive the error that there are line breaks in the actual exception string!
Good point. I hadn't thought about that. You will probably have to resort to calling GetLastError within the error page or storing the message in a session variable.
-
Re: HttpException not handled
The problem with Server.GetLastError() is that it's value is reset to null everytime you leave a particular page. To tackle this I choose to save the value of Server.GetLastError() as a session variable called "sLatestErr". It however STILL has the value null!
This is what my session data looks like:
Code:
public class SessionData
{
/// <summary>
/// SessionData object contains general information
/// with regards to the user and the current status
/// that can be used throughout application.
/// </summary>
#region Variables
public Configuration mConfiguration;
public BerechtTransactionHandler mBerechtTransactionHandler;
public string sSessionID;
//public string sUserType;
public string sAdvisorName;
public string sAdvisorEmail;
public DateTime dtCurrentDate;
public string sLatestErr; //= "";
Azubiportal.ErrorPage errorPage = new Azubiportal.ErrorPage();
#endregion
public SessionData()
{
}
public void setConfiguration()
{
try
{
dtCurrentDate = DateTime.Now;
sSessionID = mBerechtTransactionHandler.mSessionID;
sAdvisorName = mConfiguration.msBerechtUserName;
sAdvisorEmail = mConfiguration.msEMailSender;
}
catch (Exception e)
{
Console.WriteLine(e);
}
}
And this is an example of how Ive used the session variable sLatestErr:
Code:
protected void GetTrainee()
{
try
{
// Try accessing the connection class to get the ID of the current trainee
trainee = WebServiceConnection.webService.getTrainee(Azubiportal.system.BaseWebPage.selectedTraineeID);
}
catch (Exception)
{
mSessionData.sLatestErr = Convert.ToString(Server.GetLastError());
Response.Redirect("ErrorPage.aspx", true);
}
}
-
Re: HttpException not handled
Debugging showed me that the problem was that mSessionData is null so therefore mSessionData.sLatestErr doesnt work. To tackle this I created a new instance of SessionData on each page, i.e.:
Code:
public SessionData mSessionData = new SessionData();
I also gave sLatestError a test value. Now in my SessionData class I received a different error: StackOverFlowException! This is present here:
Code:
public ErrorPage errorPage = new ErrorPage();
In other words the ErrorPage is stuck in some loop, presumably endlessly creating new instances of itself.I havent been able to figure out why since the ErrorPage seems fine to me but I have realized another problem! At the moment every page creates a new instance of ErrorPage. Doesnt this mean that every page automatically loads ErrorPage.aspx even if there is no error??
-
Re: HttpException not handled
That would suggest that Server.GetLastError() gave you back a null. You need to run it in debug mode and you'll have access to the original error or exception. You can put a breakpoint in the line of code where you 'GetLastError'. You can check through the call stack, you can check the local variables and you're are likely to have a better picture of what is happening. Have you checked the event log yet?
-
Re: HttpException not handled
I managed to figure out why my ErrorPage didnt work! The ErrorPage was derived from a Master page which in turn directed one to the ErrorPage in the case of errors. When I tried loading a certain page containing errors it directed me to the ErrorPage which directed me to the Master page and then back to the ErrorPage (cas there too there were errors!) and so forth in a never ending loop! I now changed the ErrorPage to a single webform and it appears perfectly :)
I still dont know why sLatestError returns null but I will follow your advice tomorrow morning and see if I get any further with that!
-
Re: HttpException not handled
I am using Server.GetLastError() to determine the error that has been displayed. I have created a list which gives my own error messages for the most common errors in my app as follows:
Code:
if (currentError == "HttpException")
{
sMsgErr = "Zugang zur Seite nicht erlaubt.";
}
else if (currentError == "UnauthorizedAccessException")
{
sMsgErr = "Zugang nicht erlaubt.";
}
else if (currentError == "IndexOutOfRangeException")
{
sMsgErr = "Die Angegebene ID existiert nicht.";
}
else
{
sMsgErr = "Ein Fehler ist aufgetreten.";
}
In my original code I tackle many more errors but I still keep receiving the last error message. Do you think it is possible that the exception that is returned with Server.GetLastError() is much longer than just the exception name (ex: "IndexOutOfRangeException") and that the corresponding error msg is therefore not being found? How would I tackle this apart from splitting ever error that comes in to see if it contains any of the following words (since that seems like a very long method)?
-
Re: HttpException not handled
Quote:
Originally Posted by stephsh
In my original code I tackle many more errors but I still keep receiving the last error message. Do you think it is possible that the exception that is returned with Server.GetLastError() is much longer than just the exception name (ex: "IndexOutOfRangeException") and that the corresponding error msg is therefore not being found? How would I tackle this apart from splitting ever error that comes in to see if it contains any of the following words (since that seems like a very long method)?
Reply With Quote
Well you don't have to have a specific message for every possible type of error. Try to give specific message for errors that user can do something about or even if they can't then at least they get a message that is useful. For example an "IndexOutofRangeException" normally means a programming error so this is not useful at all. The user cannot do anything about it. The "UnauthorizedAccessException" is more useful as the user can speak to someone to grant them the right level of access if they need to. By the way why are you comparing against the exception as strings? If an exception was raised it has a type e.g. IndexOutOfRangeException. i don't understand why you are looking "IndexOutOfRangeException".
-
Re: HttpException not handled
Good point, I will restrict the error messages to a couple of common ones such as "UnauthorizedAccessException" and "FileNotFoundException".
The problem I am having is that Server.GetLastError() returns null! I tried saving the value of Server.GetLastError() as a session variable but null was still returned. I also tried handling the error in the Application_Error section of Global.asax but no luck with that either. Are there any other methods one can use to get the current error?
-
Re: HttpException not handled
Have you actually tried to run your application in debug mode? It will pinpoint exactly where the error is. If Server.GetLastError() returned null it suggest no error was detected or something is interefering with the way the error is handled or reported. If you would run your application in debug mode you will be able to find out exactly where the error is taking place and how it is being handled.
-
Re: HttpException not handled
Yep, I have spent ages debugging but didnt get much out of it. Server.GetLastError() returned an empty string (since I declared the variable as below) and therefore my other methods returned null.
Code:
public string sLatestErr = String.Empty;
-
Re: HttpException not handled
Quote:
Originally Posted by
stephsh
Yep, I have spent ages debugging but didnt get much out of it. Server.GetLastError() returned an empty string (since I declared the variable as below) and therefore my other methods returned null.
Code:
public string sLatestErr = String.Empty;
Ok. Did you debug from the very beginning? Was there any error to begin with? If you call GetLastError when there was was no error (exception) you are supposed to get null.
-
Re: HttpException not handled
Yes, I did debug it from the beginning. In the code below for example "e" brings up an error msg stating that the directory is not found. sLatestErr however only returns "", i.e. an empty string. When I mouse over "Server" the property "error" is null. I havent however managed to scroll over GetLastError to return it's properties.
Code:
public void CreatePDF()
{
doc = new Document(PageSize.A4, 80, 50, 30, 65);
string PDFpath = Server.MapPath("~/pdf/Beurteilungsbogen.pdf");
try
{
stream = new FileStream(PDFpath, FileMode.Create);
writer = PdfWriter.GetInstance(doc, stream);
doc.Open();
FormatPDF();
// First read the PDF before stamping it
PdfReader pdfReader = new PdfReader(Request.MapPath("~/pdf/Beurteilungsbogen.pdf"));
int pageNumber = 1;
Rectangle size = pdfReader.GetPageSizeWithRotation(pageNumber);
PdfStamper pdfStamper = new PdfStamper(pdfReader, Response.OutputStream);
pdfStamper.FormFlattening = true;
pdfStamper.Close();
}
catch (Exception e)
{
SData.sLatestErr = Convert.ToString(Server.GetLastError());
string test = SData.sLatestErr; // Returns ""
Response.Redirect("Error.aspx", true);
}
-
Re: HttpException not handled
Quote:
Originally Posted by
stephsh
Yes, I did debug it from the beginning. In the code below for example "e" brings up an error msg stating that the directory is not found. sLatestErr however only returns "", i.e. an empty string. When I mouse over "Server" the property "error" is null. I havent however managed to scroll over GetLastError to return it's properties.
Why do you need to call GetLastError when you have the details in the exception?
-
Re: HttpException not handled
What I wanted to do is return the exception type. Using if statements I then wanted to see if the exception type is equal to an exception type listed in my array. If so then the corresponding error message should be displayed. If not then a standard error message can be displayed. That way the user can learn what type of exception it is but doesnt have to read through a long complicated error message. I thought that GetLastError returns the exception type?
If this is not possible, then I will simply display the current error message using:
Code:
catch (Exception e)
{
SData.sLatestErr = Convert.ToString(e.Message);
Response.Redirect("Error.aspx", true);
}
-
Re: HttpException not handled
The best thing is to handle the exception at the point where it occurs and setup the different error messages there. The exception can tell you what type it is. Better still catch the specific exceptions you are prepared to handle...
Code:
try
{
}
catch (FileNotFoundException) // Catch each specific exception that might be useful to handle separately. Eg. giving useful error message.
{
// Configure the correct error message here.
}
catch (IndexOutOfRangeException) // Catch each specific exception that might be useful to handle separately. Eg. giving useful error message.
{
// Configure the correct error message here.
}
catch (SqlException) // Catch each specific exception that might be useful to handle separately. Eg. giving useful error message.
{
// Configure the correct error message here.
}
catch (Exception) // Use this only for general exceptions
{
// Configure the generic error message here.
}
Doing the look up against an array is an interesting thing to do but does not seem necessary here. If you can find a good enough reason to do that at least use an array of types and not an array of strings. But even as I'm typing I hear a loud voice in my head saying this not the best way to deal with the problem. Handle the exception where it occurs. All the error page should be concerned with doing is display a message. It doesn't have to be the one to decide which message to use. Do that decision at the point where the exceptions are caught.
-
Re: HttpException not handled
My application is quite big so I have at least 8 try/catch blocks per page, often much more. Using all these catch statements would dramatically increase the code. I therefore want to handle the errors in a seperate class.
How would one code this with an array of types? I am having trouble getting the actual error exception type as opposed to the exception message.
-
Re: HttpException not handled
You don't need to have exception handling at the page level. General exceptions should be handled globally anyway since the handling would be the same regardless of the page. I think I've mentioned this before that there are certain exceptions that do not need to be handled in several different places. I'm not sure what you've got in your pages but things like HttpException, UnauthorisedAccessException, SqlException you can handle in one place or a couple of places. You don't need to repeat the handling in every page. I realise your application is big. This is even more reason to re-design your exception handling otherwise it will become very difficult to maintain your application.
Here's a possibility for storing the target exception types in an array.
Code:
private Type[] _arrayOfTypes = new Type[]
{
typeof(IndexOutOfRangeException),
typeof(HttpException)
};
-
Re: HttpException not handled
Interesting, I have never seen that type of structure before. Could you explain it please?
-
Re: HttpException not handled
Code:
private Type[] _arrayOfTypes = new Type[]
{
typeof(IndexOutOfRangeException),
typeof(HttpException)
};
Type is just a class in the .NET framework...
What's happening here is that I'm using the collection initializer construct from C#3.0/.NET 3.5. It enables me to create the array with the elements I need in a single statement.
The verbose alternative would have been something like this...
Code:
Type[] _arrayOfTypes = new Type[2];
_arrayOfTypes[0] = typeof(IndexOutOfRangeException);
_arrayOfTypes[1] = typeof(HttpException);
Well in terms of lines of code it is pretty much the same. I just like to use the new language features when I have the opportunity.
Edit: I would like to put a disclaimer here to say that I'm aware that C#3.0 isn't exactly new...:)
-
Re: HttpException not handled
That makes more sense to me, thanks! I rewrote my ErrorClass using the code you gave me and I took out the error msgs which dont really concern the user. The question is how do I retrieve the error message type so that I can check it against the array?
Code:
// Create an array of types
protected Type[] _arrayOfTypes = new Type[]
{
typeof(HttpException),
typeof(UnauthorizedAccessException),
typeof(FileNotFoundException),
typeof(IOException),
typeof(IndexOutOfRangeException),
typeof(NullReferenceException)
};
// This is a different way for doing:
#region Old method
//protected void GetArray()
//{
// _arrayOfTypes[0] = typeof(IndexOutOfRangeException);
// _arrayOfTypes[1] = typeof(HttpException);
//}
// where
//protected Type[] _arrayOfTypes = new Type[2];
#endregion
public string GetErrorMsg(Type sError)
{
if (sError == _arrayOfTypes[0])
{
sMsgErr = "Zugang zur Seite nicht erlaubt.";
}
else if (sError == _arrayOfTypes[1])
{
sMsgErr = "Zugang nicht erlaubt.";
}
else if (sError == _arrayOfTypes[2])
{
sMsgErr = "Die Datei wurde nicht gefunden.";
}
else if (sError == _arrayOfTypes[3])
{
sMsgErr = "Ein E/A-Fehler ist auftritten.";
}
else if (sError == _arrayOfTypes[4])
{
sMsgErr = "Die Angegebene ID existiert nicht.";
}
else
{
sMsgErr = "Ein Fehler ist aufgetreten.";
}
return sMsgErr;
}
-
Re: HttpException not handled
Sorry I meant:
Code:
// Create an array of types
protected Type[] _arrayOfTypes = new Type[]
{
typeof(HttpException),
typeof(UnauthorizedAccessException),
typeof(FileNotFoundException),
typeof(IOException),
typeof(IndexOutOfRangeException)
};
public string GetErrorMsg(Type sError)
{
if (sError == _arrayOfTypes[0])
{
sMsgErr = "Zugang zur Seite nicht erlaubt.";
}
else if (sError == _arrayOfTypes[1])
{
sMsgErr = "Zugang nicht erlaubt.";
}
else if (sError == _arrayOfTypes[2])
{
sMsgErr = "Die Datei wurde nicht gefunden.";
}
else if (sError == _arrayOfTypes[3])
{
sMsgErr = "Ein E/A-Fehler ist auftritten.";
}
else if (sError == _arrayOfTypes[4])
{
sMsgErr = "Die Angegebene ID existiert nicht.";
}
else if (sError == _arrayOfTypes[5])
{
sMsgErr = "Der Objektverweis wurde nicht auf eine Objektinstanz festgelegt.";
//errorMsg = "Ein nicht vorhandenes Objekt wird verweisen.";
}
else
{
sMsgErr = "Ein Fehler ist aufgetreten.";
}
return sMsgErr;
}
-
Re: HttpException not handled
Quote:
Originally Posted by stepsh
The question is how do I retrieve the error message type so that I can check it against the array?
Code:
try
{
}
catch (Exception e)
{
ErroHandler.GetErrorMsg(e.GetType());
}
Every object in .NET framework implements a GetType method which should give you the type.
-
Re: HttpException not handled
Thanks I can now display the type of exception! I havent been able to find an exception that fits my type yet but Im sure it works :)