locked
How caller receive the Exception message when calling a static function using try ctach RRS feed

  • Question

  • User-1980778524 posted

    Hi,

    I have these :


       static class GeneralMethods
       {
           public asyn static Task<StorageFolder> GetFolder(string strMainFolder, string strtargetFolder)
          {
               StorageFolder targetfolder = null;
            
               //- code  checking folder --

               
                return targetfolder;
           }

        }


    //----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() ??


        Try
        {
          code-checking
        }
       catch ()
        {

        }


    2) How the caller receive the error message as well as the empty storageFolder ?

    3) What must if do, if this not workable?

    Thanks

    Friday, August 12, 2016 2:38 PM

Answers

  • User303363814 posted

    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()
        {
        }
    
        public FolderException(string message)
            : base(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 generated.

    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.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 17, 2016 10:23 PM

All replies

  • User-434868552 posted

    @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

    et cetera

    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).

    References:

    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 

    Saturday, August 13, 2016 3:25 AM
  • User303363814 posted

    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.

    Sunday, August 14, 2016 4:39 AM
  • User-1980778524 posted

    Hi all,

    It is something like this.  How to handle (1) and (2)  for below code:

    ---- caller

       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)
            {

             try
                {
                    StorageFolder mainFolder = await storageFolder.GetFolderAsync(strMainFolder);
                 
                    IReadOnlyList<StorageFolder> subfolders = await mainFolder.GetFoldersAsync();
                   
                    foreach (StorageFolder folder in subfolders)
                    {
                        if (strTargetFolder == strFolderName)
                        {
                            targetfolder = folder;
                            return targetfolder;                      
                        }
                    }

                    return targetfolder;
                }
                catch (Exception ex)
                {

                   throw ex; //<---- how the caller catch this error ? (1)

                    throw custom error <---- how to do this? (2)
             
                }
            }

    }

    Thanks

    Tuesday, August 16, 2016 12:55 PM
  • User303363814 posted

    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()
        {
        }
    
        public FolderException(string message)
            : base(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 generated.

    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.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 17, 2016 10:23 PM