![]() |
|
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, Can someone help with usefull information on this thing? |
bamccaig
Member #7,536
July 2006
![]() |
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). -- acc.js | al4anim - Allegro 4 Animation library | Allegro 5 VS/NuGet Guide | Allegro.cc Mockup | Allegro.cc <code> Tag | Allegro 4 Timer Example (w/ Semaphores) | Allegro 5 "Winpkg" (MSVC readme) | Bambot | Blog | C++ STL Container Flowchart | Castopulence Software | Check Return Values | Derail? | Is This A Discussion? Flow Chart | Filesystem Hierarchy Standard | Clean Code Talks - Global State and Singletons | How To Use Header Files | GNU/Linux (Debian, Fedora, Gentoo) | rot (rot13, rot47, rotN) | Streaming |
#00JMP00
Member #14,740
November 2012
|
The code would look somewhat like this 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 |
Thomas Fjellstrom
Member #476
June 2000
![]() |
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. -- |
bamccaig
Member #7,536
July 2006
![]() |
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... -- acc.js | al4anim - Allegro 4 Animation library | Allegro 5 VS/NuGet Guide | Allegro.cc Mockup | Allegro.cc <code> Tag | Allegro 4 Timer Example (w/ Semaphores) | Allegro 5 "Winpkg" (MSVC readme) | Bambot | Blog | C++ STL Container Flowchart | Castopulence Software | Check Return Values | Derail? | Is This A Discussion? Flow Chart | Filesystem Hierarchy Standard | Clean Code Talks - Global State and Singletons | How To Use Header Files | GNU/Linux (Debian, Fedora, Gentoo) | rot (rot13, rot47, rotN) | Streaming |
#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
Major Reynaldo
May 2007
![]() |
#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. 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. My Website! | EAGLE GUI Library Demos | My Deviant Art Gallery | Spiraloid Preview | A4 FontMaker | Skyline! (Missile Defense) Eagle and Allegro 5 binaries | Older Allegro 4 and 5 binaries | Allegro 5 compile guide |
bamccaig
Member #7,536
July 2006
![]() |
Edgar Reynaldo said: 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. You can do all this with Allegro 5.
What prevents the directory from being removed between step 1 and step 2? The operating system already checks all of this stuff for you. -- acc.js | al4anim - Allegro 4 Animation library | Allegro 5 VS/NuGet Guide | Allegro.cc Mockup | Allegro.cc <code> Tag | Allegro 4 Timer Example (w/ Semaphores) | Allegro 5 "Winpkg" (MSVC readme) | Bambot | Blog | C++ STL Container Flowchart | Castopulence Software | Check Return Values | Derail? | Is This A Discussion? Flow Chart | Filesystem Hierarchy Standard | Clean Code Talks - Global State and Singletons | How To Use Header Files | GNU/Linux (Debian, Fedora, Gentoo) | rot (rot13, rot47, rotN) | Streaming |
Edgar Reynaldo
Major Reynaldo
May 2007
![]() |
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 My Website! | EAGLE GUI Library Demos | My Deviant Art Gallery | Spiraloid Preview | A4 FontMaker | Skyline! (Missile Defense) Eagle and Allegro 5 binaries | Older Allegro 4 and 5 binaries | Allegro 5 compile guide |
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 |
|