469,612 Members | 1,628 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,612 developers. It's quick & easy.

How to change to error message

ad

I use try..catch(exception e) to catch excption and show to error(e.Message)
to user.
But the error is some difficult for user to understand.
How can I change the e.Message to some custom message.
Nov 17 '05 #1
3 1486
ad,

You catch objects of type Exception, which means you handle all exceptions
in the same place. My suggestion is to filter down the exception that you
expect to be thrown by type. then in the event handlers instead
error(e.Message) you can emit most friently message.

try
{
}
catch(ExceptionType1 e1)
{
error("message1");
}
catch(ExceptionType2 e1)
{
error("message2");
}
catch(ExceptionType3 e1)
{
error("message3");
}
catch(Exception e)
{
// for unexpected exceptions you can show the original message or some
message in the line of the "unexpected error"
error(e.Message);
}

You have to always start from the more specific to the common exception
types. If you put catch(Exception e) as a first exception block it will
catch all the exception and the execution will never enter the other blocks.
HTH
Stoitcho Goutsev (100) [C# MVP]
"ad" <ad@wfes.tcc.edu.tw> wrote in message
news:%2***************@TK2MSFTNGP15.phx.gbl...

I use try..catch(exception e) to catch excption and show to
error(e.Message)
to user.
But the error is some difficult for user to understand.
How can I change the e.Message to some custom message.

Nov 17 '05 #2
You can´t change the Message property of an Exception, but you can throw a
new exception with the text that you want passing the caught exception as
inner exception:

....
catch Exception ex
{
throw new ApplicationException("my text", ex);
}

--

Best regards,

Carlos J. Quintero

MZ-Tools: Productivity add-ins for Visual Studio .NET, VB6, VB5 and VBA
You can code, design and document much faster.
Free resources for add-in developers:
http://www.mztools.com

"ad" <ad@wfes.tcc.edu.tw> escribió en el mensaje
news:%2***************@TK2MSFTNGP15.phx.gbl...

I use try..catch(exception e) to catch excption and show to
error(e.Message)
to user.
But the error is some difficult for user to understand.
How can I change the e.Message to some custom message.

Nov 17 '05 #3
ad,

I would recommend that in the case of exceptions, you do not try and
show a friendly message at all. Rather, you should be checking the
conditions in your code that you can anticipate, and then displaying error
information when those conditions are not met.

Using exceptions as a way of handling logic errors is not the best of
approaches. For example, if you are trying to open a file, you should check
to see if the file exists first before trying to open it, instead of relying
on a FileNotFoundException being thrown.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"ad" <ad@wfes.tcc.edu.tw> wrote in message
news:%2***************@TK2MSFTNGP15.phx.gbl...

I use try..catch(exception e) to catch excption and show to
error(e.Message)
to user.
But the error is some difficult for user to understand.
How can I change the e.Message to some custom message.

Nov 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by J.C. Flores | last post: by
14 posts views Thread by Mr.KisS | last post: by
1 post views Thread by Ravi | last post: by
9 posts views Thread by KDawg44 | last post: by
6 posts views Thread by AAaron123 | last post: by
reply views Thread by devrayhaan | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.