Possible duplicate:Create your own exception or use inline exceptions?
Hi,
In program development, is it ok to model exceptions for business constraints? For instance. if xyz must be> 1 to get abc (the cart object must exist before the objects can be added) and the cart does not exist, is this reason enough to create a custom exception to model this real scenario
What reasons contribute to the use of custom exceptions?
, ArgumentNullException InvalidOperationException , . xyz 1, ArgumentException. , , , .
ArgumentNullException
InvalidOperationException
ArgumentException
. , , , .
, , , , , .. , PasswordTooWeakException ( InvalidArgumentException), -. , - , . , , - .
, - ( , -). .
, catch, , .
, . @Anthony ArgumentException. Exception, .
. , :
catch(ArgumentException e) { if(e.Message.Equals("The argument was bigger than 0")) // do something else // do something else }
, , , .
, : http://blogs.msdn.com/b/jaredpar/archive/2008/10/20/custom-exceptions-when-should-you-create-them.aspx
, , . , throw new ArgumentException("Basket not instantiated");
throw new ArgumentException("Basket not instantiated");
If in doubt, return to the MSDN Exception Rules .
Source: https://habr.com/ru/post/1785942/More articles:If I overloaded the + and = operators, do I need to overload + =? - c ++array indices - fortranDo not show status icons in Windows Explorer. - windows-7How to create inner children without creating another class? - javaКак изменить весь элемент с уже существующим значением с помощью xslt? - xmlНастройка Excel FitToPagesWide на "Автоматический" в С# (VTSO) - c#How to get a button handle of a third-party application using C #? - c #How can I make the before_filter function behave differently according to the action? - authenticationКакую версию python можно использовать с Twisted/Zope? - pythonhow to achieve unique results - sqlAll Articles