C#: Do you raise or throw an exception?
Asked Answered
O

5

14

I know that this probably doesn't really matter, but I would like to know what is correct.

If a piece of code contains some version of throw new SomeKindOfException(). Do we say that this piece of code can potentially raise an exception? Or throw an exception?

The keyword is throw, so I am kind of leaning towards that, but to raise an exception is also used a lot...

Do they have some sort of different meaning? Does one of them sort of implicate a difference over the other? Or is it just two words meaning exactly the same thing?

Orta answered 12/6, 2009 at 9:28 Comment(1)
For a related discussion in Ruby: stackoverflow.com/questions/51021Smatter
V
28

In C# terminology, raising is used in the context of events and throwing is used in the context of exceptions.

Personally, I think throw/catch combination is more beautiful than raise/catch.

Vosges answered 12/6, 2009 at 9:30 Comment(2)
Ah, of course, I totally agree. Makes total sense now! Thank you for clearing that up :)Orta
C# also uses "lift", to mean "provide a nullable version of an operator on non-nullable types", and "hoist" to mean "turn a local variable into a field of a closure class". So in C# you can raise, hoist or lift things, and they all mean something different.Adiel
C
3

I can't speak for C#, but in C++ "throw" was chosen because "raise" was already taken as a standard function name.

Counterfoil answered 12/6, 2009 at 9:30 Comment(0)
S
1

I think while throw is the better one, the two terms are interchangeable. Very useful if you have to use it twice in some text so that you avoid repeating the same word. But that's stylistics which is not necessary for technical writing (but still a nice thing to have).

Sibyls answered 12/6, 2009 at 9:43 Comment(1)
You're right in English writing, but in technical writing it's better to use consistently the same word, or the reader may think there is some difference.Mcelroy
E
1

The terminology of the various languages favors 'throw'. And I agree that 'throw' makes a lot more sense considering this is the keyword used and the compiler/language documentation always refers to exception being 'thrown'.

However, in the belly of the behemoth exceptions are actually Raised, not thrown. If you find yourself deep enough in debugger staring at an EXCEPTION_RECORD then you talk in the terminology of SEH, in which the particular class of compiler/language exceptions (eh, clr, vcpp) is just one of the many flavors exceptions come as. I guess the original conditions for which exceptions where raised for, like page faults and stack overflows, were conditions 'noticed' by the kernel that was 'informing' the user mode of the condition and not exception the user code was 'throwing' onto himself, so the term makes more sense in that context.

Extremist answered 12/6, 2009 at 11:24 Comment(0)
F
0

Either throw or raise seems fine, they mean the same to me. I wouldn't write it, but when I'm talking, I usually say 'chuck an exception'.

For events I'd use either 'fire an event' or 'raise an event'.

Foskett answered 12/6, 2009 at 9:54 Comment(1)
"Fire" is of course in common usage for events. When coming up with terminology guidelines, the team doing so thought that "fire" seemed unnecessarily belicose, so they suggested eschewing "fire" in favour of "raise". My personal opinion is that this is a bit silly, but "raise" is fine and we might as well standardize on one or the other.Adiel

© 2022 - 2024 — McMap. All rights reserved.