Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Exceptions are just that: a way to handle exceptional scenarios, scenarios which should not ordinarily happen in your application. Both examples provided are reasonable examples of how to use exceptions correctly. In both instances they are identifying that an action has been invoked which should not be allowed to take place and is exceptional to the normal flow of the application.</p> <p>The misinterpretation is that the second error, the renaming method, is the <strong>only</strong> mechanism to detect the renaming error. Exceptions should never be used as a mechanism for passing messages to a user interface. In this case, you would have some logic checking the name specified for the rename is valid somewhere in your UI validation. This validation would make it so that the exception would never be part of normal flow.</p> <p>Exceptions are there to stop "bad things" happening, they act as last-line-of-defence to your API calls to ensure that errors are stopped and that only legal behaviour can take place. They are programmer-level errors and should only ever indicate one of the following has occured:</p> <ul> <li>Something catastrophic and systemic has occurred, such as running out of memory.</li> <li>A programmer has gone and programmed something wrong, be it a bad call to a method or a piece of illegal SQL.</li> </ul> <p><strong>They are not supposed to be the only line of defence against user error.</strong> Users require vastly more feedback and care than an exception will ever offer, and will <em>routinely</em> attempt to do things which are outside of the expected flow of your applications.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. COI agree with you. That's the point of this post -- to point out that the second example is NOT the proper way to report the error to the UI. However, I disagree that a solution is to code the business rule right into the UI class itself. The UI class should call down into the business logic layer, the business logic layer performs the validation, and then the business logic layer reports back the status of the validtion to the UI. The UI handles the display of the error. The post is asking for ways of doing exactly that -- communicating errors between layers without exceptions.
      singulars
    2. COAn interesting concept, however I personally don't think asking your domain model to perform validation on your UI is a good idea. You will find that the validation requirements of your UI changes depending on the specific design of your UI. In the simplest example, you can have a free-text field or a drop-down-list, both requiring different validation. If the intent of putting your application layers is to separate concerns, asking layers to do UI-specific validation seems a bleeding together of those concerns. I would keep them entirely separate.
      singulars
    3. COI agree, in part. I believe that the UI layer should provide basic validation, but that the validation should be restricted to things like catching empty fields. All domain logic/validation should be removed from the UI. For example, if I have a "Last Name" search field, the UI tests for blank/numeric entry, but a seperate layer will do the search and inform UI layer that the name does not exist. This type of error should not be implemented as an exception. The UI should only make sure the input it sends fits the basic parameter requirements of the lower layer to which it sends the request.
      singulars
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload