-1

ive wrote in c++ and understood why exceptions is needed. mostly because of the raii that allow to you to handle all the resources you have in very good and easy way.

but, in c# i cant seem to find any reason to use exceptions. they are make the code slower and require more resources from the system.

what are the advantages of exceptions over c-style enums ? tyvm:)

  • Well exceptions are not free in C++ either.. – Neil Kirk Mar 30 '14 at 21:29
  • 2
    "they are make the code slower". Yes. Code makes the code slower. Why do we use them? because they're a language feature that make the management of exceptional circumstances easy. I'm surprised you mention RAII but don't see the relationship of C# exceptions with them, particularly when used in the scope of `using` statements and `try/[catch]/finally` blocks. Exceptions capture a considerable amount of info about the throw site... do I need to carry on? – spender Mar 30 '14 at 21:33
  • @spender, I think your comment could have warranted a full-fledged answer. – Oren Hizkiya Mar 30 '14 at 21:34
  • 3
    possible duplicate of [C++ - Arguments for Exceptions over Return Codes](http://stackoverflow.com/questions/1849490/c-arguments-for-exceptions-over-return-codes) – Tony Mar 30 '14 at 21:34
  • @Oren, I've never coded with return codes (or at least, not for a very long time), so I don't feel confident with the other side of the argument. – spender Mar 30 '14 at 21:39
  • @spender, that's intellectually honest. – Oren Hizkiya Mar 30 '14 at 21:43
  • The overhead in try/catch + throw is really when you throw. So if that isn't part of "normal operations", that's fine. If you "throw" often, however, then it's a bad thing. – Mats Petersson Mar 30 '14 at 21:50
  • @spender if you ever written code such as `if (! (ss >> x))` where `ss` is a stream, then you have kind-of used return codes instead of exceptions! – Neil Kirk Mar 30 '14 at 21:53

1 Answers1

0

C# is having an automated memory management, this doesn't meen that you don't need to use RAII because you sometimes need to release unmanaged memory or ressources.

Exception are classes they can include how much data you want and have methods. You also can use inheritance, this help you can catch a sub type exception without rewriting any catch clause. With enums you can't do any of theses.

Enum/error code management force you to write less readable code because you mix business code with error code management because you have to route sub call error to the caller. This increase the nesting deph of your code wich is also les readable.

manuc66
  • 2,701
  • 29
  • 28