Hi, I'm thinking of having a seperate module in my project that deals
with the exceptions and errors as they occur in a C program. Is this a
good idea ? I thought of doing this because it is getting really
repetitive to have the same thing in my code over and voer again eg.
if malloc fails then:
int *p;
p = malloc(sizeof(int) * 5);
if(p == NULL)
{
perror("malloc has failed");
exit(EXIT_FAILURE);
}
This situation may arise in many places in different contexts so what
I would like to do is identify such cases and have a seperate module
with a switch case that takes an error flag as an input and makes the
decision based on it. The error flags would represent the various
errors/exceptions like malloc failure, failure in opening file, scanf
failure, floating point exceptions , array out of bounds etc. I would
also like to print the name of the module where the exception occuerd
as well as the line number. Is there a function in C which provides
this facility ? 18 1771
pereges wrote:
Hi, I'm thinking of having a seperate module in my project that deals
with the exceptions and errors as they occur in a C program. Is this a
good idea ? I thought of doing this because it is getting really
repetitive to have the same thing in my code over and voer again eg.
if malloc fails then:
int *p;
p = malloc(sizeof(int) * 5);
if(p == NULL)
{
perror("malloc has failed");
exit(EXIT_FAILURE);
}
This situation may arise in many places in different contexts so what
I would like to do is identify such cases and have a seperate module
with a switch case that takes an error flag as an input and makes the
decision based on it. The error flags would represent the various
errors/exceptions like malloc failure, failure in opening file, scanf
failure, floating point exceptions , array out of bounds etc.
A good example of this are, IMHO, Marc J. Rochkind's EC_* macros
described in the "Advanced UNIX Programming" book.
Have a look at http://basepath.com/aup, especially the examples' source
code for the first chapter (look for ec.[h|c]).
I would
also like to print the name of the module where the exception occuerd
as well as the line number. Is there a function in C which provides
this facility ?
Standard C doesn't, but GCC provides the __FILE__ and __LINE__ macros,
which expand to the filename and line number where they appear.
--
Pietro Cerutti
In comp.lang.c, pereges wrote:
Hi, I'm thinking of having a seperate module in my project that deals
with the exceptions and errors as they occur in a C program. Is this a
good idea ?
IMHO, for a small number of common exceptions and/or errors, this is a good
idea, but it breaks down when you want to generalize it, or later add
behaviour changes.
[snip]
This situation may arise in many places in different contexts so what
I would like to do is identify such cases and have a seperate module
with a switch case that takes an error flag as an input and makes the
decision based on it.
For a limited number of errors or exception conditions, this would work.
But, it isn't scalable, and your initial design/implementation may not
properly handle exceptional processing.
For example, your initial design may simply report the error and call
exit(). Subsequently, you may find reason to report the error and return
back to the caller, or report the error and request a user decision, or
silently handle the error and return. Each one of these changes would
necessitate corresponding behavioural changes in your common handler, along
with a (possibly) ever-growing list of function arguments (function,
message, output-log-file FILE, user-input-action-message,
user-input-options, user-input-file FILE, user-input-via-GUI flag,
exit/return code, abnormal-termination-with-core-dump flag, ...).
You need to be careful about how you design the initial function, and under
what circumstances it will be used, otherwise accomodating your "exception
handler" function may consume more programming resources than the rest of
the program.
[snip]
Is there a function in C which provides this facility ?
Not in ISO standard C, no.
--
Lew Pitcher
Master Codewright & JOAT-in-training | Registered Linux User #112576 http://pitcher.digitalfreehold.ca/ | GPG public key available by request
---------- Slackware - Because I know what I'm doing. ------
Pietro Cerutti wrote:
pereges wrote:
>[...] I would also like to print the name of the module where the exception occuerd as well as the line number. Is there a function in C which provides this facility ?
Standard C doesn't, but GCC provides the __FILE__ and __LINE__ macros,
which expand to the filename and line number where they appear.
__FILE__ and __LINE__ are part of Standard C. In C99
there is also __func__ for the name of the enclosing function
(it's a plain identifier, though, not a macro).
-- Er*********@sun.com
On 19 May 2008 at 18:01, Pietro Cerutti wrote:
Standard C doesn't, but GCC provides the __FILE__ and __LINE__ macros,
which expand to the filename and line number where they appear.
You could also just dump core, which would give you access to a full
backtrace - very useful for debugging.
Eric Sosman wrote:
Pietro Cerutti wrote:
>pereges wrote:
>>[...] I would also like to print the name of the module where the exception occuerd as well as the line number. Is there a function in C which provides this facility ?
Standard C doesn't, but GCC provides the __FILE__ and __LINE__ macros, which expand to the filename and line number where they appear.
__FILE__ and __LINE__ are part of Standard C.
Right.
--
Pietro Cerutti
can someone please give a simple example demonstrating _FILE_, _LINE_
and _func_
pereges wrote:
can someone please give a simple example demonstrating _FILE_, _LINE_
and _func_
cat filelinefunc.c
#include <stdio.h>
int main(void)
{
printf("%s:%d (%s)\n", __FILE__, __LINE__, __func__);
return (0);
}
./filelinefunc
filelinefunc.c:4 (main)
--
Pietro Cerutti
On May 19, 11:27 pm, Pietro Cerutti <g...@gahr.chwrote:
pereges wrote:
can someone please give a simple example demonstrating _FILE_, _LINE_
and _func_
cat filelinefunc.c
#include <stdio.h>
int main(void)
{
printf("%s:%d (%s)\n", __FILE__, __LINE__, __func__);
return (0);}
./filelinefunc
filelinefunc.c:4 (main)
why the paranthesis for the last %s format specifier ?
pereges wrote:
On May 19, 11:27 pm, Pietro Cerutti <g...@gahr.chwrote:
>pereges wrote:
>>can someone please give a simple example demonstrating _FILE_, _LINE_ and _func_ cat filelinefunc.c
#include <stdio.h> int main(void) { printf("%s:%d (%s)\n", __FILE__, __LINE__, __func__); return (0);}
> ./filelinefunc
filelinefunc.c:4 (main)
why the paranthesis for the last %s format specifier ?
Because I wanted the function name to appear between parenthesis... it's
part of the format string.
--
Pietro Cerutti
Antoninus Twink wrote, On 19/05/08 19:14:
On 19 May 2008 at 18:01, Pietro Cerutti wrote:
>Standard C doesn't, but GCC provides the __FILE__ and __LINE__ macros, which expand to the filename and line number where they appear.
You could also just dump core, which would give you access to a full
backtrace - very useful for debugging.
Unless the system does not produce a core dump or equivalent. Systems
which do not produce core dumps are quite common.
--
Flash Gordon
Pietro Cerutti wrote:
pereges wrote:
.... snip ...
>
>I would also like to print the name of the module where the exception occuerd as well as the line number. Is there a function in C which provides this facility ?
Standard C doesn't, but GCC provides the __FILE__ and __LINE__
macros, which expand to the filename and line number where
they appear.
Those macros are provided by C99. Also C89, C90, and C95. I.E.
Standard C does provide them.
--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.
** Posted from http://www.teranews.com **
CBFalconer wrote:
Pietro Cerutti wrote:
>pereges wrote:
... snip ...
>>I would also like to print the name of the module where the exception occuerd as well as the line number. Is there a function in C which provides this facility ?
Standard C doesn't, but GCC provides the __FILE__ and __LINE__ macros, which expand to the filename and line number where they appear.
Those macros are provided by C99. Also C89, C90, and C95. I.E.
Standard C does provide them.
Yes, see my answer to Eric.
--
Pietro Cerutti
Lew Pitcher <lp******@teksavvy.comwrote in message
news:3e***************************@TEKSAVVY.COM-Free...
In comp.lang.c, pereges wrote:
Hi, I'm thinking of having a seperate module in my project that deals
with the exceptions and errors as they occur in a C program. Is this a
good idea ?
IMHO, for a small number of common exceptions and/or errors, this is a
good
idea, but it breaks down when you want to generalize it, or later add
behaviour changes.
Hmmmm, well, not necessarily...I think you have to come up with
the right generalized scheme. At least I seem to have gained some
good practical coding speed using such a scheme.
[snip]
This situation may arise in many places in different contexts so what
I would like to do is identify such cases and have a seperate module
with a switch case that takes an error flag as an input and makes the
decision based on it.
For a limited number of errors or exception conditions, this would work.
But, it isn't scalable, and your initial design/implementation may not
properly handle exceptional processing.
I think the trick, as always with low-level support library functions, is
to abstract what you are doing over and over and over again in the
application,
and generalize it to the extent possible.
For errors, you have a severity level of error (warning, caution, etc.), a
logging/reporting "requirement", possible prompted user actions that may
still fail to fix the problem, and the application action that is taken if
the
problem cannot be solved by user action. Most, but not all, of these
can and should be handled generically at a lower-level...
For example, your initial design may simply report the error and call
exit().
Both of these can be handled in a low-level generic library function...
Subsequently, you may find reason to report the error and return
back to the caller,
IBID. As a matter of fact, the way I write my particular applications,
you are generally kicked out of the operation gracefully and returned
to a "menu" state to select another operation, with a timed "dialog"
asking the user if they want to continue with the application at all...
or report the error and request a user decision,
IBID.
or
silently handle the error and return.
IBID, though you'd still probably want the temporary failure to
be logged...
Each one of these changes would
necessitate corresponding behavioural changes in your common handler,
along
with a (possibly) ever-growing list of function arguments (function,
message, output-log-file FILE, user-input-action-message,
user-input-options, user-input-file FILE, user-input-via-GUI flag,
exit/return code, abnormal-termination-with-core-dump flag, ...).
Well, I didn't say it would be EASY, just a little easier (maybe!)
than re-inventing all those wheels for EVERY possible error...and
you've really got to pick your specific and generic handling
carefully, and handle each at the appropriate level...
You need to be careful about how you design the initial function, and
under
what circumstances it will be used, otherwise accomodating your "exception
handler" function may consume more programming resources than the rest of
the program.
Well, if you're talking about exceptions AND errors, maybe (I'm
starting to be reminded of "try-catch" in other languages)...
Is there a function in C which provides this facility ?
Not in ISO standard C, no.
What about "errno.h" and stuff like that?
---
William Ernest Reid
Pietro Cerutti wrote:
...
A good example of this are, IMHO, Marc J. Rochkind's EC_*
macros described in the "Advanced UNIX Programming" book.
Poor choice of namespace. Identifiers begining with E and
a capital letter are reserved whenever <errno.his included.
--
Peter
Peter Nilsson wrote:
Pietro Cerutti wrote:
>... A good example of this are, IMHO, Marc J. Rochkind's EC_* macros described in the "Advanced UNIX Programming" book.
Poor choice of namespace. Identifiers begining with E and
a capital letter are reserved whenever <errno.his included.
Fiuu, excellent catch!
Indeed, Rochkind's everything-is-included-here "defs.h" includes errno.h...
Peter
--
Pietro Cerutti
Hi
On May 19, 6:48 pm, pereges <Brol...@gmail.comwrote:
Hi, I'm thinking of having a seperate module in my project that deals
with the exceptions and errors as they occur in a C program.
You might want to mimic the API of Glib: http://library.gnome.org/devel/glib/...Reporting.html
It seems fairly extensible while remaining low level.
HTH
viza
On Mon, May 19, 2008 at 10:48:09AM -0700, pereges wrote:
Hi, I'm thinking of having a seperate module in my project that deals
with the exceptions and errors as they occur in a C program. Is this a
good idea ? I thought of doing this because it is getting really
[snip]
decision based on it. The error flags would represent the various
errors/exceptions like malloc failure, failure in opening file, scanf
failure, floating point exceptions , array out of bounds etc. I would
also like to print the name of the module where the exception occuerd
as well as the line number. Is there a function in C which provides
this facility ?
I think it is important to make difference between
a bug (the programer made a mistake);
a failure (the system did not have the resources, a subtask is cancelled);
a fatal error (it does not make sense to continue the program);
bugs: the assert macro is just for bugs, it performs a check and
writes a message, that is meaningful for the programmer only.
(assert macros can be easily switched off in the production version,
where there will be no bugs)
failure: a function cannot carry out the task. It returns an error identifier
or throws an exception, if there is no error code in the interface.
If the interface has an error code, that has to propagate though a series
of function calls until it becomes meaningful to continue.
fatal error: very simple, one has to print a user friendly message and exit.
You will want to make a small function that prints to the gui/tui/console
or logfile.
Whereas it is trivial to use error codes returned by the functions, it is
quite cumbersome, since that has to be checked every time.
But there is a limited but reasonable exception handling in standard C.
1) longjmp/setjmp
If you wish, you can define a throw and a catch macro using the fact
that !setjmp() may form the controlling state of a for statement.
You cannot throw an object, but the function call hierarchy is wound up.
Personally I use this quite often in a tui based database application.
2) atexit.
This facilitates a graceful exit.
I am not quite sure, but I think you can call atexit from a function
registered using atexit and invoked by exit(). Than you can return
to a main loop using this method, too.
Hope that this makes (some) sense
Szabolcs
"pereges" <Br*****@gmail.comwrote in message news:
Hi, I'm thinking of having a seperate module in my project that deals
with the exceptions and errors as they occur in a C program. Is this a
good idea ?
People will disagree on the value of malloc() wrappers. I proposed a
solution that would give the best of both worlds - xmalloc(). However all
code that calls it is necessarily dependent on the xmalloc() module, and
there is no way round that particular difficulty.
If you look at my MiniBasic source you will see a way of handling the
complex range of errors that can occur in processing a Basic program.
Essentially the error message is sticky, so only one is reported, after
which all subsequent errors are suppressed. This means that code can
continue to process until control is passed back up to the top level, where
the error flag is checked and, if set, the function is terminated with a
message.
However that's only one paradigm you could use.
--
Free games and programming goodies. http://www.personal.leeds.ac.uk/~bgy1mm This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: AIM |
last post by:
Error in msvc in building inheritance.obj to build hello.pyd
Hello,
I am trying to build the boost 1.31.0 sample extension hello.cpp.
I can not compile the file inheritance.cpp because the two...
|
by: Gregory |
last post by:
Hi,
One of the disadvantages of using error handling with error codes
instead of exception handling is that error codes retuned from a
function can be forgotten to check thus leading to...
|
by: deko |
last post by:
I use this convention frequently:
Exit_Here:
Exit Sub
HandleErr:
Select Case Err.Number
Case 3163
Resume Next
Case 3376
Resume Next
|
by: jack |
last post by:
hi all
im facing a problem in error handelling.
what im doing is getting the images from the database to my
csharp application form .. now here in some of the records contains
images along with...
|
by: p |
last post by:
WE had a Crystal 8 WebApp using vs 2002 which we upgraded to VS2003. I also
have Crystal 9 pro on my development machine. The web app runs fine on my
dev machine but am having problems deploying....
|
by: Manuel |
last post by:
I'm trying to compile glut 3.7.6 (dowbloaded from official site)using
devc++.
So I've imported the glut32.dsp into devc++, included manually some
headers, and start to compile.
It return a very...
|
by: Pierre |
last post by:
Hi everybody,
I'm trying to clean my code a bit and I couldn't manage to centralise
the location of my services' namespace.
I tried that:
)]
Here is the error I get in VS 2005:...
|
by: bazzer |
last post by:
hey,
im trying to access a microsoft access database from an ASP.NET web
application in visual basic 2003.NET. i get the following error when i
try running it:
Server Error in...
|
by: developer |
last post by:
Hi All
I have made a .NET project.
the files included are borland c++ files that i am migrate to VC++ .NET
I am using Microsoft Visual C++ .NET 2003.
the compilation goes through properly,...
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
| |