Win a copy of Programmers Guide to Apache Thrift this week in the Open Source 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
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

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

 
Ranch Hand
Posts: 1115
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: 1329
3
Java Netbeans IDE Spring
  • 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: 703
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: 64473
225
  • 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.
 
Marshal
Posts: 24461
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: 43
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: 1115
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: 1115
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: 1115
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: 1115
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: 43
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
Marshal
Posts: 24461
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: 1115
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?


 
Marshal
Posts: 5980
155
Chrome Eclipse IDE Java Postgres Database Ubuntu VI Editor
  • 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: 2277
95
Eclipse IDE Google Web Toolkit 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: 43
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
 
Forget Steve. Look at this tiny ad:
ScroogeXHTML - small and flexible RTF to HTML converter library
https://coderanch.com/t/710903/ScroogeXHTML-RTF-HTML-XHTML-converter
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!