-1

the idea I have about using exceptions and try {} catch {} blocks is that those are used for error handling. I was reading Bjarne's Strostrup FAQ page section about exceptions and I came across this

There are other uses of exceptions - popular in other languages - but not idiomatic in C++ and deliberately not supported well by C++ implementations (those implementations are optimized based on the assumption that exceptions are used for error handling).

What are other usages for exceptions in other languages (C# or java for example)?

Moha the almighty camel
  • 4,327
  • 4
  • 30
  • 53
  • 1
    Your question is too broad to really be answerable. So (even though I disagree with current rules, and some of the most up-voted questions on SO were of this type) I've voted to close it. Still, as an example, one usage is to break out of a deep recursion when an final result has been obtained. – Cheers and hth. - Alf Mar 30 '14 at 18:04
  • 1
    related: http://stackoverflow.com/questions/7480146/difference-between-exception-handling-in-c-and-java – Blue Ice Mar 30 '14 at 18:04
  • Even if exceptions are used more commonly in other languages, and not only for "exceptional" situations, it's almost always for error handling, just like in C++. – Some programmer dude Mar 30 '14 at 18:07
  • Some other languages use exceptions as a standard control flow mechanism for deep returns. BTW: C has `longjump`. – Deduplicator Mar 30 '14 at 18:10

2 Answers2

2

In Python, in the spirit of "ask for forgiveness, not permission", exceptions are frequently used as part as the normal control flow of the application. For instance, when looking up an element in a dictionary (think std::unordered_map in C++):

try:
  my_value = my_dict["the answer to life, the universe and everything"]
except KeyError:
  my_value = 42

In C++, this is not considered an "erroneous" situation; exceptions should only be used in interaction with "unpredictable" things like hardware devices and (to some degree) the operating system.

Thomas
  • 174,939
  • 50
  • 355
  • 478
  • 1
    Also in the internal workings of the Python `for` loop. – Cheers and hth. - Alf Mar 30 '14 at 18:05
  • 1
    Please remember that bad user input is mostly considered unexceptional. – Deduplicator Mar 30 '14 at 18:07
  • 1
    I fail to see how this is different then the use in C++. The usage of exception in protocols such as the iterator protocol seems a *much* better match as Cheers pointed out. – Bakuriu Mar 30 '14 at 18:10
  • In C++ the standard containers don't even throw exceptions, ever, so this pattern wouldn't even be possible in C++. They just return the default value of the container's value type and you'd use `.find()` and compare to `.end()` to check for presence _before_ accessing the element. Feel free to add another answer (or edit mine) for the other example. – Thomas Mar 30 '14 at 18:12
  • @Bakuriu the difference is that in C++ throwing an exception is a very high overhead process, so you try to avoid it whenever possible. In Python it's optimized so that it's no different than any other flow control paradigm. – Mark Ransom Mar 30 '14 at 18:14
  • 1
    C++ containers can throw, though mostly on allocation failure. They just don*t force you to use checked indexing, which they support. – Deduplicator Mar 30 '14 at 18:18
  • 1
    @Thomas I don't see the difference between the `KeyError` in your example and the, for example, `out_of_range` exception raised by a vector's `at` method. If you want to avoid the exception in python you can use `.get`. On the other hand the usage of exceptions in language protocols is a completely different usage that isn't found in C++. – Bakuriu Mar 30 '14 at 18:21
  • @Bakuriu: Yes, `vector` has `at`, but to my knowledge `unordered_map` doesn't have such a thing. And even with `vector`, using `at` and catching the exception would be highly unidiomatic C++, while it's perfectly normal in Python. – Thomas Mar 30 '14 at 18:23
0

One other use is InterrutedException in java. It allows waiting on monitors or sleeping threads step out of wait or sleep.

Drunix
  • 3,313
  • 8
  • 28
  • 50
  • Interrupting a busy thread is considered evil and there are many gotchas. – Deduplicator Mar 30 '14 at 18:08
  • @Deduplicator You probably think if ´Thread.stop´ which is a hard stop with all kinds of problems and which is tehrefore deprecated. ´Thread.interrupt´ gracefully asks the thread to terminate, that's why it needs to step out of wait et.al. to handle the interrupt request. – Drunix Mar 30 '14 at 18:13