Exception Handling Class Woes

This is a discussion on Exception Handling Class Woes within the C++ Programming forums, part of the General Programming Boards category; Hi , I have just been investigating the use of classes to do exception handling, and am just running into ...

  1. #1
    Registered User ventolin's Avatar
    Join Date
    Jan 2004
    Posts
    92

    Exception Handling Class Woes

    Hi ,

    I have just been investigating the use of classes to do exception handling, and am just running into a few problems, and not sure why!!

    Here is what im attempting code wise .. does any one know why i get an abnormal program termination when running this code? (using WinXP / MSVC++)

    Code:
    #include <iostream>
    
    using namespace std;
    
    class Exception
    {
    public:
    	
    	Exception() {}
    	Exception(char *file,int line) 
    	{ 
    		cout << "Exception on line: " << line << endl; 
    		cout << "Exception in file: " << file << endl; 
    	}
    
    	~Exception() { cout << "Destructor called" << endl; }
    };
    
    int main()
    {
    
    	int i = 0;
    
    	if(i == 0) 
    		throw Exception(__FILE__,__LINE__);
    	
    	return 0;
    }

  2. #2
    Registered User
    Join Date
    Aug 2002
    Location
    Hermosa Beach, CA
    Posts
    446
    Because you are throwing the exception, and you are not catching it. That is what is supposed to happen. Maybe this is what you want?

    Code:
    #include <iostream>
    
    using namespace std;
    
    class Exception
    {
    public:
    	
    	Exception() {}
    	Exception(char *file,int line) 
    	{ 
    		cout << "Exception on line: " << line << endl; 
    		cout << "Exception in file: " << file << endl; 
    	}
    
    	~Exception() { cout << "Destructor called" << endl; }
    };
    
    int main()
    {
    
    	int i = 0;
    
            try {
    	if(i == 0) 
    		throw Exception(__FILE__,__LINE__);
            }
            catch (Exception& e) {
                cout << "Caught exception" << endl;
            }
    	
    	return 0;
    }
    Also, usually, you would expose the file and line number via some interface (or just make them public members), and then you would print out whatever you wanted in the catch block. Also, you might want to look at the std::exception class.
    The crows maintain that a single crow could destroy the heavens. Doubtless this is so. But it proves nothing against the heavens, for the heavens signify simply: the impossibility of crows.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Exception Handling to continue execution
    By dunxton in forum C Programming
    Replies: 13
    Last Post: 02-03-2009, 08:00 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 06:18 AM
  5. Abstract class problem
    By VanJay011379 in forum C++ Programming
    Replies: 9
    Last Post: 07-31-2002, 01:30 PM

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