Last post Aug 17, 2016 10:23 PM by PaulTheSmith
Aug 12, 2016 02:38 PM|SkyDriving|LINK
I have these :
static class GeneralMethods
public asyn static Task<StorageFolder> GetFolder(string strMainFolder, string strtargetFolder)
StorageFolder targetfolder = null;
//- code checking folder --
//----caller call this static function
StorageFolder fdl = await GeneralMethods.GetSubfolder("MySchedulefolder", "holiday");
I want to know these :
1) caller call the function. How to catch the error or exception in the code-checking in the static function : GetFolder() ??
2) How the caller receive the error message as well as the empty storageFolder ?
3) What must if do, if this not workable?
Aug 13, 2016 03:25 AM|gerrylowry|LINK
@SkyDriving welcome to forums.asp.net
Regarding exceptions: it does not matter whether an exception occurs in static or non-static code.
Think of the operating system (for most us in the forums.asp.net world) as Windows (although that is changing with the growth of open source cross-platform .NET).
Metaphor (over simplified):
if the operating system is the ocean ...
below the ocean is iis (or some other web server)
below that is your website
below that a method you use for a specific task
below that some .NET method
below that some low-level routine
at any level, an exception might thrown ... if that exception is not wrapped in a try/catch it bubbles up towards the surface until it is caught at some higher level.
if the exception gets all the way to the surface and is not caught by the operating system, the operating system might crash with a BSOD (Blue Screen of Death).
Exception Class: https://msdn.microsoft.com/en-us/library/system.exception(v=vs.110).aspx
Exception Hierarchy: https://msdn.microsoft.com/en-us/library/z4c5tckx(v=vs.110).aspx
Handling and Throwing Exceptions: https://msdn.microsoft.com/en-us/library/5b2yeyab(v=vs.110).aspx
Exceptions and Exception Handling (C# Programming Guide): https://msdn.microsoft.com/en-us/library/ms173160.aspx
Best Practices for Exceptions: https://msdn.microsoft.com/en-us/library/seyhszts(v=vs.110).aspx
How to: Create User-Defined Exceptions: https://msdn.microsoft.com/en-us/library/87cdya3t(v=vs.110).aspx
Aug 14, 2016 04:39 AM|PaulTheSmith|LINK
Can you post some actual code? What is 'code-checking'? What is it checking? What relation does it have to the previous code you have shown?
Can you state the precise problem that you are having?
Is your problem the compiler error that you get for "catch()"? Remove the () to have your handler catch all exceptions or add an exception type inside the brackets to catch particular types of Exceptions.
Aug 16, 2016 12:55 PM|SkyDriving|LINK
It is something like this. How to handle (1) and (2) for below code:
StorageFolder fdl = await MyStatucMethods.GetSubfolders("MyTravel", "Haiwaii");
----- this method in a static class call MyStaticMethods
public async static Task<StorageFolder> GetSubfolders(string strMainFolder, string strFolderName)
StorageFolder mainFolder = await storageFolder.GetFolderAsync(strMainFolder);
IReadOnlyList<StorageFolder> subfolders = await mainFolder.GetFoldersAsync();
foreach (StorageFolder folder in subfolders)
if (strTargetFolder == strFolderName)
targetfolder = folder;
catch (Exception ex)
throw ex; //<---- how the caller catch this error ? (1)
throw custom error <---- how to do this? (2)
Aug 17, 2016 10:23 PM|PaulTheSmith|LINK
The normal thing to do is to create a custom exception which has the original exception as its InnerException.
Any custom exception that you create should subclass System.Exception (or one of its subclasses). The base System.Exception has a property called InnerException, this is where you store the exception that caused the problem.
Do you have code for your custom exception? It could look something like
public class FolderException: Exception
public FolderException(string message)
public FolderException(string message, Exception inner)
: base(message, inner)
Any callers of your GetSubfolders method could protect the call with a Try block and Catch the FolderException explicitly. In the Catch handler the caller can examine the .InnerException if they need to know more detail about why the FolderException was
The Catch block in GetSubfolders would be
catch (Exception ex)
throw new FolderException("Folder listing failed", ex);
This could/should be expanded to catch different Exceptions which all create the same FolderException but with different messages.