3 ms·
In some languages, the standard way to signal failure is to throw an exception. You can't really define exceptions as abnormal in those cases, because failures
by chousuke 5y ago
In some languages, the standard way to signal failure is to throw an exception. You can't really define exceptions as abnormal in those cases, because failures are part of normal operation; you can't expect them not to happen if you want robust software.
- Veuxdo 5y agoWhy not return an error in the case of an anticipated failure? You don't have to use exceptions for that.
- formerly_proven 5y agoBecause that's highly un-idiomatic in pretty much every language that has exceptions, except G++, which arguably doesn't really have exceptions as they are strengstens verboten. Even if you really really like error return codes and really really don't like exceptions, you'll likely agree that writing very un-idiomatic code that goes against the grain of the language and all the libraries you might be using is likely a much bigger loss than the value you might have added by avoiding exceptions in your part of the block.
- Veuxdo 5y agoIt's really comes down to wether you think exceptions are an everyday thing not. I like going by the dictionary definition of the word "exception" which states that they are not. Out of curiosity, in what languages are error return types non-idiomatic?
- slaymaker1907 5y agoPython and Java are two that come to mind. It is idiomatic in both to return null/None on error, but it is not idiomatic to use a result type. Hell, Python even uses exceptions to terminate iterators. This isn't to say the idioms are never broken or that they shouldn't be broken. I agree that if you are doing something like parsing a string to an integer, that should probably be a result type and not just throw an exception.
- chousuke 5y agoIn almost all dynamic languages I know of for example. Way too easy to forget about error handling if you use values. Exceptions at least tend to cause noise. In languages like Java an Python, you can return error values but because you can also always return nulls, you're just making more trouble for yourself than it's worth avoiding exceptions.
- deleted 5y ago[deleted]
- ivas 5y agoIn your code, you don't have to use exceptions to indicate a failure but you still have to catch exceptions thrown by third-party libraries (or their dependencies). It is not easy to stay away from exceptions if your language supports them.