This week's book giveaway is in the Artificial Intelligence and Machine Learning forum.
We're giving away four copies of Zero to AI - A non-technical, hype-free guide to prospering in the AI era and have Nicolò Valigi and Gianluca Mauro on-line!
See this thread for details.
Win a copy of Zero to AI - A non-technical, hype-free guide to prospering in the AI era this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

Doubts about throwing checked or unchecked exception

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,

I have to ask you a question about the decision to throw checked or unchecked exceptions in a multilayer application.

Let's say that I've the following structure:

- Controller class that will call the Service layer and handle exceptions, returning a body response to the Client.
- Service class, will implement the business logic.

The Service class impements a method to retrieve the User data from a database, a simple findById().
In case of entity not found I would throw a custom exception, let's say UserNotFoundException, that the Controller class will handle, building a proper response to the Client.
The UserService class is called only but the UserController, no other Controller or Service makes use of it.

The question is, should the UserNotFoundException implemented as an unchecked (runtime) or checked exception (force the Controllare layer to handle it)?

Generally, which kind of rule should I follow to decide how to implement an exception handling process?

Thanks for your help.
 
Bartender
Posts: 1282
39
IBM DB2 Netbeans IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have a look at Java Documentation. In a nuthshell, you should use unchecked exception when dealing with unrecoverable errors (a stack overflow, a connection to a DBMS dropped).
My personal taste: use checked exception as much as possibile in your business layer.
 
Kirk James
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Claude Moore wrote:Have a look at Java Documentation. In a nuthshell, you should use unchecked exception when dealing with unrecoverable errors (a stack overflow, a connection to a DBMS dropped).
My personal taste: use checked exception as much as possibile in your business layer.



Actually I want to recover from every exception by catching it and building a proper body for the Client side of the application.

I mean, when I think about a recoverable error I suppose that somewhere in my code the exception is catched and nothing will be rethrowed back from it.

 
Marshal
Posts: 70602
287
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think it isn't so much a case of where the exception comes from, as what you are going to do with it if it is thrown. If you can think of a reasonable action to take if an XYZException is thrown then make it checked.
If you think, if an ABCExcption is thrown, there is little we can do, and maybe we have to change the code to prevent such an exception, then unchecked.
Agree with Claude that checked exceptions are often better.
 
Master Rancher
Posts: 4698
49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Kirk James wrote:
I mean, when I think about a recoverable error I suppose that somewhere in my code the exception is catched and nothing will be rethrowed back from it.



That's not a recoverable error.
That's just a way of reporting an error.
 
Kirk James
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your opinion, so, about the scenario I showed previously, do you think that a checked exception could be a better solution?

Supposing that UserNotFoundException is an UncheckedException, I will not force the external layer (Controller) to catch the exception and build a specific body for the Client, in that case so it will be handled as a generic Exception (500) instead of a not found (404)

Snippet edit:

 
Marshal
Posts: 25930
69
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Kirk James wrote:Thank you for your opinion, so, about the scenario I showed previously, do you think that a checked exception could be a better solution?



The general rule is: If you expect that something in your application will be able to do something useful when the exception is thrown, then it should be a checked exception. Whereas if you don't expect anything in the application will be able to deal with the exception, and therefore the application should end instead, then it should be unchecked. If it's a web app then yes, generally a checked exception allows your application to notify the client in application-specific terms and return a 404, and an unchecked exception allows your application to automatically do nothing about it and allow the server to return a 500.

Note that "something useful" could even include "log the exception and continue with no other action". Or even "continue with no other action". That all depends on the design of the application.

 
Campbell Ritchie
Marshal
Posts: 70602
287
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Paul Clapham wrote:. . . That all depends on the design of the application.

Agree: often the hardest part of using exceptions is deciding what to do if one is thrown.

KJ: Please don't try putting BB tags inside code tags: they don't work. HTML tags might work in documentation comments or if you want to specify a particular character with a tag. Otherwise avoid them too.
 
Saloon Keeper
Posts: 22634
153
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In HTTP(S), 500-level response codes should not be used for things like invalid data. A 500 is supposed to indicate that an app function failed unexpectedly - a bug or a problem with request routing.

If a user submits a bad request, the response should generally be a 400-series code. For example, 400 (Bad Request), 404 (Not Found - can mean data not found as well as web page not found).

Response codes are really not fine-grained and not the best way to communicate with users. They're mostly for directing client applications rather than notifying humans - who should be presented with specific error messages.
 
Paul Clapham
Marshal
Posts: 25930
69
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tim Holloway wrote:In HTTP(S), 500-level response codes should not be used for things like invalid data. A 500 is supposed to indicate that an app function failed unexpectedly - a bug or a problem with request routing.



And with respect to your question, this means that your code should not throw an unchecked exception. Not unless you plan to catch it at a higher level and deal with it appropriately, that is. In which case you might just as well throw a checked exception.
 
Kirk James
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your help guys
 
He's my best friend. Not yours. Mine. You can have this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic