-2

There are a lot of explanations how the Exceptions works in the Java, that is the root class of the Exception, their ineritance. But few days ago I was asked new question for me: What is exactly reason for Exceptions in the Java? What is the target it in Java. WHY (not what is it or how it works there)? What is you understanding this question and explanation?

Andrew Niken
  • 606
  • 2
  • 8
  • 18
  • 1
    Some duplicates: [Why are exceptions considered better than explicit error testing?](https://softwareengineering.stackexchange.com/questions/166039/why-are-exceptions-considered-better-than-explicit-error-testing) [Check first vs. exception handling](https://softwareengineering.stackexchange.com/questions/139171/check-first-vs-exception-handling) [Result object vs throwing exceptions](https://softwareengineering.stackexchange.com/questions/405038/result-object-vs-throwing-exceptions) – eis Apr 04 '21 at 12:02
  • And some more: [Which and why do you prefer exceptions or return codes](https://stackoverflow.com/questions/99683/which-and-why-do-you-prefer-exceptions-or-return-codes) Other links: [Cleaner, more elegant and harder to recognize](https://devblogs.microsoft.com/oldnewthing/20050114-00/?p=36693) [Which is better, return code or exception](https://www.ibm.com/support/pages/which-better-return-code-or-exception) – eis Apr 04 '21 at 12:05
  • 2
    Back in the day, James Gosling, one of the creators of Java, gave [an interview covering the subject](https://www.artima.com/articles/failure-and-exceptions). I'm sure there are other, similar ones, and Gosling had (maybe still has) a blog in which he discussed such matters. – John Bollinger Apr 04 '21 at 12:05

2 Answers2

2

Exceptions are practical and useful. That's it. This is the same for Java as for any other language that supports them.

Longer version:

  • Exceptions keep your code simple, readable and organized.
  • Finding and fixing problems in your code becomes much simpler because you see exactly where the problem occured and how your code got there.
  • You don't have to check after every line if something went wrong.
  • You don't need special return values from functions to tell the caller that the function failed.
  • You can have powerful handlers where your code "jumps" automatically when some unexpected condition occurs.
  • They are non-intrusive - you don't have to deal with them unless you want to.
  • Even if you don't handle an exception yourself, there is still a well-defined and predictable default mechanism that will handle it.
  • With exception chaining you also see what was the cause of the problem.
jurez
  • 4,436
  • 2
  • 12
  • 20
  • This answer is more concise and neat explanation. definitely a good answer –  Apr 08 '21 at 09:20
2

Exception handling is a longstanding mechanism in programming, dating at least back to the 1960s (see Multics and PL/I for examples). Some system and language designers find it useful, some do not.

The rationale is that exceptions are appropriate for handling circumstances that are 'exceptional' and that therefore are best handled by out-of-line code.

There is an analogue in hardware. Early computers might set a status flag (which the program might or might not look at) if, for example, you attempted to divide by zero. Or perhaps the machine would even halt. Subsequently, exceptions became common: on divide-by-zero, the machine would trap to a specific location, in which the runtime system would have been expected to have installed a handler.

Short answer: it's a matter of taste in language design.

user15187356
  • 807
  • 3
  • 3