Why have many programmers moved to using exception handling for input or output? For these programmers, what is the motivation behind this decision?
put on hold as primarily opinion-based by gnat, gbjbaanb, DougM, Snowman, Bart van Ingen Schenau 12 hours agoMany good questions generate some degree of opinion based on expert experience, but answers to this question will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise. If this question can be reworded to fit the rules in the help center, please edit the question. |
|||||||||||||||||||||
|
Because input and output are unreliable.Exceptions are, in part, an acknowledgement that there are going to be conditions which the software cannot reasonably recover from.
Exceptions are the programmer's way of saying "I can't continue under the present conditions." In essence, when you throw an exception, you're giving up and asking for a do-over. You can do this without gracefully exiting your input/output subroutine or even worrying about memory management, because the orderly disposal of the relevant objects is already built into the exception-handling mechanism. You can control where in your code the exception gets handled without elaborate |
||||
|
Because exceptions (as opposed to error code return values) cannot be ignored. The typical code for i/o calls before exceptions, was usually written in terms of printf/scanf/gets/puts. These functions return values that allow the developer to check for errors, but that is not trivial. Most developers, knowing the error handling is not trivial, simply ommit it; They tend to think a typical fscanf call should look like this:
when it should probably look like this:
This code can (to a point) be extracted into a function, parametrized and (probably) hidden inside a wrapper on scanf, or a macro (both of which have downsides). If you do not write it though, your application simply doesn't handle the errors. On the other hand, exceptions give you more useful error information, propagate naturally up the call stack and cannot be ignored (if you do, they will stop your application). |
|||||
|
Because it is more readable code.For example (i love examples):
What if here we will return a value instead of throwing exceptions? Let me see:
Okay, it is still fine. But how clients code will looks?
That's a common part of it. Exceptions:
As a regular developer i can see what code is responsible for each part of error/exception.
Other plus of exceptions: It can be ommited in client code but it will throw exception eventually when issue occured. It will not be silient, buggy and unreadeable code. |
|||
|
I agree with remaining answer on one fact : That "exceptional return" does happen and needs to be handled. But reason why exceptions exist is not that exceptional returns exist. It is possible to have "return values" for each of the possible error states, just like C does. The reason why exceptions are preferable to C-like error handling is that it is extremely rare that error from a function will be handled in immediate caller. It is much more common for error state to bubble up the stack until it gets into place where it can be reasonably handled. Also, single "handling" code will handle different method calls. If you are using C-like error handling, you have to create extensive amount of code to do the bubbling and aggregation of errors all the way to the handler code. This requires discipline and rigor and drastically complicates return values from methods, where every possible exceptional return needs to have it's own value). Exceptions and infrastructure around them simply do all of this for you. And while I do agree exception handling too needs some level of discipline and rigor, it needs it much less. |
|||
|
Remember first you are solving problems. Imagine a simple solution, adding two numbers. You should do the following.
That is the approach I always take. Its very TDD but it works for me and I have to say I find it quicker than any other approach. Exceptions are clean. And in a language like python there would be no coding required to raise most of them.. 'a'+2 raises an exception for free :-) |
|||
|
I find exceptions are cleaner for code style since I can wrap the code and deal with any errors in a specific location (the catch block), as opposed to littering my code with lots of if statements ( |
|||
|
Because it's the easy way out.Exceptions are, in part, an acknowledgement that error-handling is hard and while we can recover from exceptions, it's much easier not to. Exceptions are the programmer's way of saying "I have this ideal situation that must be matched and I'll not bother spending more lines nor time handling situations that are not ." In essence, when you throw an exception, you're giving up:
|
|||||||||||||||||||||
|