Throw the same Exception if it is not processed, or create a new exception?

Paragraph 6 in this article states:

Try not to throw an exception because of the price. Boo, if rethrowing was mandatory, repeat throwing the same exception instead of creating a new exception. This will bring extra performance. You can add additional information at each level to this exception.

Well, but that violates the separation of layers, right?

Let's say I have a specific DAO implementation that throws a SQLException

Say my service level (or business level ..) calls a method from the DAO layer, but decides not to handle the thrown exception.

If I re-throw SQLException at the view level, my view layer will not only be associated with the DAO Layer, will it?

Is it wrong to throw a new exception, so that the view depends only on a level lower than two?

What benefits does the same exception throw apart from performance?

+5
source share
2 answers

If I re-throw SQLException on the view level, my view layer will not only be associated with the DAO layer, will it?

This is absolutely correct.

Is it wrong to throw a new exception, so the view only depends on the level of the level below, and not two?

That's right. If your DAO level cannot handle the exception coming from SQL, it should catch it, make as much sense as possible and throw its own exception.

Consider an example: let your DAO layer allow you to add new elements where a specific attribute must be unique. The SQL level can have a unique constraint or a unique index to enforce this constraint at the RDBMS level. If the caller of your DAO layer tries to save an object that violates the uniqueness constraint, an SQL exception will be thrown. If you allow this exception to apply to callers, most likely they will not know what to do with it, and perhaps even show them to end users:

ORA-00001: unique constraint (UxPatient_rec_soc) violated

This solution is very bad, but the alternative to try and understand it on the client is even more fragile.

Your DAO should catch the exception, decide what it means to the caller, and throw its own exception. This way you can change your implementation regardless of your caller.

General Note: When considering throwing / re-throwing exceptions, performance considerations should be considered, since exceptions should only be thrown in rare exceptional situations. The clarity of your interfaces is much more important.

+5
source

I agree with you that it would be more convenient to describe the exception for your layer wrapping the original. SQLException in this case may be a low-level problem that can be interpreted and placed in a business context, causing a new, more descriptive exception.

Also keep in mind that usually the execution of error handling itself is not what you would like to optimize for. So it’s true that throwing an exception is slower than returning from the method, but this only happens when something goes wrong. (Because you should avoid using exceptions to handle regular program threads)

I see no advantage in throwing the same exception if you really cannot add any information by wrapping it. In this case, do not do this.

+3
source

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


All Articles