473,387 Members | 1,532 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

throwing custom message?

How can I catch an error and throw the exception message plus some custom text?
For example, I'd like to do something like this:
try
{
//generate error
}
catch
{
//throw ex + " my custom message";
// OR
//throw ex.Message + " my custom message";
}

How is this possible without writing my own custom exception class?

Thanks.
Feb 7 '07 #1
3 20214
generally you would wrap it in an applicationException

eg

try
{
}
catch(Exception ex)
{
throw new ApplicationException("your message here", ex);
}

dont forget to include the original exception as the second parameter - this
way u can look at the original exception later when u handle this.
Feb 8 '07 #2
>
How can I catch an error and throw the exception message plus some custom text?
<Snip>

VMI,

You can throw an exception with a custom error message without writing a
custom exception class. Inside your catch block, just throw the most
appropriate pre-defined exception type with your modified error message.

Note, it is generally considered best practice to throw a fairly specific
exception instead of the generic System.Exception. Let's say if you have a
stub method that is not supported yet, you should throw a
NotSupportedException. Or if you are validating inputs inside a function
call you might throw a InvalidArgument exception. Otherwise you might throw
an ApplicationException.

Here is an example.

sbyte i = 127;
try
{
// Generates an overflow error. Signed byte can only hold
values from -128 to 127..
checked {
i = (sbyte) (i+1);
}
}
catch (Exception ex)
{
// Throw an exception with a modified error message.
throw new ApplicationException("Something went wrong here: " +
ex.Message);
}

Useful links for exception handling guidelines:
http://www.codeproject.com/dotnet/ex...nceperthread12

http://msdn2.microsoft.com/en-us/lib...87(VS.71).aspx

Hope this helps,
Jason Vermillion

Feb 8 '07 #3
I would prefer the way XOR offered.
By Using "throw new ApplicationException("your message here", ex);" to
throw an Exception with a CustomText you won't loose the original
Exception, because it is kept in the InnerException Property

Feb 8 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Thomas Koch | last post by:
Hi there - I have created my own implementation of System.Configuration.IConfigurationSectionHandler in order to have a special configuration section in an App.Config file that configures my...
40
by: Kevin Yu | last post by:
is it a bad programming design to throw exception in the try block then catch it??
5
by: KJ | last post by:
This is kind of hard to explain but I have a The controls are created with CreateChildControls(). Now let say I click a button and an error occurs in the control. If I throw it up it goes back...
0
by: Eustice Scrubb | last post by:
In line coding problem. Here's my code: <script language="VB" runat="server"> Dim myConnection As SqlConnection Sub Page_Load(Src As Object, e As EventArgs) ' Create a connection to the SQL...
1
by: Vinoth Kumar | last post by:
Hi All, I have a problem in throwing custom exceptions from a webservice method. The custom exception is being converted into soapexception in the webservice client. only the custom exception...
5
by: =?Utf-8?B?RGVlcGFrIFZpag==?= | last post by:
Hi, I an using Web Services as my business layer and wanted to throw custom exceptions from the web method based on some business logic. Now, when I make a custom exception class bu inheriting...
21
by: Chris M. Thomasson | last post by:
Is it every appropriate to throw in a dtor? I am thinking about a simple example of a wrapper around a POSIX file... ________________________________________________________________________ class...
3
by: Kevinst | last post by:
Hello, I have a c# com object that exposes several methods. This com object is to be used in vbscripts as well as vba scripts. In my application error handling is done by throwing different...
0
hyperpau
by: hyperpau | last post by:
Before anything else, I am not a very technical expert when it comes to VBA coding. I learned most of what I know by the excellent Access/VBA forum from bytes.com (formerly thescripts.com). Ergo, I...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.