This week's book giveaway is in the Java in General forum.
We're giving away four copies of Event Streams in Action and have Alexander Dean & Valentin Crettaz on-line!
See this thread for details.
Win a copy of Event Streams in Action this week in the Java in General 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
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

After completing the writing of code should I include the entire code in a try and catch block ?

 
Ranch Hand
Posts: 1131
4
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a code where a myProcess() method is responsible for entire functionality. Should I enclose this in a try catch block as below:




thanks
 
Ranch Hand
Posts: 1338
3
Netbeans IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Independently of the try-catch issue.

myProcess() is a method so it should be preceded of an instance of the class in which it is implemented.

Ex;




I assume this was already clear to you, isn't it?

On the other hand, the try-catch looks ok to me, just the format isn't.
 
Bartender
Posts: 708
10
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do you think the call to myPrpocess() should be enclosed in a try-catch?
Does that method throw an exsception?
 
Marshal
Posts: 65053
247
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Monica Shiralkar wrote:. . . a myProcess() method is responsible for entire functionality. . . .

Why? That sounds like very dubious design.
 
Sheriff
Posts: 24594
55
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Catching the expression and ignoring it with only a log entry isn't a great strategy, either. I wouldn't recommend that unless it's really okay to ignore the exception which is thrown.
 
Ranch Hand
Posts: 58
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Monica Shiralkar wrote:I have a code where a myProcess() method is responsible for entire functionality. Should I enclose this in a try catch block as below:




thanks

it depends on code, and it's courtcy in Java to use try catch cluose again it depends on, what type of  exceptions are, may be checked are unchecked exception, if you say it may throw an exception then use throws clause,  in calling method header, because checked exception are propagated with throws clause, if unchecked exceptions are there then it will compiled well but at runtime it causes error if don't specify
 
Monica Shiralkar
Ranch Hand
Posts: 1131
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Catching the expression and ignoring it with only a log entry isn't a great strategy, either. I wouldn't recommend that unless it's really okay to ignore the exception which is thrown.




For local it is easy to check but ,if the code gives some error when running it on the server then I will be able to see that in the logs this way.
 
Monica Shiralkar
Ranch Hand
Posts: 1131
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Why do you think the call to myPrpocess() should be enclosed in a try-catch?
Does that method throw an exsception?



Suppose if the code is reading from a table in the database but the column does not exist, it will give runtime exception that column does not exist.
 
Monica Shiralkar
Ranch Hand
Posts: 1131
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I assume this was already clear to you, isn't it?



Sorry for that typo.
 
Monica Shiralkar
Ranch Hand
Posts: 1131
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Why? That sounds like very dubious design.




The entire processing is broken down into 4 methods:
fetchTableData
computeResults
saveOutputResults


and myProcess() is the method which calls the 3 methods.


 
sohail hussain
Ranch Hand
Posts: 58
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Monica Shiralkar wrote:

Why? That sounds like very dubious design.




The entire processing is broken down into 4 methods:
fetchTableData
computeResults
saveOutputResults


and myProcess() is the method which calls the 3 methods.


means methods inside method call I don't no such concepts that  its, because one method call contains other method call only one call, how does three methods will execute
 
Paul Clapham
Sheriff
Posts: 24594
55
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Monica Shiralkar wrote:

Catching the expression and ignoring it with only a log entry isn't a great strategy, either. I wouldn't recommend that unless it's really okay to ignore the exception which is thrown.




For local it is easy to check but ,if the code gives some error when running it on the server then I will be able to see that in the logs this way.



Yes, that is true. I have done that myself. But you asked your question without any context, so it would have to be interpreted as "Should I always do this...". That's why I answered the way I did.
 
Monica Shiralkar
Ranch Hand
Posts: 1131
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

means methods inside method call I don't no such concepts that  its, because one method call contains other method call only one call, how does three methods will execute



myProcess() does the below:

calls fetchTableData  and gets the response.
uses the above response to call computeResults method compute the result and return this as response.
uses the above response to save into the database by calling saveOutputResults method.


Is it a wrong design?


 
Sheriff
Posts: 6109
157
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the consensus is that putting a try/catch around your entire process makes little sense.  You should have a try/catch around the smallest amount of code that makes sense, which usually not more than ten lines or so.
 
Bartender
Posts: 2326
100
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just wanted to point out:
should actually be:
This way of logging is more better since a config change can enable or disable info logs and still keep error on. Additionally, the error method will show the complete stack trace in logs. In the info method, you're simply calling an Exception's toString() method.
A person debugging any issue would always find stack traces to be more useful than mere Exception message.
 
sohail hussain
Ranch Hand
Posts: 58
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

myProcess() does the below:

calls fetchTableData  and gets the response.
uses the above response to call computeResults method compute the result and return this as response.
uses the above response to save into the database by calling saveOutputResults method.


Is it a wrong design?


OK fine, but my suggestion is you can use  method chaining concept please provide your complete code, I want to know how it works thanks  M. S
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!