Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Catching ostream exception

This thread is locked; no one can reply to it. rss feed Print
Catching ostream exception
#00JMP00
Member #14,740
November 2012

Please read carefully, if you want to answer...

I have a weird problem.

I try to check if I can create a file. To avoid not defined behaviour,
the code should throw an exception. Actually it throws an exception, but
creates the file anyway. How is this??

Can someone help with usefull information on this thing?

bamccaig
Member #7,536
July 2006
avatar

It's difficult to help without showing us the code. In particular, how are you checking that you can create a file? As a rule, it's often better to just try to create the file and deal with any errors if they occur rather than trying to predict ahead of time if it'll be possible to create a file (which would also potentially create a race condition).

#00JMP00
Member #14,740
November 2012

The code would look somewhat like this

#SelectExpand
1 2ostream test; 3 4try { 5 6test.open (Zielpfad...other necessary stuff); 7 8throw(99); 9 10 11}catch (int e) { 12 13if (e==99) allegro_message ("Konnte Datei nicht erstellen..."); 14 15} 16test.close();

It does create the file, but still throws the exception, which makes the code
useless.

Thomas Fjellstrom
Member #476
June 2000
avatar

You're throwing the exception? C++ exceptions don't checkpoint all operations. If you want to remove the file on errors after the file is open, you have to do it.

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

bamccaig
Member #7,536
July 2006
avatar

Note that you can enable exceptions for an IO stream object: http://www.cplusplus.com/reference/ios/ios/exceptions/. That will automatically throw if there's a problem. If you wish to use exception handling for file IO in C++ that may be the "right" way, albeit C++ exceptions leave much to be desired. Alternatively, you can rely on return values and explicit checks... In this particular case you're throwing regardless of the result of opening the file so it's no wonder the exception occurs. :-/

Append:

If you try to open a file for writing that doesn't exist the system will typically create it for you assuming you have sufficient privileges in the file system to do it. If you want to only open an existing file then you will need to specify the correct incantation of parameters when opening the file for the underlying system calls to require the file to exist before opening it. Since you're concealing the exact code it's impossible for us to further reason about what trouble you're having...

#00JMP00
Member #14,740
November 2012

Actually, I only wanted to know, if it is possible to create the file, as the path or the destination could be faulty.

Therefore I don't know why the execption occurs, when the operations is successfull.

Somebody pointed at closing the file. This could really be a problem, since closing not open streams sometimes crashes the program. But this problem would have been dealt with, if the exception had worked ok.

Edgar Reynaldo
Member #8,592
May 2007
avatar

#00JMP00 said:

Actually, I only wanted to know, if it is possible to create the file, as the path or the destination could be faulty.

The method I would use to determine this is as follows :

1. Check if the containing folder of the file you want to create exists already.
2. Check the permissions of that folder.
3. Check if the file exists already.
4. Check the file's permissions if necessary.

You can do all this with Allegro 5.

See al_create_fs_entry, and al_get_fs_entry_mode. There's no need for exceptions at all. But you can still throw one if you like. ;)

bamccaig
Member #7,536
July 2006
avatar

The method I would use to determine this is as follows :

1. Check if the containing folder of the file you want to create exists already.
2. Check the permissions of that folder.
3. Check if the file exists already.
4. Check the file's permissions if necessary.

You can do all this with Allegro 5.

What prevents the directory from being removed between step 1 and step 2? :-/ What if you check the permissions wrong, or the underlying drivers work differently than you think they do?

The operating system already checks all of this stuff for you.

Edgar Reynaldo
Member #8,592
May 2007
avatar

Because this way you know why it failed. Catching a vague iostream exception won't tell you that. As for file corruption or changes mid-code, the user needs to take care of any synchronization problems themself. If you execute the steps I laid out, and then try to open the file and the bad or fail bit is set or thrown you know someone else is using the file.

This is all moot anyway, as using fopen would either succeed or fail to create or open a file and you would know the result right away. (edit - well, you might want to know whether it exists first, see below).

Edit
You might also be interested in al_filename_exists, or al_create_fs_entry and al_fs_entry_exists.

pkrcel
Member #14,001
February 2012

#00JMP00 said:

Therefore I don't know why the execption occurs, when the operations is successfull.

From what I gather you throw the exception regardless of how the operation itself returns.

I'd really use the built-in exception in std::ostream for what you need.

It is unlikely that Google shares your distaste for capitalism. - Derezo
If one had the eternity of time, one would do things later. - Johan Halmén

Go to: