When to use custom exception

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?

+3
source share
5 answers

, ArgumentNullException InvalidOperationException , . xyz 1, ArgumentException. , , , .

. , , , .

+1

, , , , , .. , PasswordTooWeakException ( InvalidArgumentException), -. , - , . , , - .

+1

, - ( , -). .

, catch, , .

0

, . @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

0

, , . , throw new ArgumentException("Basket not instantiated");

If in doubt, return to the MSDN Exception Rules .

0
source

Source: https://habr.com/ru/post/1785942/


All Articles