News for hackers. Stuff that matters.
Re:Why should I have written ZeroMQ in C, not C++ by 0xABADC0DA on Thursday May 10, 2012 @02:34PM (#3955154)
Checked exceptions are great, but unchecked exceptions are a huge disaster only mitigated by failsafe features such as RAII or garbage collection with finalizers. Sometimes like in highly dynamic languages the failsafes are enough that it's ok to just use unchecked exceptions, but in a language like C++ you really can't afford to make mistakes in the first place.

The process of adding an exception in Java:

1. throw TheException

2. compile

3. add "catch(TheException e)" to fix errors or "throws TheException" to signature

4. repeat 2 until no more errors

Compare to C++:

1. throw TheException

2. examine all functions to determine if they might need to handle TheException or consult intuition about program structure

3. repeat 2 until 'done'

So the real problem with exceptions in C++ is the compiler can't tell you where to look next and what you forgot about. Step 2, magically find all the places that should catch the exception, is both time consuming and error prone.

I find it strange that C++ has all kinds of strictness with types (have to cast void*, const virus, etc) but with exceptions they just throw caution to the wind.

Re:Why should I have written ZeroMQ in C, not C++ by MatthewPhillips on Thursday May 10, 2012 @02:56PM (#3955259)
Java is the only language that I can think of that gets exceptions right, in that it forces you to acknowledge them every step in the chain and choose whether you're going to catch them or throw them upwards.
Re:Why should I have written ZeroMQ in C, not C++ by SeanLuke on Thursday May 10, 2012 @03:41PM (#3955538)
Strange. It seems to me that the consensus in the Java community is that checked examples were among the worst misfeatures of the language.

Googling for ---java checked exceptions--- is illuminating.

Re:Why should I have written ZeroMQ in C, not C++ by JamisonM on Thursday May 10, 2012 @05:53PM (#3956310)
It is strange that the consensus in the Java community sort of ended up being against checked exceptions. I for one think that this consensus is completely wrong and I suspect that what looks like a consensus is just a bunch of very loud people shouting down everyone else (and to me not making very compelling arguments). Elliotte Rusty Harold's brief post on this pretty much nails it for me: http://cafe.elharo.com/blogroll/voting-for-checked-exception...
Re:Why should I have written ZeroMQ in C, not C++ by nostrademons on Friday May 11, 2012 @12:06AM (#3957583)
The problem is that it's a trade-off - it's not a case of one side being right or wrong. Checked exceptions introduce API fragility: when the implementation of a method changes in a seemingly inconsequential way (it may now throw an error), then all it's callers and all their callers, transitively need to have their signatures edited. What should be a simple matter between just the leaf method and the top-level now has to involve all the code between them, which may not even be in code you authored or controlled.

Unchecked exceptions, however, mean that code that would previously always succeed may now never even be executed. Which makes it difficult to reason about the behavior of your function.

It's the same trade-off that appears in several other areas of language design. Do you force developers to write down their assumptions in the source code, so that everyone reading it knows exactly what's going on? Or do you let them keep it in their heads, so that it can change easily when the system requirements change? Dynamic vs. static typing, global variables vs. parameters, polymorphism vs. conditionals, default arguments vs. explicitly specifying parameters - it's all the same fundamental argument applied to different language constructs.

Which side your on usually ends up depending on whether you read more code or write more code. Maintenance devs always want things to be explicit, because it makes it easier for them to grok the whole codebase and make the change they need to do. Green-field innovators always want things to be implicit, because it means they have to specify less, and everything they specify tends to end up changing anyways. Sometimes the same person ends up cursing both ends of the divide depending on what they happen to be doing at the time. I do a bunch of speculative prototyping for Search Features at Google; in this role, 90% of my code never sees the light of day, and so I write it with tools like Python or JQuery that let me easily change things around and not specify too many of the details. I also do a fair bit of infrastructure work on the search stack; in this role, I write barely any code and spend the bulk of my time tracking down where an obscure piece of data is coming from and how it needs to be modified to add a new feature.

Re:Why should I have written ZeroMQ in C, not C++ by JamisonM on Friday May 11, 2012 @02:19AM (#3957974)
It is a trade-off, on that I agree with you and I do understand that checked exceptions do tend to propagate implementation-level details up where that might be undesirable. I also agree that in green-field situations it can be awkward figuring out what to do with an IOException that you suspect will not exist in a few hours.

The problem as I see it is that if you advocate the alleged consensus that checked exceptions are evil then you are taking the position that there is no trade-off. Checked-exception lovers like myself acknowledge this grey area and the design challenges it presents. That is a big part of why I believe that a Java with checked exceptions is more "reality-based" than one without them, it recognizes the challenge and gives an API designer the tools to do the right thing. I want to have that hammer in my tool chest instead of banging on things with the back of my hatchet.

Re:Why should I have written ZeroMQ in C, not C++ by MatthewPhillips on Thursday May 10, 2012 @03:52PM (#3955616)
That's interesting. I'm not a day to day Java programmer so my perspective is different from the Java community's. I have ran into problems numerous times in other languages where an exception isn't discovered until runtime in production because there's simply no way of knowing whether a library's function might throw an exception or not. The workaround is to be overprotective. I've always envied that Java devs know what to expect from every function they call
Re:Why should I have written ZeroMQ in C, not C++ by Anderkent on Thursday May 10, 2012 @07:02PM (#3956615)
Your java library might still throw unchecked exceptions, so you have to be as careful as if all exceptions were unchecked.
Re:Why should I have written ZeroMQ in C, not C++ by evincarofautumn on Thursday May 10, 2012 @03:05PM (#3955315)
Haskell does this also, and its type system is much more expressive than Java’s, enough so that exceptions are not (exclusively) a built-in language feature.
Re:Why should I have written ZeroMQ in C, not C++ by aidenn0 on Thursday May 10, 2012 @02:57PM (#3955266)
Read up on exception specifiers in C++ and reconsider this comment.
Re:Why should I have written ZeroMQ in C, not C++ by davedx on Thursday May 10, 2012 @03:05PM (#3955319)
I just did; what I couldn't figure out, was is there a way to specify multiple different exception types in your C++ throw()?

In Java, you can do:

public int myfunc(int a, int b) throws IOException, InvalidGoatException {}

How would you do this in C++?

Re:Why should I have written ZeroMQ in C, not C++ by cbsmith on Friday May 11, 2012 @11:35AM (#3959723)
You can do much the same thing, only the meaning is the reverse.
Re:Why should I have written ZeroMQ in C, not C++ by signa11 on Thursday May 10, 2012 @03:10PM (#3955347)
> @davedx asks : is there a way to specify multiple different exception types in your C++ throw()?

yeah throw() can contain multiple types that can possibly be emitted e.g. int foo() throw(a,b,c,d) implies that foo can only throw either a/b/c/d types.

Re:Why should I have written ZeroMQ in C, not C++ by Peaker on Thursday May 10, 2012 @05:54PM (#3956312)
Except that is only enforced at runtime, and converts the exception type to unexpected_exception, which is even worse!
Re:Why should I have written ZeroMQ in C, not C++ by pubby on Thursday May 10, 2012 @05:17PM (#3956087)
Exception specifiers are deprecated and for good reason.