0

I have a question regarding whether exception classes should contain business rules, for example, supposing I have several exception methods, and in these exception methods I end up populating entity information to be saved in the database. For example

public class CustomException : Exception
{

    public CustomException(IActionResult httpResult, string? taskType = null, string? accessControlExposeHeaders = null)
    {
        HttpResult = httpResult;

        if (taskType != null)
            Data["some-header"] = taskType;
        if (accessControlExposeHeaders != null)
            Data["Access-Control-Expose-Headers"] = accessControlExposeHeaders;
    }
    
    internal static CustomException ThrowCustomException()
    {
        // Here I make manipulations of the entity to be saved in the database, for example, error status, status code to be saved and so on.
    }
}

Or would it be better to create a validation class, and in this class, do the necessary manipulations, and then call the exception class just by passing the necessary constructor?

I've read the links below, but I don't quite understand them

Should a business rule violation throw an exception?

Business Rule Violations and Exceptions

1
  • 4
    I think this is conflating even several different issues. It's one thing to use (or abuse) Exceptions in the first place. But given, you do use them, I do not see any reason why they should have knowledge about how to mutate any database entity. I have never used them nor have them seen used other than purely informational. Commented Mar 19 at 13:31

2 Answers 2

3

Short answer: No.

Longer answer: Unless you have a very good reason, I argue you probably shouldn't even use custom exceptions in the first place, unless you need the exception to carry some extra information compared to the already built-in exceptions the framework provides.

Exceptions should be used for things that you can't control in code, and handling exceptions should be minimal - log them, notify the user something went wrong, and depending on the severity of the exception, decide if the program can continue to run or not.

As a rule of thumb - exceptions should be used for exceptional circumstances, not for flow-control or short-circuiting exit routs.

Oh, and of course, the Must read - Vexing exceptions on FAIC by Eric Lippert

Sign up to request clarification or add additional context in comments.

2 Comments

Would you recommend the Result pattern and only use exceptions in extreme cases?
It's not so much extreme as exceptional, but yes, I would recommend using the Result pattern in places where you can't (or wont) inject a logger. In fact, I've blogged about it back in 2021
0

According to this answer: https://stackoverflow.com/a/537831/2773515, on your first linked question on your post, I think the best practices that you mentioned is clarified there, so, IMO, no, exceptions didn't manipulating anything based on exception != error "directive". Your validation class will be responsible to check and manipulate entity throwed by your custom exception which is responsible to alert your application that one business rule was violated.

Maybe you should consider not returning data at all on custom exception nor throwing an exception at all, and your validation class returning custom class with manipuated entity and an false state result.

2 Comments

Would you recommend the Result pattern and only use exceptions in extreme cases?
@VagnerWentz, normally I use Result pattern to return business rules violations and Exception to catch any other non planned or unhandled errors.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.