• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Is this a correct way of handling exceptions in the application?

 
Ranch Hand
Posts: 2925
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a REST webservice application which has multiple layers. The controller class calls the other layers. In each of the layers for every checked exception which the compiler forces to catch I catch the exception and throw my own custom exception with message "The application has encountered and exception and the stack trace is". Also I log the exception. My controller class does not force me to catch my custom exception, so I put the entire code of controller method in try catch block and catch my custom exception and log the custom exception.
Is this a correct way of handlin excepitons?

thanks
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sounds way too complicated and contorted to me. Main line code in a catch block? Why? And why catch the exceptions at every level?
 
Rancher
Posts: 1171
18
IntelliJ IDE Hibernate Firefox Browser MySQL Database Spring Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't think it's bad to log your exeptions. But normally you wouldn't catch them in each layer, you add the trows exeption to the method call, and keep thowing them till they reach the endpoint where you catch them.
I also agree that it's bad practise to put your entire code in a try block, use the exeptions the idea tells you about, in the catch block yu still can throw your custom exeption.
Finally, its good you warn your user that something went wrong, but I would describe it with my own words, don't include the stacktrace, that only complicates things since you already log them.
 
Monica Shiralkar
Ranch Hand
Posts: 2925
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If I should not catch exception in every layer and use throws, then how should I throw a custom exception for my application?

 
Daniel Demesmaecker
Rancher
Posts: 1171
18
IntelliJ IDE Hibernate Firefox Browser MySQL Database Spring Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The exception has to be catched at some point (read the first layer) there you can trow your custom exception in the catch block.
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Monica Shiralkar wrote:If I should not catch exception in every layer and use throws, then how should I throw a custom exception for my application?


I dont understand what's stopping you? If it's a check exception, simply declare it in the throws clauses. If it's unchecked, you don't even need to do that.
 
Monica Shiralkar
Ranch Hand
Posts: 2925
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks all. Based on the suggestions,I think I should not catch exceptions in other layers and just use throws clause for checked exceptions. In the first layer, I should catch the exception and throw my custom exception with that message something like this:



Is this correct?
 
Daniel Demesmaecker
Rancher
Posts: 1171
18
IntelliJ IDE Hibernate Firefox Browser MySQL Database Spring Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why are you putting your customException in a try block?
 
Saloon Keeper
Posts: 15485
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Logging exceptions AND throwing new exceptions doesn't make sense. You can do two things:

1) A method is allowed to throw a certain (possibly custom) exception. Just propagate exceptions of that type, or catch and wrap and rethrow exceptions of different types, if that makes sense.
2) In all other cases, make the exception message, and possibly the stack trace visible to the user. That can be done with a popup message, or with a log file, or whatever else you can think of.

There's no golden rule. It depends on what makes conceptual sense for your method contract.

In general, IOException is one of those exceptions that you can just propagate upwards until you reach a calling method that doesn't clearly do anything with I/O. Most of the time, that's an event handler for a button you clicked on. From there, you can just show a popup with the exception message.

Show us the signature of your method, and tell us what the method is supposed to do.
 
Daniel Demesmaecker
Rancher
Posts: 1171
18
IntelliJ IDE Hibernate Firefox Browser MySQL Database Spring Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:Logging exceptions AND throwing new exceptions doesn't make sense. .



Why not? In the customexception she trows she can warn the user with a simple message(althought she could do that without trowing a new one), while the logfile contains the stacktrace for the person that maintains the program...
 
Rancher
Posts: 1044
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Daniel Demesmaecker wrote:

Stephan van Hulst wrote:Logging exceptions AND throwing new exceptions doesn't make sense. .



Why not? In the customexception she trows she can warn the user with a simple message(althought she could do that without trowing a new one), while the logfile contains the stacktrace for the person that maintains the program...



Well, if you do this in every layer, then the situation can become confuse. It is however (usually) possible to create an exception with another as a cause. But to cope with a chaining coming from several layers might become tedious. An exception should be caught and coped with where it is meaningful. For example if a string is given to some method which in turn gives it further to other methods and at the end a File is made out of it and finally some IO operation is tried against the File, but fails, the resulting exception should arguably percolated to the place, where the string was originally provided, and should be coped with there.

A Happy New Year btw!
 
Stephan van Hulst
Saloon Keeper
Posts: 15485
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's fine, but then why not log the exception in the same place you display the message?

I think every exception should have one place where they're handled. If you log an exception and then rethrow it, there's a big chance it's going to be logged twice, needlessly blowing up your logging file.
 
Daniel Demesmaecker
Rancher
Posts: 1171
18
IntelliJ IDE Hibernate Firefox Browser MySQL Database Spring Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ivan Jozsef Balazs wrote:

Daniel Demesmaecker wrote:

Stephan van Hulst wrote:Logging exceptions AND throwing new exceptions doesn't make sense. .



Why not? In the customexception she trows she can warn the user with a simple message(althought she could do that without trowing a new one), while the logfile contains the stacktrace for the person that maintains the program...



Well, if you do this in every layer, then the situation can become confuse. It is however (usually) possible to create an exception with another as a cause. But to cope with a chaining coming from several layers might become tedious. An exception should be caught and coped with where it is meaningful. For example if a string is given to some method which in turn gives it further to other methods and at the end a File is made out of it and finally some IO operation is tried against the File, but fails, the resulting exception should arguably percolated to the place, where the string was originally provided, and should be coped with there.

A Happy New Year btw!



Totally beside the point, if you would have red the start of the post you would see, I've said the exact same thing.
 
Daniel Demesmaecker
Rancher
Posts: 1171
18
IntelliJ IDE Hibernate Firefox Browser MySQL Database Spring Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:That's fine, but then why not log the exception in the same place you display the message?

I think every exception should have one place where they're handled. If you log an exception and then rethrow it, there's a big chance it's going to be logged twice, needlessly blowing up your logging file.



You're right there, Stephan.
 
Monica Shiralkar
Ranch Hand
Posts: 2925
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Why are you putting your customException in a try block?



That is because if I do not do so , it will give compile error for the custom exception not caught or thrown.
 
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, if you're going to throw an Exception, you have to mark your method as as such:
 
Monica Shiralkar
Ranch Hand
Posts: 2925
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks but this is in the front layer. Am I not supposed to catch it here rather than throwing it? If I put in throws clause , I can catch it later.But this is my front layer so I need to catch it here.
 
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If that's the top layer of your app, and you don't want the exception to get out of your app, then you need to handle the exception at that point and not wrap it.

For a REST app this would presumably involve returning a suitable error code and user-useful message.
 
Stephan van Hulst
Saloon Keeper
Posts: 15485
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Even for REST applications, the controller is not the correct place to make sure no exception leave your method calls.

Throw suitable exception types from your methods, even from controller actions. To make sure exceptions get rewritten to JSON messages or something similar, you need to add a piece of middleware to your request pipeline that handles exceptions. Most MVC frameworks provide such a handler that you can edit.
 
Monica Shiralkar
Ranch Hand
Posts: 2925
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What correction should I do in the below code:






 
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This code here:



is basically equivalent to the shorter and easier to understand code here:



It's true that it doesn't log the text "The application has encountered an exception" but then since you're logging an exception, that should already be obvious.

So now you have



Since you're doing the same thing in both catch blocks you can simplify that some more:


 
Monica Shiralkar
Ranch Hand
Posts: 2925
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks. So how can I throw Custom exception in this code?
 
Paul Clapham
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why would you want to do that? The code doesn't call for using a custom exception. Your original code had 14 lines of code where only 5 were necessary, plus a whole unnecessary class.
 
Monica Shiralkar
Ranch Hand
Posts: 2925
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok. If I want to show a custom message instead of the exception, then how should I do it?
 
Paul Clapham
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Although in real life you wouldn't want to do that because you should be logging the exception there.
 
Monica Shiralkar
Ranch Hand
Posts: 2925
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not for logging custom message but for showing custom message.E.g for cases of showing a custom message on the web UI?
 
Paul Clapham
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Monica Shiralkar wrote:Not for logging custom message but for showing custom message.E.g for cases of showing a custom message on the web UI?



That's an entirely different thing.

If you want to put something into the web response, for example, then you're going to have some pieces of code which are responsible for doing that. And you should use that code accordingly. Generalized discussions about the use of customized exceptions have really nothing to do with that and they are basically unhelpful. Instead you should start with working examples of the code used for your particular web UI and write other code based on them.
 
Monica Shiralkar
Ranch Hand
Posts: 2925
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When should exception be wrapped around Custom exception and when should it not be ? (as I was attempting to do)
 
Paul Clapham
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You should use a custom exception when you need to do that. How do you know when you need to do that? You look at the design of your application and it should suggest whether you need to do it or not.

So... It Depends.

Your desire for absolute rules independent of context is really not useful. There aren't that many such rules. You should really concentrate on learning things the other way around: look at an actual application and find out why it makes you do one thing or another.
 
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:Since you're doing the same thing in both catch blocks you can simplify that some more:


Nitpick alert!

Actually this code won't compile as FileNotFoundException is a subclass of IOException.
So this can and should be shortened to:
 
reply
    Bookmark Topic Watch Topic
  • New Topic