C#: Exception handling

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Shani Aulakh
    New Member
    • Nov 2007
    • 44

    C#: Exception handling

    Hi All,

    I can't find any good examples of throwing exceptions. I have the following code. I want to include all the possible exceptions i can. Help me out on this. I am just moving away from console object and moving towards exception object.

    Code:
            public int DeleteFilesOlderThan(int Days, int Hrs, int Mins, string filter)
            {
                DateTime dt = GetRelativeDateTime(Days, Hrs, Mins);
                ArrayList oldFilePaths = FilesOlderThan(dt, filter);
                int count = 0;
                string message = null;
                foreach (string filePath in oldFilePaths)
                {
                    try
                    {
                        File.Delete(filePath);
                        count++;
                    }
                    catch (FileNotFoundException e)
                    {
    
                    }
                    catch (DirectoryNotFoundException e)
                    {
    
                    }
                    catch (Exception e)
                    {
                        message = "Unable to delete " + filePath + "\r\n";
                        message += String.Format("Reason: {0}", e.Message);
                        MessageBox.Show(message);
    
                        // Error..Property or indexer 'System.Exception.Message' cannot be assigned to--it is read only
                        //e.Message = "Unable to delete " + filePath + e.ToString();
                        //Console.WriteLine("Unable to delete " + filePath);
                        //Console.WriteLine("Reason: {0} ", e);
                    }
                    //message = ("Files successfully deleted");
                    //MessageBox.Show(message);
                    //message = String.Format("{0} ex {1} files successfully deleted", count, oldFilePaths.Count);
                    //MessageBox.Show(message);
                    //return count;
                }
                return count;
            }
    
            // Using GetRelativeDateTime as a private method instead of public however as it is only to be used internally
            private static DateTime GetRelativeDateTime(int days, int hours, int minutes)
            {
                return DateTime.Now.AddDays(-days).AddHours(-hours).AddMinutes(-minutes);
            }
    
            public ArrayList FilesOlderThan(DateTime dt, string filter)
            {
                //m_FolderPath = @"C:\CsharpTempFolder"; 
                // Using the supplied path 
                //DirectoryInfo directory = new DirectoryInfo(Directory.GetCurrentDirectory())
                DirectoryInfo directory = new DirectoryInfo(m_FolderPath);
                // Filter is applied to the directory.GetFiles method to get files of given extension
                // get FileInfos for the files in the current directory matching the filter expression
                FileInfo[] files = directory.GetFiles(filter);
                //list to hold the result
                ArrayList older = new ArrayList();  
                foreach (FileInfo file in files)
                {
                    //if smaller (older) than the relative time 
                    if (file.CreationTime < dt) 
                        older.Add(file.FullName); //add it to the list 
                }
                return older;
            }
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    For starters, never put blank catch blocks. They gobble up exceptions instead of handling them.

    Comment

    • Shani Aulakh
      New Member
      • Nov 2007
      • 44

      #3
      I just want to know what exceptions i can use and how...thats why i have catch block empty

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by Shani Aulakh
        I just want to know what exceptions i can use and how...thats why i have catch block empty
        Well, you could check the specs for the methods you are using to get all the possible checked exceptions that can be thrown. The other execptions depend on what your variables contain during execution.

        Comment

        • Shani Aulakh
          New Member
          • Nov 2007
          • 44

          #5
          okay. So, for exceptions..I can give out the same message
          for example
          Code:
          catch (FileNotFoundException e)
          {
          message = "Unable to delete " + filePath + "\r\n";
          message += String.Format("Reason: {0}", e.Message);
          MessageBox.Show(message);
          }
          catch (DirectoryNotFoundException e)
          {
          message = "Unable to delete " + filePath + "\r\n";
          message += String.Format("Reason: {0}", e.Message);
          MessageBox.Show(message);
          }
          catch (Exception e)
          {
          message = "Unable to delete " + filePath + "\r\n";
          message += String.Format("Reason: {0}", e.Message);
          MessageBox.Show(message);
          }

          Comment

          • Shashi Sadasivan
            Recognized Expert Top Contributor
            • Aug 2007
            • 1435

            #6
            If you want to show the same message for the three types of exception, then you can use the catch(Exception ex) stub only (ie the last exception block)

            Comment

            • r035198x
              MVP
              • Sep 2006
              • 13225

              #7
              Originally posted by Shashi Sadasivan
              If you want to show the same message for the three types of exception, then you can use the catch(Exception ex) stub only (ie the last exception block)
              Yep, the whole point of catching subclasses of Exception is to handle each one differently. If you are doing the same thing for all of them (and that is not good) then you can just catch the mother of them.

              Comment

              Working...