-4

I try to understand why should I "try" a method to catch errors.

It looks to me like the concept is: "letting a process run assuming its not properly developed".

Should I always assume that a program based on classes and inheritance is bound for unexpected errors, which I should handle with tools like try catch and throw?

Should all Java programs be included within try catch framework?

  • 3
    Unclear what help you need. Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it’s hard to tell what problem you are trying to solve or what aspect of your approach needs to be corrected or explained. See the How to Ask page for help clarifying this question. – gnat Aug 25 '14 at 16:10
  • 1
    Your users probably don't expect unexpected errors. – JeffO Aug 25 '14 at 16:38
  • 1
    "Save to File" -> chooses a place in C:\Program Files -> Windows refuses because original program was not started with administrative privileges -> program crashes. If only there were some way to mark when code depended on an outside resource and attempt to handle the error without crashing the whole program... – BrianH Aug 25 '14 at 20:41
3

it looks to me like the concept is:- ' letting a process run assuming its not properly developed.

Error conditions are often beyond the control of the programmer at the level where they occur. Exception handling is one way to handle problems that come up.

For example, consider a purchasing account that lets a user order products and pay for them out of an associated bank account. If the purchasing account was set up properly, it should work fine every time. But what if the account was created but no bank account was ever associated with the purchasing account? That's an error that the purchasing account can't (or probably shouldn't) deal with by itself.

Exception handling is designed to help errors propagate up to a level where they can be handled most effectively.

should i always assume that a program based on classes and inheritance is bound for unexpected errors

Any sufficiently complex software is likely to have potential for errors. Any interesting software that's used by people is almost certain to need to handle errors in some form. Inheritance and classes don't necessarily have anything to do with the issue -- exception handling can be done in procedural languages like C.

On the other hand, exception handling isn't the only way to handle problems -- Objective-C has an exception handling mechanism, but throwing an exception is usually reserved for fatal errors.

which i should handle with such a heavy tools like try catch throw throws ?

Use it when it's the right tool for the job. Whether it's the right tool or not depends in part on the conventions used in the context you're working in, the tools your language provides, your ability to handle the error condition yourself, etc.

should all java programs be included withing try catch framework?

Not necessarily -- some programs are simple enough not to need exception handling. But any skilled Java programmer will understand the mechanism, be prepared to handle exceptions thrown by any classes they use, and know when to throw an exception.

| improve this answer | |

Not the answer you're looking for? Browse other questions tagged or ask your own question.