• 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

Handling error before catch/finally block when necessary?

 
Ranch Hand
Posts: 37
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

When situation arises such that a client requires to handle an exception before executing the catch() or finally block, how do we handle errors? From within the try block itself?

Thanks and eagerly waiting to see some reply.
 
Master Rancher
Posts: 4806
72
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can't, really. Unless you can test for an error condition before you even call the method. For example, if there's a chance that a reference is null, don't call ref.method() without first checking if ref is null:
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To go on from what MS said: that sort of thing is using Exceptions for flow control. In fact, you would be using the Exceptions as a substitute for if statements, which is by no means good design.
 
Ranch Hand
Posts: 466
1
IntelliJ IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you asking about nested try-catch block? If not, then like Mike and Campbell explained it would not be a wise idea/design to put multiple checks to avoid the exception.
 
Mike Simmons
Master Rancher
Posts: 4806
72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:To go on from what MS said: that sort of thing is using Exceptions for flow control. In fact, you would be using the Exceptions as a substitute for if statements, which is by no means good design.


Um, I wasn't talking about using exceptions for flow control at all. I was talking about the opposite, using flow control to prevent exceptions. And I don't think anything that the original poster talked about implied using exceptions for flow control. Than again, I'm not sure what the original poster meant, if it wasn't what I said, so maybe they could explain the goal better.

Vinod Tiwari wrote:like Mike and Campbell explained it would not be a wise idea/design to put multiple checks to avoid the exception.


That sounds like the opposite of what I said.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I must have been un‑clear. I meant that throwing an catching Exceptions is akin to using exceptions for flow control. I thought that was what OP was asking about.
What you showed is, as you said, the opposite. Using the if (obj == null)… test is a way to prevent exceptions.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic