Exception throwing - Advice required
-
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
-
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
You can alternatively create a custom exception class using ApplicationException class[^] For more on Exception Handling Best Practices in .NET
-
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
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.
-
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
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
-
You can alternatively create a custom exception class using ApplicationException class[^] For more on Exception Handling Best Practices in .NET
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
-
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