This is a discussion on try-throw-catch within the C++ Programming forums, part of the General Programming Boards category; Originally Posted by anon Code: void FileHandling::ReadFile(const string& filename) throw( int ) Again you are saying you would throw int's. ...
Did you try removing it? Like this:Code:void ReadFile(const string& filename);
I tried running the code and found the problem. The issue was the same as the crash I mentioned earlier when you create a FileHandling object but don't call ReadFile. When you exit from ReadFile early (before the dynamic array is created) you get the same problem, it causes a crash when the FileHandling object is destroyed.
The reason is that you don't initialize vec, so when the destructor tries to delete it then it tries to call delete on a garbage memory location. The best solution of course is to use the vector class. But if you aren't allowed to, then just initialize vec to 0 in the constructor and the delete will not cause any problems if it is never set to another value.
Also, if you must keep the dynamic array, you should disable copying so that you don't get crashes when your class is copied.
Another thing: you use delete, but new[]. You must match the array and non-array forms of new and delete correctly. If you allocate with new[], you must deallocate with delete[].
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
Another note about throw(type):
Visual Studio doesn't support throw(type), so rather you should use throw(...) if anything at all. Unless you don't want your code to compile with Visual Studio.
For information on how to enable C++11 on your compiler, look here.
よく聞くがいい!私は天才だからね! ^_^
The code will compile fine, it will just generate warnings. If you want exception specifications for whatever reason, then you'll have no problems porting your code to Visual Studio.
throw(int) doesn't work.
throw(...) does, though.
Visual Studio doesn't support throw(type) specifications, for whatever reason...
For information on how to enable C++11 on your compiler, look here.
よく聞くがいい!私は天才だからね! ^_^
It compiles and runs for me (VC++ 7.1). Not sure why you think it doesn't work.
OK, so that's weird because I'm sure it didn't work in 2005.
In 2008 I just get a warning that the compiler ignores it because it's not implemented.
For information on how to enable C++11 on your compiler, look here.
よく聞くがいい!私は天才だからね! ^_^