• 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 approach for exception handling in a core java application correct?

 
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 core java application with many try catch blocks. I am thinking of creating a custom exception and throwing in each and every catch block. Or should I create different custom exception messages for different catch blocks.?

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
I think the more interesting question is why do you have so many try-catch blocks?
 
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
whenever in the code it gave me a compile time error: e.g It should throw IO error, I enclosed code with try catch block. Now I am trying to refine the code.
 
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:whenever in the code it gave me a compile time error: e.g It should throw IO error, I enclosed code with try catch block.


Mistake. If you don't need to handle the exception, you don't catch the exception. You've likely got way too many try-catch blocks that aren't necessary.

Rule #1 of catching exceptions: don't
Rule #2 of catching exceptions: violate rule #1 only if you have something that needs to be done to handle the exception at that level
 
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

Rule #1 of catching exceptions: don't
Rule #2 of catching exceptions: violate rule #1 only if you have something that needs to be done to handle the exception at that level



So should I do the following:

Use throws clause for the method which throws exception
and
When I call this method from main method I can put try catch block.
and
in the catch block I can throw own custom exception giving some proper customized message e.g "the application has encountered and exception".

If the above is correct then should there be one custom exception for all exceptions or different ones.

thanks
 
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
I don't understand the need for custom exceptions at all.
 
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

I don't understand the need for custom exceptions at all.



So is that correct if I just remove the custom exception part and do the rest as I have written in last post? Should my catch block in main method have e.printStackTrace();? Mine is a core java application running from a scheduler.

thanks
 
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I don't understand the need for custom exceptions at all.



I think there is a big need for custom exceptions in certain situations. Low level exceptions leak information about an implementation which is often best to abstract away. For example when implementing an interface for a particular component one implementation might produce IOExceptions, and another might produce completely different types of exceptions. The calling layer probably only cares that something failed, so the interface declares that it throws some custom exception (for example a ConfigurationException if this is a configuration module).

 
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. I followed few steps to bring my application to follow the coding standards. Some of those were:

-->Removed all try catch blocks from methods and replaced by throws clause. I am catching the exceptions only in main method.

-> In the catch block I am replacing e.printStacktrace by log.info. I am not clear that from the catch block is it better to have log.info

with some info Or is it better to throw your own exceptions from here.

-> For logging I am writing logs to a folder. I am thinking what should be the location of these logs folder (for a core java

application).

thanks
 
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:-->Removed all try catch blocks from methods and replaced by throws clause. I am catching the exceptions only in main method.


Remember that there may be good reasons to catch an exception at a lower level, but to only catch them if you have something constructive and concrete to do (or simply to avoid compiler errors). You should never catch an exception and then just rethrow it higher. That's exactly what the throws clause is for.

You should also never* catch an exception and do nothing.

* Unless you really know what you are doing and have a really good reason for doing so. And when you do, be sure to add a comment as to why.

-> In the catch block I am replacing e.printStacktrace by log.info. I am not clear that from the catch block is it better to have log.info
with some info Or is it better to throw your own exceptions from here.


How you present errors to your user audience depends upon that audience. Showing an end user a stack trace is almost never an appropriate action.

-> For logging I am writing logs to a folder. I am thinking what should be the location of these logs folder


That entirely depends upon the needs of the application.
 
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

How you present errors to your user audience depends upon that audience. Showing an end user a stack trace is almost never an appropriate action.



The application is a core java application running through a scheduler. It will populate records in database.

That entirely depends upon the needs of the application.



Suppose the log folder grows to a very big size that it can no more write to that folder. Will the application stop working in that case for this reason or will simple just not write logs?

thanks
 
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:

How you present errors to your user audience depends upon that audience. Showing an end user a stack trace is almost never an appropriate action.



The application is a core java application running through a scheduler. It will populate records in database.


That doesn't address the issue. Who is the audience of this application? Who will be looking at the errors? What do they need to see?

Suppose the log folder grows to a very big size that it can no more write to that folder.


Investigate the use of a logger such as Log4J and "rolling" appenders.

Also, carefully inspect your logging to make sure that what you are logging is appropriate. Producing reams of useless logging doesn't do anybody any good.
 
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

That doesn't address the issue. Who is the audience of this application? Who will be looking at the errors? What do they need to see?



This program will just populate data in database. Another program (not in java) will be displaying data to the user through reporting but that is some separate program.

Investigate the use of a logger such as Log4J and "rolling" appenders.

Also, carefully inspect your logging to make sure that what you are logging is appropriate. Producing reams of useless logging doesn't do anybody any good.



I am using log4j. Suppose for some reason it is not able to write to that log file.So will the whole program fail to execute in that case or only the log wont be captured?

 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Monica. Shiralkar wrote:
This program will just populate data in database. Another program (not in java) will be displaying data to the user through reporting but that is some separate program.



This still doesn't answer Bear's question though. Are the people that will read the logs (however they get displayed) technical people, or non-technical people. What do they need to know about the error? What will they do when they see the error? Do they need a full stack trace, or just an overview of the problem? All of these questions need an answer before you can decide what needs to be logged.


Monica. Shiralkar wrote:I am using log4j. Suppose for some reason it is not able to write to that log file.So will the whole program fail to execute in that case or only the log wont be captured?



I believe log4j will be resillient to situations where it fails to log. However you still need to come up with a proper logging strategy. What happens to the logs when they are made? Do you have a monitoring system like splunk, and if so how will it get hold of the logs? How often are you going to rotate the logs, and what happens to the logs when they are rotated? (Note that the log4j configuration file will let you configure log rotation if you need it).

Also carefully consider the log level you're using. Are you only going to log exceptions (in which case you probably want to log at WARNING or ERROR level)? Are you going to include debug logging to help you when you're developing? These types of logs will obviously go in at DEBUG level. Is there any information you need to log at INFO level (maybe meaningful actions that your program is taking that someone needs to be aware of)?
 
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

This still doesn't answer Bear's question though. Are the people that will read the logs (however they get displayed) technical people, or non-technical people. What do they need to know about the error? What will they do when they see and error? Do they need a full stack trace, or just an overview of the problem? All of these questions need an answer before you can decide what needs to be logged



If there is some issue with the data being not populated in database, they will ask me to check why my program did not function properly.At that time I will have to check the logs. No one has asked me to implement logs but I think I would require logs for this reason.
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Monica. Shiralkar wrote:

This still doesn't answer Bear's question though. Are the people that will read the logs (however they get displayed) technical people, or non-technical people. What do they need to know about the error? What will they do when they see and error? Do they need a full stack trace, or just an overview of the problem? All of these questions need an answer before you can decide what needs to be logged



If there is some issue with the data being not populated in database, they will ask me to check why my program did not function properly.At that time I will have to check the logs. No one has asked me to implement logs but I think I would require logs for this reason.



So the logs are only for you (or other technical people) to debug when things go wrong. In that case I would advise including the full stack trace in the logs. The standard log4j calls allow you to pass an exception after your log message, which will ensure you the stack trace is in the logs.

Also be aware that it's possible your program will stop doing what you expect it to without throwing an Exception, so make sure you have a good amount of debug logging in place so you can track what your program is doing.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic