Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. exception handling in class libraries

exception handling in class libraries

Scheduled Pinned Locked Moved ASP.NET
designquestiondiscussion
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    Taurian110
    wrote on last edited by
    #1

    When designing classes is it a best practice to do exception handling within class or should it be taken care of at the UI level? In other words if my class is in ClassLibrary project should I check and throw exceptions there like this:

    public MyClass
    {
      public MyClass(){...}
      GetSomeDataById(string id)
      {
        try
           {
              // Open connection and all that good stuff
              ...
           }
         Catch
           { /* Catch*/ }
         finallay
           {
             /* close connection */ 
           }
      }
    }
    --------------------------------------------------------------
    // in .aspx file
     MyClass myClass = new MyClass("whatever");
       string someData = myClass.GetSomeDataGyId("10");
       if(String.IsNullOrEmply(someData)
       {
          // Some code
       }
    

    or should I do something like this:

    public MyClass
    {
      public MyClass(){...}
      GetSomeDataById(string id)
      {
           // Open connection and all that good stuff 
           ....
           // close connection
      }
    }
    
    -------------------------------------------------
    //in aspx file
    try
    {
       MyClass myClass = new MyClass("whatever");
       string someData = myClass.GetSomeDataGyId("10");
       if(String.IsNullOrEmply(someData)
       {
          // Some code
       }
    }
    catch Exception(e)
    {
        // Call some function to display this message
    }
    
    N 1 Reply Last reply
    0
    • T Taurian110

      When designing classes is it a best practice to do exception handling within class or should it be taken care of at the UI level? In other words if my class is in ClassLibrary project should I check and throw exceptions there like this:

      public MyClass
      {
        public MyClass(){...}
        GetSomeDataById(string id)
        {
          try
             {
                // Open connection and all that good stuff
                ...
             }
           Catch
             { /* Catch*/ }
           finallay
             {
               /* close connection */ 
             }
        }
      }
      --------------------------------------------------------------
      // in .aspx file
       MyClass myClass = new MyClass("whatever");
         string someData = myClass.GetSomeDataGyId("10");
         if(String.IsNullOrEmply(someData)
         {
            // Some code
         }
      

      or should I do something like this:

      public MyClass
      {
        public MyClass(){...}
        GetSomeDataById(string id)
        {
             // Open connection and all that good stuff 
             ....
             // close connection
        }
      }
      
      -------------------------------------------------
      //in aspx file
      try
      {
         MyClass myClass = new MyClass("whatever");
         string someData = myClass.GetSomeDataGyId("10");
         if(String.IsNullOrEmply(someData)
         {
            // Some code
         }
      }
      catch Exception(e)
      {
          // Call some function to display this message
      }
      
      N Offline
      N Offline
      Neeraj Arora
      wrote on last edited by
      #2

      If you are using the class from more than one location and the messages might be different for different locations then you should throw the exception from the class. You should log the exact location of exception in log file so that you must know the origin of the exception. I hope it will solve your problem. Good luck.

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups