try-throw-catch

This is a discussion on try-throw-catch within the C++ Programming forums, part of the General Programming Boards category; Originally Posted by matsp That will not help anything, except introduce a global variable, which I'm pretty sure isn't of ...

  1. #16
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Quote Originally Posted by matsp View Post
    That will not help anything, except introduce a global variable, which I'm pretty sure isn't of any use here.

    --
    Mats
    misterwakka's (sp?) example failed for me too. Changing to this construct, it worked just fine. How will it not help anything?

  2. #17
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Strange that it changes anything - what you should do to fix it would be to chage "int code" to "ERRORS code" in the catch statement.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #18
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Well, I copied it over incorrectly from my example to his code.

    What I should have added was
    Code:
    	if(infile.fail()) { 
                    int myerror = NoSuchFile ;  
    		throw myerror ; 
                    }
    And the enum does not need the "myerrors" instantiation as well. Good catch.

    Todd

  4. #19
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Sure, you can also cast an enum to integer:
    Code:
       throw static_cast<int>(NoSuchFile);
    But what's the point of that, when the "correct" thing to do is to actually use the enum that is defined in the first place.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #20
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Quote Originally Posted by matsp View Post
    Strange that it changes anything - what you should do to fix it would be to chage "int code" to "ERRORS code" in the catch statement.

    --
    Mats
    That works too. They both (throw and catch) need to be int, or they both need to be the enum. I'm surprised, since enums are ints, the catch won't work with int when throwing the enum.

    Todd

  6. #21
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Todd Burch View Post
    That works too. They both (throw and catch) need to be int, or they both need to be the enum. I'm surprised, since enums are ints, the catch won't work with int when throwing the enum.

    Todd
    Actually, enum's and int's are not the same - they are very similar and can easily be converted back and forth, but enum's are distinctly different types from int's - even if the processor doesn't make much of a difference, the compiler thinks of them differently.

    But the point of declaring an enum is to differentiate it from an int, so that's why the compiler sees it differently.

    In plain C, the difference is even smaller.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #22
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    OK, thanks for the in-depth!! (must get back to work...)

  8. #23
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Are you saying that the only changes you made to your original code was to rename the numberOfRows enum and changethe catch to "catch (ERRORS code)"? What is the difference in behavior when you run the two versions?

  9. #24
    Registered User
    Join Date
    Dec 2007
    Posts
    49
    I can't believe it. Damn. A few moments ago everything worked just fine. I made some changes and now when it is supposed to throw an exeption I get some memory leak and dbgdel.cpp is showing up!!! :-((
    I think I'll just add the whole code...I really appreciate your help guys.

    Header
    Code:
    #ifndef FILE_HANDLING__
    #define FILE_HANDLING__
    
    #include <vector>
    #include <iostream>
    using namespace std;
    
    enum ERRORS
    {
    	NoSuchFile,
    	BadNumberOfRows,
    	WrongNumberOfLinesInHeader,
    	ErrGetLine
    };
    
    class FileHandling
    {
    private:
    	int numRecords;
    	double sum, average, minimum, maximum;
    	double* vec;
    public:
    	FileHandling();
    	~FileHandling();
    	void ReadFile(const string& filename) throw(int);
    	void CalculateValues();
        void printdata() const;
    
    	//static const int NoSuchFile=1;
    	//static const int BadNumberOfRows=2;
    	//static const int WrongNumberOfLinesInHeader=3;
    	//static const int ErrGetLine=4;
    	
    };
    
    #endif
    class CPP
    Code:
    #include "FileHandling.h"
    #include <fstream>
    using namespace std;
    
    FileHandling::FileHandling()
    {
    	numRecords=0; sum=0; average=0; minimum=0; maximum=0; //zero all class variables
    }
    
    FileHandling::~FileHandling()
    {
    	delete vec;
    }
    
    void FileHandling::ReadFile(const string& filename) throw(int)
    {
    	int numberOfRows=0;
    	ifstream infile(filename.c_str());
    	if(infile.fail()) 
    		throw NoSuchFile; //The problem
    	infile>>numberOfRows;//get the number of records in the file
    	infile.ignore(); //have to ignore \n before getline
    	if(!numberOfRows)
    		throw BadNumberOfRows;
    	vec = new double[numberOfRows]; //assign maximal needed space for array. The loop won't acceed this value anyway
    	for (int i=0;i<numberOfRows;i++) //cycle through whole file
    	{
    		if(!infile.eof()) //check not eof()
    		{
    			const int MaxLine = 80;
    			char str[MaxLine];
    			infile.getline(str, MaxLine);
    			if(infile.fail())
    				throw ErrGetLine;
    			if (str[0]!='#') //if line is not comment
    			{
    				double number=atof(str);
    				vec[numRecords]=number;
    				numRecords++;
    			}//end of if
    		}
    		else
    			throw WrongNumberOfLinesInHeader;
    	}//end of for
    }//getdata
    
    void FileHandling::CalculateValues()
    {
    	minimum=vec[0];
    	maximum=vec[0];
    
    	for(int counter=0; counter<numRecords; counter++) //sum up whole vector
    	{	
    		sum+=vec[counter];
    		if (vec[counter]<minimum) 
    			minimum=vec[counter];
    		if (vec[counter]>maximum)
    			maximum=vec[counter];
    	}
    	average=sum/numRecords;
    }//CalculateValues
    
    void FileHandling::printdata() const
    {
    	cout<<"**** FILE AUDIT RESULTS ***"<<endl
    		<<"Total records:"<< numRecords <<endl
    		<<"Sum: "<<sum<<endl<<"Average of the records: "<< average<<endl
    		<<"Minimum value found: "<<minimum<<endl<<"Maximum value found: "<<maximum<<endl;
    }//printdata
    main CPP
    Code:
    #include "FileHandling.h"
    
    void displayinfo(){
    cout << "----------------------------------------" << endl;
    cout << endl<<endl;
    }
    
    
    int main()
    {
    	displayinfo();
    	try
    	{
    		FileHandling operations;
    		operations.ReadFile("data.txt");
    		operations.CalculateValues();
    		operations.printdata();
    	}
    	//catch (int code)
    	catch (ERRORS code)
    	{
    		cerr<<"error code number: "<<code<<endl;
    		exit(1);
    	}
    	return 0;
    }
    An attached working file should of course be called data.txt
    otherwise one of the errors occurs.

  10. #25
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Perhaps this:

    Code:
    public:
    	FileHandling();
    	~FileHandling();
    	void ReadFile(const string& filename) throw(int);
    	void CalculateValues();
        void printdata() const;
    
    	//static const int NoSuchFile=1;
    	//static const int BadNumberOfRows=2;
    	//static const int WrongNumberOfLinesInHeader=3;
    	//static const int ErrGetLine=4;
    	
    };

  11. #26
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Code:
    void FileHandling::ReadFile(const string& filename) throw(int)
    Again you are saying you would throw int's. Either don't use a throw specification at all or be honest about it. The compiler won't forgive.

    Concerning error specifications, you are using new which might throw std::bad_alloc, so the specs are wrong.

    Instead of allocating memory with new you should use a std::vector, or otherwise you'll leak the allocated memory when the function throws (or handle that clean-up manually - your choice).
    Last edited by anon; 01-21-2008 at 04:01 PM.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  12. #27
    Registered User
    Join Date
    Dec 2007
    Posts
    49
    Quote Originally Posted by Todd Burch View Post
    Perhaps this:

    Code:
    public:
    	FileHandling();
    	~FileHandling();
    	void ReadFile(const string& filename) throw(int);
    	void CalculateValues();
        void printdata() const;
    
    	//static const int NoSuchFile=1;
    	//static const int BadNumberOfRows=2;
    	//static const int WrongNumberOfLinesInHeader=3;
    	//static const int ErrGetLine=4;
    	
    };
    Really good idea, which I was sure will solve the problem...But most unfortunatly it didn't...Bummer...


    Code:
    void ReadFile(const string& filename) throw(ERRORS);
    void FileHandling::ReadFile(const string& filename) throw(ERRORS)

  13. #28
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Do you still get the same issue if you remove the throw specifications?

    A couple notes on your code. First, you should still change the if (!numberOfRows) part. It will work better now that you have initialized the variable, but it isn't as correct as it could be.

    Second, you should probably be using the string class instead of C style strings unless you have a specific reason for limiting lines to 80 characters or less. The string class is generally safer and easier to use than C style strings, and in this case you wouldn't have to limit line length to 80 characters.

    Third, you should use the standard vector container rather than a dynamic array of doubles for your vec array, unless your class/instructor requires you not to. The reason is that the vector container is safer and easier to use than a plain dynamic array. For example, your code will crash if you create a FileHandling object but don't call ReadFile, or if you try to copy a FileHandling object. If you used a vector, then you probably wouldn't have either of those bugs.

  14. #29
    Registered User
    Join Date
    Dec 2007
    Posts
    49
    Quote Originally Posted by Daved View Post
    Do you still get the same issue if you remove the throw specifications?

    A couple notes on your code. First, you should still change the if (!numberOfRows) part. It will work better now that you have initialized the variable, but it isn't as correct as it could be.

    Second, you should probably be using the string class instead of C style strings unless you have a specific reason for limiting lines to 80 characters or less. The string class is generally safer and easier to use than C style strings, and in this case you wouldn't have to limit line length to 80 characters.

    Third, you should use the standard vector container rather than a dynamic array of doubles for your vec array, unless your class/instructor requires you not to. The reason is that the vector container is safer and easier to use than a plain dynamic array. For example, your code will crash if you create a FileHandling object but don't call ReadFile, or if you try to copy a FileHandling object. If you used a vector, then you probably wouldn't have either of those bugs.
    The problem is caused while performing the throws...I can't check otherwise because I have no other way to control errors rather than try-catch...

    About the other issues, thank you, and yes these are the requirements of this stupid exarcise. I'm not familiar with catch=throw becuase I usually work with win32 and now I'm taking c++ course and should learn all the console abilities.

  15. #30
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    The throw specification is the part at the end of the function declaration:
    Code:
    void ReadFile(const string& filename) throw(int);
    I just meant to remove those, since those aren't mandatory and you can still use try/catch/throw without them (and many would suggest that you not use them).

Page 2 of 3 FirstFirst 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Exceptions "try, catch, and throw Statements" ???
    By Loic in forum C++ Programming
    Replies: 2
    Last Post: 08-12-2008, 09:22 PM
  2. Throw Catch for External Program
    By dav_mt in forum C++ Programming
    Replies: 6
    Last Post: 04-19-2008, 09:52 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. Try...catch...throw or ifs?
    By Kylecito in forum C++ Programming
    Replies: 9
    Last Post: 03-02-2006, 09:41 PM
  5. try catch throw
    By ygfperson in forum C++ Programming
    Replies: 2
    Last Post: 07-27-2003, 02:15 AM

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