IT Community - Software Programming, Web Development and Technical Support

How to write error free programs in c and c++?

This is a discussion on How to write error free programs in c and c++? within the C and C++ Programming forums, part of the Software Development category; Hi, I have started this thread to guide you to write a error free programs in C & C++. I ...


Go Back   IT Community - Software Programming, Web Development and Technical Support > Software Development > C and C++ Programming

Register FAQ Members List Calendar Mark Forums Read
  3 links from elsewhere to this Post. Click to view. #1 (permalink)  
Old 03-23-2007, 02:04 AM
Karpagarajan Karpagarajan is offline
D-Web Analyst
 
Join Date: Mar 2007
Posts: 299
Karpagarajan is on a distinguished road
Thumbs up How to write error free programs in c and c++?

Hi,

I have started this thread to guide you to write a error free programs in C & C++. I have faced lot of problems in C. Here I have mentioned the code , do not use these kind of codes in your program. It will give the uncaught exceptional errors. In C or C++, we can easily try catch the errors and give the proper messages for the code. But in the following cases, we cannot trap the errors.

Uncaught exceptions

1. Divide by zero
a. int x = 0; 2/x
b. double x = 0.0; 2.0/x
c. int x = 0; 2%x
d. You will also see overflow and underflow occasionally
2. Stack overflow
a. Infinitely recursive function
void InfiniteRecurse( int x )
{
if ( false )
{
// terminating condition which is never met
return;
}
else
{
// recurse condition which is always met
InifiniteRecurse(x+1);
}
}

b. Infinitely recursive set of functions
Same as (a) but a set of functions are mutually recursive, so the call stack looks like a -> b -> c -> a -> b -> c -> a -> b -> c -> a -> b -> c -> ...
c. Valid recursive function but each call using too much stack space

void BigRecurse( unsigned int x )
{
int aBigArray[1000];

if( x >= 1000 )
{
return;
}
else
{
aBigArray[x] = x;
BigRecurse(x+1);
}
}
3. Out of memory; this may show up as an exception on some systems, others will just return NULL from the new or malloc (Visual C++'s C library returns NULL and does not throw an exception).
int* p = new int;
4. User or library code generated exceptions that failed to get wrapped in a try/catch. Third party code may throw exceptions under some circumstances. Your code might intentionally throw exceptions. If these miss getting caught then the exceptions will make it all the way to the top of the thread.
ret = ThisFunctionThrowsAnException();

thanks
__________________
Karpagarajan. R
Necessity is the mother of invention

Last edited by Karpagarajan : 03-23-2007 at 02:06 AM.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 03-27-2007, 04:49 PM
Karpagarajan Karpagarajan is offline
D-Web Analyst
 
Join Date: Mar 2007
Posts: 299
Karpagarajan is on a distinguished road
Thumbs up Re: How to write error free programs in c and c++?

Using C++ Exceptions
1. Usage of C++ exceptions is the preferred error-handling strategy.
2. It is preferable to use exception classes. Base libraries provide their own exceptions classes: MFC, CException, Standard C++ library exception, Compiler COM support—_com_error, and so forth. For COM errors, use the following prototype static function:
LocalErrors::ThrowError(HRESULT errorCode,LPCTSTR errorDescription, .);
LocalErrors::ThrowError(HRESULT errorCode,UINT stringResourceId, .);
Win32 error codes can be transformed to HRESULT by using the HRESULT_FROM_WIN32 macro. Preferable exception classes are from the library used. In cases of using several libraries in one project, the preferable ones are MFC and Compiler COM support exception classes for Windows-only projects. For cross platform projects, use the Standard C++ library exception classes, its derivations, and simple types.
3. A function exception specification. To create error-proof code and to avoid unhandled exceptions, use explicit function exception specification. It helps to understand which exceptions can be thrown by the function.
void AnyFunction() throw(_com_error, CException);
void FunctionWithoutExceptions() throw();
4. Handling exceptions. To handle exceptions, use try/catch statements.
try
{
// Statements that can throw an exception.
AnyFunction();
}
catch(_com_error &e)
{
}
catch(CException *ex)
{
ex->Delete();
}
catch(.)
{
// Unhandled exceptions catches here.
}
5. Using old-style error handling. In a few circumstances, using exceptions is impossible or inconvenient. The example is writing a small COM object using ATL. Using the _ATL_MIN_CRT definition requires not using exceptions. This case should be handled using a resulting return value.
HRESULT GetQueryString(CComBSTR &queryString);
DWORD GetQueryString(CComBSTR &queryString);
BOOL OpenSource();
Use the following prototype static members to operate with detailed error information.
LocalErrors::SetLastError(HRESULT errorCode,
LPCTSTR errorDescription, .);
LocalErrors::SetLastError(HRESULT errorCode,
UINT stringResourceId, .);
HRESULT LocalErrors::GetLastError(LPCTSTR
*errorDescription = 0);

Place LocalErrors::SetLastError where the error must be described. Use Structured Exception Handling (SEH) where it's needed and transform to resulting code.
6. Mixed error handling. Usage of C++ exceptions is preferable. Thus, error result codes must be thrown as appropriate exceptions.
HRESULT hr = GetQueryString(queryString);
if(FAILED(hr))
{
throw _com_error(hr); // Or _com_issue_error(hr);
}
or
BOOL isSuccessful = OpenSource();
if(!isSuccessful)
{
LPCTSTR errorDescription = 0;
HRESULT hr = LocalErrors::GetLastError(&errorDescription)
ThrowError(hr, errorDescription);

thanks
__________________
Karpagarajan. R
Necessity is the mother of invention

Last edited by Karpagarajan : 03-27-2007 at 04:54 PM.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

LinkBacks (?)
LinkBack to this Thread: http://www.discussweb.com/c-c-programming/804-how-write-error-free-programs-c-c.html
Posted By For Type Date
Digg / Programming / Upcoming This thread Refback 07-20-2007 07:37 AM
Digg / Programming / Upcoming This thread Refback 07-20-2007 04:20 AM
Digg / Technology / Upcoming This thread Refback 07-20-2007 03:42 AM

Similar Threads
Thread Thread Starter Forum Replies Last Post
Top 10 P2P File Sharing Programs Sabari Networking & Internet Connectivity 9 12-07-2007 04:18 AM
What’s the difference between Response.Write() andResponse.Output.Write()? prasath ASP and ASP.NET Programming 1 07-19-2007 03:56 AM
How to write programs that run on XBox360 / Sony PSP? itbarota Game Development 0 07-16-2007 12:29 AM
SEO programs juol The Lounge 1 03-15-2007 10:15 AM
PPC Programs (Traditional) sfod223 Promotion Techniques 2 02-20-2007 07:28 AM


All times are GMT -7. The time now is 04:01 PM.


Copyright ©2004 - 2007, DiscussWeb. All Rights Reserved.

SEO by vBSEO 3.0.0