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. General Programming
  3. C#
  4. Exception throwing - Advice required

Exception throwing - Advice required

Scheduled Pinned Locked Moved C#
helpquestion
6 Posts 6 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.
  • I Offline
    I Offline
    Imtiaz Murtaza
    wrote on last edited by
    #1

    Friends, Quite frequently i need to throw exception from the code. But i don't want to write my custom exception class. Reason is that i don't have any dynamic data to be associated. In such situation, currently i am doing something like this: skyColor = GetSkyColor(); if(skyColor == Blue) { Console.WriteLine("Its a day"); } else if(skyColor == Black) { Console.WriteLine("It is night"); } else { msg = String.Format("{0} is not a valid sky color", skyColor.ToString() ); throw new Exception(msg); } As you can see above i am throwing "Exception" which is considered as bad practice. The second option is that i use InvalidOperationException, but this exception is used when object state is not valid which is not the case here. Third option is that i write my own exception class InvalidSkyColorException. The case i described above is well qualified for custom exception as i can associate dynamic data SkyColor. But issue is that in most of the cases i don't even have dynamic data to associate. I just want to throw a string message. Now if i start writing my custom exception class for each of the situation, then there will be thousands of such classes in the project. So what you guys suggest me in such cases. Shall i continue to throw generic "Exception" or use InvalidOperationException ? Or is there better solution ?

    Imtiaz

    K V B N 4 Replies Last reply
    0
    • I Imtiaz Murtaza

      Friends, Quite frequently i need to throw exception from the code. But i don't want to write my custom exception class. Reason is that i don't have any dynamic data to be associated. In such situation, currently i am doing something like this: skyColor = GetSkyColor(); if(skyColor == Blue) { Console.WriteLine("Its a day"); } else if(skyColor == Black) { Console.WriteLine("It is night"); } else { msg = String.Format("{0} is not a valid sky color", skyColor.ToString() ); throw new Exception(msg); } As you can see above i am throwing "Exception" which is considered as bad practice. The second option is that i use InvalidOperationException, but this exception is used when object state is not valid which is not the case here. Third option is that i write my own exception class InvalidSkyColorException. The case i described above is well qualified for custom exception as i can associate dynamic data SkyColor. But issue is that in most of the cases i don't even have dynamic data to associate. I just want to throw a string message. Now if i start writing my custom exception class for each of the situation, then there will be thousands of such classes in the project. So what you guys suggest me in such cases. Shall i continue to throw generic "Exception" or use InvalidOperationException ? Or is there better solution ?

      Imtiaz

      K Offline
      K Offline
      Karmendra Suthar
      wrote on last edited by
      #2

      You can alternatively create a custom exception class using ApplicationException class[^] For more on Exception Handling Best Practices in .NET

      N 1 Reply Last reply
      0
      • I Imtiaz Murtaza

        Friends, Quite frequently i need to throw exception from the code. But i don't want to write my custom exception class. Reason is that i don't have any dynamic data to be associated. In such situation, currently i am doing something like this: skyColor = GetSkyColor(); if(skyColor == Blue) { Console.WriteLine("Its a day"); } else if(skyColor == Black) { Console.WriteLine("It is night"); } else { msg = String.Format("{0} is not a valid sky color", skyColor.ToString() ); throw new Exception(msg); } As you can see above i am throwing "Exception" which is considered as bad practice. The second option is that i use InvalidOperationException, but this exception is used when object state is not valid which is not the case here. Third option is that i write my own exception class InvalidSkyColorException. The case i described above is well qualified for custom exception as i can associate dynamic data SkyColor. But issue is that in most of the cases i don't even have dynamic data to associate. I just want to throw a string message. Now if i start writing my custom exception class for each of the situation, then there will be thousands of such classes in the project. So what you guys suggest me in such cases. Shall i continue to throw generic "Exception" or use InvalidOperationException ? Or is there better solution ?

        Imtiaz

        V Offline
        V Offline
        Vikram A Punathambekar
        wrote on last edited by
        #3

        Imtiaz Murtaza wrote:

        So what you guys suggest me in such cases. Shall i continue to throw generic "Exception" or use InvalidOperationException ? Or is there better solution ?

        Thumb rule, do not follow blindly: If the calling code needs to perform a special function on encountering this specific error situation, use a custom exception class. If you are simply logging the message, displaying a dialog to the user, etc, and the only thing that changes is the error message, use System.Exception.

        Cheers, Vıkram.


        I've never ever worked anywhere where there has not been someone who given the choice I would not work with again. It's a job, you do your work, put up with the people you don't like, accept there are probably people there that don't like you a lot, and look forward to the weekends.   - Josh Gray.

        1 Reply Last reply
        0
        • I Imtiaz Murtaza

          Friends, Quite frequently i need to throw exception from the code. But i don't want to write my custom exception class. Reason is that i don't have any dynamic data to be associated. In such situation, currently i am doing something like this: skyColor = GetSkyColor(); if(skyColor == Blue) { Console.WriteLine("Its a day"); } else if(skyColor == Black) { Console.WriteLine("It is night"); } else { msg = String.Format("{0} is not a valid sky color", skyColor.ToString() ); throw new Exception(msg); } As you can see above i am throwing "Exception" which is considered as bad practice. The second option is that i use InvalidOperationException, but this exception is used when object state is not valid which is not the case here. Third option is that i write my own exception class InvalidSkyColorException. The case i described above is well qualified for custom exception as i can associate dynamic data SkyColor. But issue is that in most of the cases i don't even have dynamic data to associate. I just want to throw a string message. Now if i start writing my custom exception class for each of the situation, then there will be thousands of such classes in the project. So what you guys suggest me in such cases. Shall i continue to throw generic "Exception" or use InvalidOperationException ? Or is there better solution ?

          Imtiaz

          B Offline
          B Offline
          Bharat Jain
          wrote on last edited by
          #4

          Hi Imtiaz, My suggestion would be , you can group up the types of exception in our applicaion and create a custom exception class for each group , for Example all the exception related to color can be grouped into an exception class called 'InValidColorChoiceExpection' than it does not matter if it is a sky color or any other exception related to color , you may not create a individual exception class for each exception, the point here to to properly Categorize the exceptions , in some case you may use the inbuilt .net Exception classes , for example if there is invalid Operation performed , you may not require to Create something like 'CustomInValidOpreation' , there is already an exception for it. So in short, group you exception , try to fit it in a group that already exist , if not then only go for a new custom exception class. I hope i am understandable :)

          -Regards Bharat Jain bharat.jain.nagpur@gmail.com

          1 Reply Last reply
          0
          • K Karmendra Suthar

            You can alternatively create a custom exception class using ApplicationException class[^] For more on Exception Handling Best Practices in .NET

            N Offline
            N Offline
            N a v a n e e t h
            wrote on last edited by
            #5

            KSuthar wrote:

            custom exception class using ApplicationException class[

            Don't derive from ApplicationException class. Derive it from Exception instead. See the remarks section here[^].

            Navaneeth How to use google | Ask smart questions

            1 Reply Last reply
            0
            • I Imtiaz Murtaza

              Friends, Quite frequently i need to throw exception from the code. But i don't want to write my custom exception class. Reason is that i don't have any dynamic data to be associated. In such situation, currently i am doing something like this: skyColor = GetSkyColor(); if(skyColor == Blue) { Console.WriteLine("Its a day"); } else if(skyColor == Black) { Console.WriteLine("It is night"); } else { msg = String.Format("{0} is not a valid sky color", skyColor.ToString() ); throw new Exception(msg); } As you can see above i am throwing "Exception" which is considered as bad practice. The second option is that i use InvalidOperationException, but this exception is used when object state is not valid which is not the case here. Third option is that i write my own exception class InvalidSkyColorException. The case i described above is well qualified for custom exception as i can associate dynamic data SkyColor. But issue is that in most of the cases i don't even have dynamic data to associate. I just want to throw a string message. Now if i start writing my custom exception class for each of the situation, then there will be thousands of such classes in the project. So what you guys suggest me in such cases. Shall i continue to throw generic "Exception" or use InvalidOperationException ? Or is there better solution ?

              Imtiaz

              N Offline
              N Offline
              Najmal
              wrote on last edited by
              #6

              hiii In this Case u can use Exception()...

              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