locked
Why missing catching exception of AccessDenyException?

    Question

  • I have develop a Metro DLL which might be called by some metro apps whose capbility of accessing documents liberary is unsured.I intent to catch the AccessDenyException to judge what I would do,but it is failed?

    the codes is as following:

    StorageFolder^ folder(nullptr);
     try
     {
      folder=KnownFolders::DocumentsLibrary;
     }
     catch (Exception^ e)
     {
      String^ error=e->Message;
      
     }
     catch( const exception & e)
     {
      const char* error=e.what();
     
     }
     catch (AccessDeniedException^ e)
     {
      String^ error=e->Message;
      
     }

    No exception has been catched,but the app crash

    Tuesday, July 17, 2012 8:26 AM

Answers

  • This should work :-

    StorageFolder^ folder= ApplicationData::Current->LocalFolder;
    
    	
    task<StorageFile^> t([folder]()
      {
    	 return folder->CreateFileAsync("Settings.xml",CreationCollisionOption::FailIfExists);
      
      });
    
      auto opr = create_task(t);
    
      opr.then([this](task<StorageFile^> t)
      {
      
    	  try
    	  {
    	  StorageFile^ fol = t.get();
    	  }
    	  catch(Exception^ e2)
    	  {
    		  this->t1->Text = e2->Message;
    	  }
    
      }, task_continuation_context::use_current());

    -Sagar

    • Marked as answer by Jesse Jiang Thursday, August 2, 2012 7:23 AM
    Friday, July 20, 2012 5:32 PM
    Moderator

All replies

  • Hi cybupt,

    I have created a Metro DLL, and a Metro app calling it to test this issue. I found the catch clause with "const exception & e" is wrong, so I comment it out. When building and debugging the DLL and the app, no exception was caught.

    Please pay attention to the try clause in your first post. That code snippet just assigns the DocumentsLibrary property to the folder variable. It will not produce AccessDeniedExeption.

    I suggest you select Package.appxmanifest file from Solution Explorer and open it. You should check the Capabilities and Declarations tabs to make sure that all the settings have been done properly. For more information about Metro app capability delcaration, you can refer to this link: http://msdn.microsoft.com/en-us/library/windows/apps/hh464936.aspx.

    If you want to catch the AccessDeniedException, I suggest you put the function calls into the try clause, such as CreateFileAsync and GetFileAsync. AccessDeniedException may occur if the functions calls of this kind fail.

    I hope this reply is helpful to you. If you have any questions, please feel free to let me know.
    Best regards,


    Helen Zhao [MSFT]
    MSDN Community Support | Feedback to us


    • Edited by Helen Zhao Wednesday, July 18, 2012 8:00 AM
    • Marked as answer by cybupt Wednesday, July 18, 2012 9:30 AM
    • Unmarked as answer by cybupt Friday, July 20, 2012 1:59 AM
    Wednesday, July 18, 2012 7:59 AM
  • Thanks .

    But I found I often miss catching some exception ,but it might break app running.

    For example,

    try
     {
      StorageFolder^ folder=ApplicationData::Current->LocalFolder;
      task<StorageFile^>(folder->CreateFileAsync("Settings.xml",CreationCollisionOption::FailIfExists)).then([](StorageFile^ )
      {
       
      });
     }
     catch (Exception^ e)
     {
      String^ error=e->Message;
     }

    I can not catch any exception if the file exists,but a unhandled exception would break app down.

    I don't konw why?

    Friday, July 20, 2012 2:07 AM
  • This should work :-

    StorageFolder^ folder= ApplicationData::Current->LocalFolder;
    
    	
    task<StorageFile^> t([folder]()
      {
    	 return folder->CreateFileAsync("Settings.xml",CreationCollisionOption::FailIfExists);
      
      });
    
      auto opr = create_task(t);
    
      opr.then([this](task<StorageFile^> t)
      {
      
    	  try
    	  {
    	  StorageFile^ fol = t.get();
    	  }
    	  catch(Exception^ e2)
    	  {
    		  this->t1->Text = e2->Message;
    	  }
    
      }, task_continuation_context::use_current());

    -Sagar

    • Marked as answer by Jesse Jiang Thursday, August 2, 2012 7:23 AM
    Friday, July 20, 2012 5:32 PM
    Moderator