views:

1612

answers:

5

Why wasn't the Java "throws" clause (in method declaration) included in C#?

+22  A: 

Anders Hejlsberg (the lead C# architect) explains it in this interview:

http://www.artima.com/intv/handcuffs.html

Patrik Hägne
I just read it and I don't really agree with his assertions but it was interesting nonetheless.
Joe Philllips
+14  A: 

(In addition to Patrik's somewhat-definitive answer.)

Checked exceptions in Java are a very controversial issue. I used to love them, and missed them a lot when writing C#. It felt like I was driving without a seatbelt. Now, they annoy me... because while they sound like a good idea in theory, they've definitely caused me a lot of grief but without providing much tangible benefit. I can't remember ever encountering a bug in C# code which checked exceptions would have saved me from. That's not to say it can't happen, but it hasn't happened to me.

The annoying thing is that in some ways it still feels like C# is too lax - but that Java's approach isn't quite the right one. It's like there's a better solution waiting to be discovered, and Java's attempt was a good experiment, but it didn't quite work.

Jon Skeet
+3  A: 

The main reason is that C# designers decided not to use "checked exceptions". This means that the developer doesn't have to surround an exception throwing clause inside a try-catch block. It was considered that this is only helpful for small scale applications and doesn't have a real benefit for bigger projects. Additionally checked exceptions were actually misused by the developers, who constantly use empty catch blocks. Since there are no checked exceptions, there is no reason for a method to declare, which exceptions could be thrown.

The use or not of "checked exceptions" is a controversial topic, but most seem to agree that there is no need for them. Spring, which is a prominent framework in Java, turns checked exceptions into runtime ones.

kgiannakakis
+2  A: 

I think checked exceptions, as a language feature, exposes some of the cultural differences between Microsoft and Sun.

For the most part, Microsoft is a client company. Most of the software they make, and what their development stack is targeted at, is client software.

Yes, yes, I know that Microsoft makes server software. However, Office and Windows are WAY bigger in terms of revenue than Windows Server and Exchange are.

I think it's fair to say that most software written in .NET, (and even more so with VB 6) is client software. Sure, a big chunk of it is Web Software, that runs on a Web Server, but most of it is really ... "clienty type web apps". I'd say most web apps are more like "Word" then they are like Exchange.

And yes, I know there are WCF and SOAP services and things like that, but those are usually just "middle ware" for "clienty" type stuff.

Sun, on the other hand, is primarily a server company. Their software isn't exactly the greatest when it comes to user interface (this isn't a slight on Unix... just Solaris.. Mac OS X is based on unix and it has a phenomenal user interface, the difference between the two is that Sun really doesn't care as much about UI as Apple does ). They even sell THIN CLIENTS, which try to push everything onto the server.

So... in any case... Microsoft is mainly a client company, and Sun is mainly a server company.

When you write server software, reliability is a big thing. It's huge. If an email server crashes, people don't get email, and business grinds to a halt. So, making sure that the server can intelligently handle most errors that can happen is a big part of selling an email server.

With client software. reliability IS important, but no where near AS important as it is with server software. As long as most main line scenarios work, people are happy. In many cases, crazy edge scenarios can just be ignored, or handled generically.

One key to being super reliable is making sure that you handle all the possible edge cases that can occur. What happens if we can't open the database file because it's locked by another process, or we don't have permission to read it? What if we run out of memory, or disk space? What happens if the power goes out while running this procedure?

With really high reliability requirements, having checked exceptions CAN be helpful. If there is a scenario you didn't anticipate, and it's important to your business that you anticipate everything, then having the compiler tell you ... "hey you didn't handle the RocketFuelExhaustedException" would be helpful.

So, I think checked exceptions stem from Sun's point of view as a server vendor.

To Microsoft, as a client company selling developer tools to client software developers, checked exceptions are of course horribly irritating things that just get in the way of real work getting done.

That's my $0.02 anyways...

Scott Wisniewski
That's an interesting viewpoint. It's a shame Sun didn't anticipate the empty catch block though!
John Topley
So what you're saying is that Java code is more 'enterprisey', right?
configurator
I'm not sure if 'enterprisey' is how I would describe it. All of the enterprises I've worked for have used Microsoft's server stack.Granted, my sample size is pretty small.I just mean that most of Microsoft's emphasis is on the client.Most of Sun's is on the server.
Scott Wisniewski
This is some very funny stuff. "What if the power goes out?" Yeah, throw a PowerOutageException. Let's see you recover from that one :)
eljenso
I added the "what if the power goes out" as another example of how sever software sometimes needs to be robust...Although, in a super pedantic nit picky kind of way, it COULD be possible (with a UPS or something) to throw a PowerOutageException, but that wasn't what I meant
Scott Wisniewski
It just all seems very far fetched to me. "Because MS is a client company and Sun is a server company"? Java's predecessor was developed for use in set-top boxes and other embedded devices. Early Java was re-targeted for browsers and the WWW in general. Anything but servers. So how does this fit in?
eljenso
Sun hasn't had much success on the client, though.Also, if you look at Java, most of it's UI facilities are really poor.Before Eclipse, a compelling Java UI library didn't exist....
Scott Wisniewski
...Furthermore, the UI facilities for applets are pretty poor. When compared to Flash they are abysmal.They may have tried to do client stuff, but they aren't very good at it.They are much better at server stuff.
Scott Wisniewski
A: 

Exceptions are apparently 'exceptional' events. In .net's paradigms, exceptions should never occur (that's why you have methods like TryParse, ...), so it makes little sense to be forced to handle events that shouldn't occur anyways.

Thomas Danecker
So an EOFException is supposed to be a surprise when reading a file? I think the opposite is true - exceptions are used far too often in .NET, for situations that are far from exceptional.
Harper Shelby
@Harper Shelby: That's so called "expection handling" then. You should not use exceptions when your programming language has other control structures like "if-then-else". In your example case, why use try{...}catch(EOF) when there's the more controlled "if(f.isEof())" (or similar)?
phresnel