473,396 Members | 1,975 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,396 software developers and data experts.

Assertion on Type conversion

Hello,

I have problem that might be easily answered.

Consider the following scenario:

CustomType aType = null;

try
{
aType = CustomType.Parse(someString);
}
catch
{
throw new ApplicationException("wrong custom type!");
}
//continue using the aType

Now here is my question:

I want to get rid of the Try-Catch statement, how can I replace the
above statements with Debug.Assert? is it possible at all?

thanks,

Nov 17 '05 #1
5 1442
Debug.Assert would have to be called in the Parse() method. It is only useful in DEBUG builds, so it's not recommended for runtime
parameter checking for non-debug builds (live deployments, etc.)

Instead, I would recommend testing someString before attempting to parse it and throwing an ArgumentException when it can't be
parsed. This will prevent invalid use of the Parse() method at runtime in any build configuration.

Is there a reason you want to do this that someone may help you with?

--
Dave Sexton
dave@www..jwaonline..com
-----------------------------------------------------------------------
<ar****@yahoo.com> wrote in message news:11**********************@l41g2000cwc.googlegr oups.com...
Hello,

I have problem that might be easily answered.

Consider the following scenario:

CustomType aType = null;

try
{
aType = CustomType.Parse(someString);
}
catch
{
throw new ApplicationException("wrong custom type!");
}
//continue using the aType

Now here is my question:

I want to get rid of the Try-Catch statement, how can I replace the
above statements with Debug.Assert? is it possible at all?

thanks,

Nov 17 '05 #2
Dave, thanks for the reply.

1- I didn't explain my problem clearly.
2- As you suggested CustomType.Parse has its own exception
3- I am *expecting* that conversion of "someString" to CustomType is
always true.

*post-condition assertion in Design-by-Contract is what I am looking
for*
As you already mentioned all the assertions are checked during the
debug mode, so I was looking for some kind of code that help me to
eliminate the try-catch statement by using an Assert statement.

BTW, if I could kust use "as" statement I could accomplish this task.

Nov 17 '05 #3
You can't override "as" functionality, but you may override the explicit cast operator on your type:

public static explicit operator CustomType (string typeAsString)
{
// Todo: parse typeAsString into CustomType
}

I'm still not sure what you are trying to accomplish, but this may help, I guess. By the way, be careful using operator overloads
since languages like VB.NET will not support them. They will have to call the implementation-hidden method like op_Explicit, etc.

--
Dave Sexton
dave@www..jwaonline..com
-----------------------------------------------------------------------
<ar****@yahoo.com> wrote in message news:11**********************@f14g2000cwb.googlegr oups.com...
Dave, thanks for the reply.

1- I didn't explain my problem clearly.
2- As you suggested CustomType.Parse has its own exception
3- I am *expecting* that conversion of "someString" to CustomType is
always true.

*post-condition assertion in Design-by-Contract is what I am looking
for*
As you already mentioned all the assertions are checked during the
debug mode, so I was looking for some kind of code that help me to
eliminate the try-catch statement by using an Assert statement.

BTW, if I could kust use "as" statement I could accomplish this task.

Nov 17 '05 #4
I already have that, but I still need to use it in try-catch, don't I?

This is what I wanted to acheive:

http://c2.com/cgi/wiki?DesignByContract

But I can't explain it because I think I haven't grasp it properly as
well. I know that much that it tries to separate the implementation
from what we are expectnig to see on the output, the main reason for
doing that is to catch the errors before hand. As it is suggested in
the above URL I believe XP Unit-Testing can replace it.

Nov 17 '05 #5
The author's claim, from what I undestand it to be, is that methods should not require Try..Catch blocks to check conditional
requirements known to the calling method. I very much disagree.

The author states that "if a method has specified some pre-condition then the failure of that condition is the responsibility of the
client of the method". In programming terms, from my understanding of the author's statement, this means that a methods failure
depends on the caller's ability to check for conditions, and assure them to be correct, prior to the calling of the method.

This is the sole purpose of documentation :)

The author did not explain what actions the caller should take if it cannot satisfy these conditions. It seems to be assumed that
the caller will be able to gracefully handle "failure".

Quote: "The client *should* do whatever is necessary to ensure it will meet the pre-conditions. It may do runtime tests or it may
assume some condition is satisfied based on its own specification"

The problem with this is that the method may have to first check internal state before adjusting the "condition", in which case the
caller will not be able to do "whatever is necessary". Furthermore, methods and "encaspulation" teach us that reusability produces
less clutter and code-bloat. If the call-stack is bloated due to runtime-checking of "conditions", well that's just business logic
coded incorrectly. Maybe the implemenation should be thought out more effeciently.

Anyway, in .NET I've never heard of the performance of an application hinging on the size of the call stack.

It seems the author is not using Exceptions as they are intended in the .NET framework. Exceptions are meant for cases where an
application (or method) cannot gracefully handle invalid conditions. Documentation provides a means for coding against methods to
insure these conditions are met prior to calling a method. If an exception is thrown, the code should be rewritten to do
runtime-checks before calling the exception-throwing method.

This is called a bug. An overlooked exception that could be handled gracefully.

If CustomType is your object, and Parse is your method (meaning that you have the ability to change the implementation), then I
would use a Try...Catch block and throw an ArgumentException when an invalid input parameter has been passed to the method.
Implement a method like "IsValid" to do a runtime check first if your concerned about the ArgumentException being thrown. After
all, it's an "exception" to your code, something that has not been accounted for.

No user wants to see a Debug.Asser popup while running there application. This is for debug use only on a development and testing
platform.

I hope I've cleared it up (and didn't offend anyone)

GL

--
Dave Sexton
dave@www..jwaonline..com
-----------------------------------------------------------------------
<ar****@yahoo.com> wrote in message news:11**********************@o13g2000cwo.googlegr oups.com...
I already have that, but I still need to use it in try-catch, don't I?

This is what I wanted to acheive:

http://c2.com/cgi/wiki?DesignByContract

But I can't explain it because I think I haven't grasp it properly as
well. I know that much that it tries to separate the implementation
from what we are expectnig to see on the output, the main reason for
doing that is to catch the errors before hand. As it is suggested in
the above URL I believe XP Unit-Testing can replace it.

Nov 17 '05 #6

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

Similar topics

3
by: Todd Miller | last post by:
Hi, I recently discovered an assertion failure in the Python garbage collection system when scripts using our C extension (numarray) exit. The assertion is activated for Pythons configured using...
0
by: benevilent | last post by:
Hey, I'm getting an assertion error as a result of embedding python. "Modules/gcmodule.c:231: visit_decref: Assertion `gc->gc.gc_refs != 0' failed." I only get this assertion error with...
4
by: Morgan Leppink | last post by:
Hey all - We are running SQL 2000 with ALL available service packs, etc. applied. We just built a brand new database server, which has dual 2Ghz XEONs, 2GB memory, and the following disk...
7
by: Madhu Gopinathan | last post by:
Hi, I hope this is the right forum for this question. I am extending ICollection to create a Collection Type (say MyCollection) wherein I can control the types of objects being added to the...
1
by: Timur Safin | last post by:
Hi All, Sorry if it is offtopic here, I wasn't able to find any more relevant group... I'm slowly approaching AMD64 build for our product (as my personal fun project). And after I ran that...
5
by: Ron Louzon | last post by:
I have some C++ code that uses the CSingleLock( CCriticalSection *) constructor. In visual C++ 6.0, this code compiles and runs fine in both Debug and release modes. However, in Visual Studio...
2
by: Penny Balkwill via .NET 247 | last post by:
(Type your message here) I am supporting a system which uses ORACLE Forms, and we are getting an intermittent error: Assertion failed! Program: D:\Dev6i\bin\ifrun60.exe File:...
10
by: Frederick Gotham | last post by:
Irrespective of whether NDEBUG is defined, must an assertion expression always be evaluated? For instance, is the following code perfectly OK? #define NDEBUG #include <assert.h> int...
22
by: kudruu | last post by:
Hello, I am having a problem in Microsoft Visual C++ with some code I have written (I am a bit of a novice so I appologize if this is a very mundane problem). This is a segment from one of the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.