Win a copy of 97 Things Every Java Programmer Should Know 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
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

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

 
Ranch Hand
Posts: 1466
8
  • 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: 1402
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: 732
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: 69424
276
  • 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: 25594
69
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: 84
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: 1466
8
  • 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: 1466
8
  • 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: 1466
8
  • 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: 1466
8
  • 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: 84
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: 25594
69
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: 1466
8
  • 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: 7108
184
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: 2587
124
Google Web Toolkit Eclipse IDE Java
  • Likes 1
  • 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: 84
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
 
Monica Shiralkar
Ranch Hand
Posts: 1466
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Knute Snortum wrote: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.



Thanks, is there any problem in putting it around the entire process method?
 
Knute Snortum
Sheriff
Posts: 7108
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not if that method throws an error.  You want to be able to see the entire try/catch block on one screen or less.
 
Monica Shiralkar
Ranch Hand
Posts: 1466
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes I can see the entire try catch block on the screen because the only line inside the try catch block is the call to myProcess method.
 
Monica Shiralkar
Ranch Hand
Posts: 1466
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Corrected it and edited.
 
Live a little! The night is young! And we have umbrellas in our drinks! This umbrella has a tiny ad:
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic