Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: CBXE8 - Try Catch System::ChDir(), TDirectory::CreateDirectory() failed ?


This question is answered. Helpful answers available: 2. Correct answers available: 1.


Permlink Replies: 2 - Last Post: Aug 26, 2015 5:56 PM Last Post By: GAI CHEW KAI Threads: [ Previous | Next ]
GAI CHEW KAI

Posts: 117
Registered: 7/25/14
CBXE8 - Try Catch System::ChDir(), TDirectory::CreateDirectory() failed ?  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Aug 26, 2015 12:10 AM
Embarcadero® C++Builder XE8 Version 22.0.19027.8951, Indy 10.
Windows 10 Pro x64
Android 4.1.2

QUESTIONS:

1. I try to create a folder under Android environment within try catch statement, both System::ChDir() and TDirectory::CreateDirectory() cannot capture exception when the specified folder failed to be created, and function foo() below returns 0 always, why ?

eg.

//
// foo() is placed under .h file.
//

__inline int foo (void)
{
UnicodeString us;
int iResult;

us = L"\storage\myfolder";

iResult = 0;
try {
TDirectory::CreateDirectory( us );
}
catch ( ... ) {
iResult = -1;
}

iResult = 0;
try {
System::ChDir( us );
}
catch ( ... ) {
iResult = -1;
}

return iResult;
}

Please advise.
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: CBXE8 - Try Catch System::ChDir(), TDirectory::CreateDirectory() failed? [Edit]  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Aug 26, 2015 11:32 AM   in response to: GAI CHEW KAI in response to: GAI CHEW KAI
GAI wrote:

1. I try to create a folder under Android environment within try catch
statement, both System::ChDir() and TDirectory::CreateDirectory()
cannot capture exception when the specified folder failed to be
created, and function foo() below returns 0 always, why ?

System::ChDir() does not raise an exception on failure. You have to call
System::IOResult() after ChDir() exits to find out if it failed or not.

TDirectory::CreateDirectory() raises an exception if the specified path is
malformed, but does not raise an exception if the actual creation fails,
and there is no way to check if a failure occured, other than to see if the
directory actually exists or not after CreateDirectory() exits.

us = L"\storage\myfolder";

In C/C++, you need to escape forward slashes in literals.

Try this:

__inline int foo (void)
{
    UnicodeString us = L"\\storage\\myfolder";
 
    try
    {
        TDirectory::CreateDirectory(us);
        if (!TDirectory::Exists(us))
            return -1;
 
        System::ChDir(us);
        if (System::IOResult() != 0)
            return -1;
    }
    catch (const Exception &) {
        return -1;
    }
 
    return 0;
}


--
Remy Lebeau (TeamB)
GAI CHEW KAI

Posts: 117
Registered: 7/25/14
Re: CBXE8 - Try Catch System::ChDir(), TDirectory::CreateDirectory() failed? [Edit]  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Aug 26, 2015 5:56 PM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
It working now, thanks again :)
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02