473,398 Members | 2,403 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,398 software developers and data experts.

exception handling mechanism

Hello,

I'd like to implement an exception handling mechanism
in my C programs. Up to now I have been using a local
error handling scheme where errors are handled (when
appropriate) in the lowest-level functions eg.

char *
foo (void)
{
int err = 0; /* (error flag: 1=set) */
char *str = xmalloc (BUF_LEN);

int ret = do_something (str);
if (!ret)
{
++err;
goto clean_up;
}

clean_up:
if (err)
{
/* handle error */
}

return str;
}

void
bar (void)
{
char *s = foo ();
/* ... */
}

What this sheme lacks is a way for the called function to pass error
information back to the calling function so that it can deal with it
in appropriate situations. I've heard of people implementing TRY,
CATCH, THROW using setjmp & longjmp, but I've never seen any code
for this...

Regards,
Ralph

Nov 14 '05 #1
1 1104


"Ralph A. Moeritz" wrote:

Hello,

I'd like to implement an exception handling mechanism
in my C programs. Up to now I have been using a local
error handling scheme where errors are handled (when
appropriate) in the lowest-level functions eg.

char *
foo (void)
{
int err = 0; /* (error flag: 1=set) */
char *str = xmalloc (BUF_LEN);

int ret = do_something (str);
if (!ret)
{
++err;
goto clean_up;
}

clean_up:
if (err)
{
/* handle error */
}

return str;
}

void
bar (void)
{
char *s = foo ();
/* ... */
}

What this sheme lacks is a way for the called function to pass error
information back to the calling function so that it can deal with it
in appropriate situations. I've heard of people implementing TRY,
CATCH, THROW using setjmp & longjmp, but I've never seen any code
for this...

Regards,
Ralph


Why make life complicated? There are two extremely simple solutions:
1)
int foo( char **result ) {
if ( result == NULL ) return ERROR_1; /* #define ERROR_1 somewhere */
*result = malloc(...);
...
if (error) return ERROR_2;
...
return 0;
}

void bar(void) {
char *s;
int status = foo(&s);
}

2)
char *foo( int *status )

--
Fred L. Kleinschmidt
Boeing Associate Technical Fellow
Technical Architect, Common User Interface Services
M/S 2R-94 (206)544-5225
Nov 14 '05 #2

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

Similar topics

10
by: Bill Davidson | last post by:
Hi there, Please forgive me for posting this article on multiple groups. Being new in the newsgroups, I was not sure which group would have been appropriate for my question. Sorry. My...
28
by: Scott Brady Drummonds | last post by:
Hi, all, I just got out of a meeting with a team of software developers that I recently joined as they are staffing to create a medium-sized project (potentially all of which will be written in...
3
by: Master of C++ | last post by:
Hi, I am an absolute newbie to Exception Handling, and I am trying to retrofit exception handling to a LOT of C++ code that I've written earlier. I am just looking for a bare-bones, low-tech...
4
by: Robbie Hatley | last post by:
I've been playing with "auto_ptr" and the "Resource Acquisition Is Initialization" concept. On page 199 of Lippman's book "Effective C++" he says "All active local class objects of a function are...
4
by: Anirudha Vengurlekar | last post by:
Hello Community, We can have nested TRY , CATCH , FINALLY blocks in the application. So in what situation (in a class or in a function) I would use this nested Try catch blocks? If someone...
6
by: Kevin Jackson | last post by:
Let's say we log exceptions to the windows application event log using the Exception Management Application Block. Is there a pattern that can be used so the exception is logged only once and not...
44
by: craig | last post by:
I am wondering if there are some best practices for determining a strategy for using try/catch blocks within an application. My current thoughts are: 1. The code the initiates any high-level...
132
by: Zorro | last post by:
The simplicity of stack unraveling of C++ is not without defective consequences. The following article points to C++ examples showing the defects. An engineer aware of defects can avoid...
1
by: George2 | last post by:
Hello everyone, Such code segment is used to check whether function call or exception- handling mechanism runs out of memory first (written by Bjarne), void perverted() { try{
7
by: Muzammil | last post by:
Hi, i m unable to make a programable exception handling. i can just throw an exception and catch it and try for ones. but its not enough, i want to an exception which can work untill user dont...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
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
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.