It doen't make sense listing __LINE__, since will change for every line... But __FILE__ should be listed for a given filename (test.c above, for example)...
It's great for debugging - This is a quick example of how it can be used... [edit] I know that it isn't actually "thowing" an exception, it was just taken from some code I already had and simplified... [/edit]
Code:
#include <stdio.h>
#include <stdlib.h>
#define THROW_EXCEPTION(exType) my_throwing_exception( __FILE__ , \
__LINE__ , \
exType)
#define MALLOC_FAILED_EX 0
const char *getExceptionMessage(int exType)
{
switch (exType)
{
case MALLOC_FAILED_EX:
return "malloc call failed";
/* ... */
}
return "Unknown error";
}
void my_throwing_exception( const char *fileName, int lineNumber, int exType)
{
fprintf(stderr, "\nExeption found\nFile: %s\nLine: %d,\nMessage: %s", fileName, lineNumber, getExceptionMessage(exType));
}
int main(void)
{
char *apple = NULL;
int *banana = NULL;
long int *grape = NULL;
int result = EXIT_SUCCESS;
if (NULL == (apple = malloc(sizeof(*apple))))
{
THROW_EXCEPTION(MALLOC_FAILED_EX);
result = EXIT_FAILURE;
}
free(apple);
if (NULL == (banana = malloc(sizeof(*banana))))
{
THROW_EXCEPTION(MALLOC_FAILED_EX);
result = EXIT_FAILURE;
}
free(banana);
if (NULL == (grape = malloc(sizeof(*grape))))
{
THROW_EXCEPTION(MALLOC_FAILED_EX);
result = EXIT_FAILURE;
}
free(grape);
return result;
}
I first saw it used this way in a code tracer from R. Heathfield et. al., "C Unleashed", "Chapter 7: When Things Go Wrong: Code-Mending" (A great advanced book that was originally recommended by Salem)