causing default exceptions to throw additional info

This is a discussion on causing default exceptions to throw additional info within the C++ Programming forums, part of the General Programming Boards category; So I've been playing with massive amounts of error handling measures, and for reasons known only to a few shriveled ...

  1. #1
    Peace
    Join Date
    Aug 2001
    Posts
    1,510

    causing default exceptions to throw additional info

    So I've been playing with massive amounts of error handling measures, and for reasons known only to a few shriveled neurons somewhere in the back, I feel like playing with more. My applications should practically debug themselves .

    What I'd like to do, is somehow cause default exceptions to throw additional information, such as the file and line number they occured on. I can create my own custom exception handlers but I dont have any info on the standard exceptions.

    Code:
    try
    {
       int * p = NULL;
       *p = 10;
    }
    catch(...)
    {
       printf("The programmer is an idiot in %s at line %i", file, line);
    }
    My current knowledge indicates that this cant be done, but my current knowledge also indicates that theres always someone who knows more than me.
    "There's always another way"
    -lightatdawn (lightatdawn.cprogramming.com)

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,699
    I've thought about that too. I don't handle exceptions very often though. ;-/ You could define an error handling class and print all file and line attributes from there. I would like to see one that would attempt to recover from the exception, though. That would be most useful.
    Code:
    bool fun(bool value)
    {
        return std::pow(std::exp(1), std::complex<float>(0, 1) 
        * std::complex<float>(std::atan(1)*(1 << (value + 2))))
        .real() > 0;
    }

  3. #3
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,493
    My current knowledge indicates that this cant be done, but my current knowledge also indicates that theres always someone who knows more than me
    I have to agree. I'm not the foremost expert on this stuff, but I've never heard of what you're asking.

    A few other reasons I can think of why it isn't implemented:
    1) Throwing exceptions is resource-consuming. For stuff like dereferencing NULL pointers, it can really slow a program down.
    2) Exceptions are thrown inside the standard library. (I don't know if the new operator is handled there.) Dereferencing a NULL pointer is fairly low-level. The compiler would have to throw the exception in that case, and that would intermix standard library code with the C++ language syntax. (theoretically)
    3) Throwing an exception can be done manually in that case with very little trouble.

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Well I'm not sure for standard exceptions but you can throw your own custom exceptions. Make a exception class that you can pass the line # and file to then catch it. Just a thought.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    A default exception can be thrown on any line in your program, so how would it be able to return specific line information? It seems to me that default functionality requires that the exception not return program specific information.

    You can derive your own exception class from the base class exception, and then use one handler to catch both standard exceptions and your exception class by using the type exception& as the parameter for your catch block, but you will have to throw the exception yourself in the try block--something it seems you don't want to do.
    Last edited by 7stud; 08-02-2003 at 11:50 PM.

  6. #6
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    C++ exceptions wont alert you to a Dereference-Null situation anyway.....

    Your thinking about Structured Exception Handling - a windows tool & a whole other ballgame!

  7. #7
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    With the exmaple you gave, you couldnt easily get a code line for the error....but you can get a memory address with SEH

    Code:
    #include <windows.h>
    #include <iostream>
    
    LONG Handler(LPEXCEPTION_POINTERS lpep)
    {
    	std::cout << "An exception was raised at memory location "
    		<< lpep->ExceptionRecord->ExceptionAddress;
    	return EXCEPTION_EXECUTE_HANDLER;
    }
    
    int main() 
    { 
    
    	
    	__try
    	{
    		int* i = 0;
    		*i = 10;
    	}
    	__except(Handler(GetExceptionInformation()))
    	{}
    
    }
    <<1) Throwing exceptions is resource-consuming.

    There's a slight overhead when useing a try/catch block, but hardly anything to notice if you dont end up throwing. Ande if you do, it doesnt really matter IMO.......if you use C++ exceptions properly, you would only throw them when some critical error has taken place - not for normal diagnostic methods. And in the case of a critical error - there's hardly ever any need to optimise for a crash or failure!

    "Well yeah I have that new photoediting program - nothing changed from the earlier version, but when it crashes, it does so on average about 20% quicker!!" - If it crashes, it crashes....aim your optomisations at the parts of your program where they will count in the long run!

  8. #8
    Peace
    Join Date
    Aug 2001
    Posts
    1,510
    Thanks for the input guys. Thats pretty much the conclusions I had reached, but I thought I'd just check anyhow on the off chance that someone knew some kind of black magic solution.

    My error handling class already catches, logs and reports memory leaks and their point of allocation, and can free memory that has been lost. It also catches and handles most exceptions and carefully logs unusual behavior (such as accessing arrays OOB). Guess I cant have it all.

    Thanks again.
    "There's always another way"
    -lightatdawn (lightatdawn.cprogramming.com)

  9. #9
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    I thought there were macros within compilers that did allow you to retrieve 'line numbers'. It might be interesting to see how this macro would behave outside of the compiler - maybe it would still give the line number or maybe it would convert that to an address.

    The only way I know of to retrieve the address of the instruction that caused the exception is to check the registers and look at IP - which most compilers don't let you do. A register, stack, and memory dump at the time of the exception would provide a lot of information - albeit probably too much to wade through.

  10. #10
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    Originally posted by Bubba
    The only way I know of to retrieve the address of the instruction that caused the exception is to check the registers and look at IP - which most compilers don't let you do. A register, stack, and memory dump at the time of the exception would provide a lot of information - albeit probably too much to wade through.
    SEH provides that info.....

  11. #11
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,799
    In MSVC++ 6.0 you can use the macros __FILE__ for the filename, and __LINE__ for the line number. Would those be of help?
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  12. #12
    Peace
    Join Date
    Aug 2001
    Posts
    1,510
    >>__FILE__ for the filename, and __LINE__ for the line number

    Yes, i know; But I need a way to cause the exception to expand these macros.

    >>A register, stack, and memory dump at the time of the exception would provide a lot of information - albeit probably too much to wade through.

    Exactly. The concept is to provide a relevant piece of informatino regarding the exception, which would allow the programmer to rapidly locate (and fix) the problem. I'm working with applications upwards of 52 files averaging 400 lines (955 at most) per source file. Even the headers get up to 200 lines at times. Working with this much extremly OOP based code makes following program execution extremly difficult. That means finding the location of the error and its caller, even harder.
    "There's always another way"
    -lightatdawn (lightatdawn.cprogramming.com)

  13. #13
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    Sounds like a hefty problem there lightatdawn. Hmm. Let me do some experimenting and research and get back to you.

    I'm not sure there is a simple solution b/c when XP gives you a memory dump it goes through every stinking module in memory at the time - again, too much info. Very hard to track down errors/exceptions when so much stuff is going on all at once with polymorphic objects, processes/threads, and background tasks - not to mention your task being switched to and from the CPU a zillion times a minute.

    But there has to be a way to simplify all that to isolate the error.

  14. #14
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    Originally posted by lightatdawn

    Exactly. The concept is to provide a relevant piece of informatino regarding the exception, which would allow the programmer to rapidly locate (and fix) the problem. I'm working with applications upwards of 52 files averaging 400 lines (955 at most) per source file. Even the headers get up to 200 lines at times. Working with this much extremly OOP based code makes following program execution extremly difficult. That means finding the location of the error and its caller, even harder.
    If it's errors in your code that you are trying to find, why not try an assert?

    How they report an error differs with the implementations I have used, but they usually report the line number & file........also, they only operate in a debug mode, and so when you release your (by then) error free release version, they wont hinder it in any way.

    I've also seen people add more to assert with an AND statment...
    like assert ((foo != NULL) && "More info here");. The latter part doesnt do anything, but if the first expression is false, the assert will kick in and the text will most likely be displayed in the warning - not sure I like this little trick to much, but it's an idea...

    Code:
    #include <iostream>
    #include <cassert>
    
    class foo
    {
    public:
    	void bar(){std::cout << "Lalalala" << std::endl;}
    };
    
    foo* GetPtr()
    {
    	static foo f;
    	static int i;
    	
    	if(++i > 2)
    		return 0;
    		
    	return &f;
    
    }
    
    
    int main( void )
    {
    	foo* ptrf = 0;
    	for(int i = 0;i < 10;++i)
    	{
    		ptrf = GetPtr();
    		assert((ptrf != 0) && "NULL Pointer Returned!!");
    		ptrf->bar();
    		
    	}	
    }

  15. #15
    Peace
    Join Date
    Aug 2001
    Posts
    1,510
    I'm not a big fan of asserts. There really isnt any parts in my code that are "likely" to throw an exception. It just that on occasion, a new piece of code will interface with an old piece of code in such a way that the old (or new) code breaks. Suddenly I have crashing (or erratic behavior) and no way to find out the cause. Possibly its intermittent and so I cant track it with the debugger. Debugging DX apps is hell anyhow.
    "There's always another way"
    -lightatdawn (lightatdawn.cprogramming.com)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. Long-lasting objects that throw exceptions
    By drrngrvy in forum C++ Programming
    Replies: 7
    Last Post: 10-05-2006, 04:30 PM
  3. Unhandled exceptions (throw with no try)
    By Mario F. in forum C++ Programming
    Replies: 3
    Last Post: 07-01-2006, 06:31 AM
  4. Help doing an e-mail program in c...
    By Tyler_Durden in forum C Programming
    Replies: 88
    Last Post: 01-02-2005, 02:12 PM
  5. Why throw exceptions?
    By Brown Drake in forum C++ Programming
    Replies: 2
    Last Post: 11-02-2001, 05:24 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21