-
February 10th, 2010, 07:53 AM
#31
Re: HttpException not handled
Interesting, I have never seen that type of structure before. Could you explain it please?
-
February 10th, 2010, 08:14 AM
#32
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...
Last edited by nelo; February 10th, 2010 at 08:26 AM.
-
February 10th, 2010, 09:27 AM
#33
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;
}
-
February 10th, 2010, 09:28 AM
#34
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;
}
-
February 10th, 2010, 09:32 AM
#35
Re: HttpException not handled
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.
-
February 10th, 2010, 10:08 AM
#36
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
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|