Click here to Skip to main content
15,908,675 members
Home / Discussions / .NET (Core and Framework)
   

.NET (Core and Framework)

 
AnswerRe: AppDomain.ApplicationBase headache [modified] Pin
Dave Kreskowiak18-Jul-06 5:20
mveDave Kreskowiak18-Jul-06 5:20 
GeneralRe: AppDomain.ApplicationBase headache Pin
Rob Philpott18-Jul-06 9:09
Rob Philpott18-Jul-06 9:09 
GeneralRe: AppDomain.ApplicationBase headache Pin
Dave Kreskowiak18-Jul-06 10:02
mveDave Kreskowiak18-Jul-06 10:02 
AnswerRe: AppDomain.ApplicationBase headache Pin
Josh Smith18-Jul-06 9:48
Josh Smith18-Jul-06 9:48 
Questionhow to make transparent control above video? Pin
gemfeeling16-Jul-06 19:41
gemfeeling16-Jul-06 19:41 
AnswerRe: how to make transparent control above video? Pin
Bob X17-Jul-06 11:14
Bob X17-Jul-06 11:14 
AnswerRe: how to make transparent control above video? Pin
Andrew Lygin20-Jul-06 9:13
Andrew Lygin20-Jul-06 9:13 
QuestionException handling in .NET Pin
serious-sam14-Jul-06 0:35
serious-sam14-Jul-06 0:35 
Hi all,

I've recently had to work on a .NET web application. Now, although I've been familiar with .NET for approximately a year now, I'm primarily a Java developer. And while I think .NET is a great idea, there is one thing I don't quite understand about it -- the way exceptions are handled. In particular, I'm dissapointed by the lack of checked exceptions. A method does not need to declare that it may throw an exception, making it harder to manage the propagation of exceptions in your programs.

Perhaps a simple example to demonstrate...

Java:

// Read in a single line from the specified file and return it<br />
static void myReadLine(String filename) throws IOException {<br />
    BufferedReader reader = new BufferedReader(new FileReader(filename));<br />
    return reader.readLine();<br />
}


C#:
// Read in a single line from the specified file and return it<br />
static void MyReadLine(string filename)<br />
{<br />
    StreamReader reader = new StreamReader(filename);<br />
    return reader.readLine();<br />
}


It's pretty obvious what both methods are doing, and that they are doing it in pretty much the same way. The main difference is in how those methods can now be used. In C#.NET, you can call MyReadLine() from anywhere in your code and everything will happily compile. If things go wrong and an System.IOException is thrown in that method, then unless you've done your homework and surrounded the call to MyReadLine() with a try-catch block, your application will crash. With a pretty stack trace printed on the screen. Ungracefully.

In Java, the code will not even compile unless you are explicitly catching that exception when calling myReadLine(). An IOException is a checked exception in Java -- meaning that if it's ever thrown, it must be caught somewhere in your code. Other exceptions, such as, say NullPointerException, are run-time exceptions and do not have to be explicitly caught (although sometimes it's not a bad idea to do so). In .NET, it seems, all exceptions are considered run-time.

Even if you happened to know that StreamReader.ReadLine() throws an exception, there's a fair chance that some methods in your code could throw exceptions without you even knowing it. And if they can, then they will -- at the worst possible time, naturally. So your options are either keep your fingers crossed, or trawl through the documentation, checking for possible exceptions for each method your code is calling. Neither option is particularly appealing. Nor is catching the general System.Exception always a good idea. Whenever something has gone sour, the code that makes the sun shine again should know exactly what when wrong and exactly what to do about it. Catching a general exception just tells your program something went wrong -- no more, no less.

Why am I writing this? If you've read this far, you'll probably agree that sure sounds like a whinge. And you'd be right. But there's more to it. I'd like to ask:

Can someone cast light on why are exceptions being handled this way in .NET? What are the advantages of treating all exceptions as run-time exceptions?

Is it something that you just learn to deal with as a .NET developer? How do you deal with it?

Cheers,
Misha
AnswerRe: Exception handling in .NET Pin
Alois Kraus14-Jul-06 0:57
Alois Kraus14-Jul-06 0:57 
GeneralRe: Exception handling in .NET Pin
serious-sam14-Jul-06 4:14
serious-sam14-Jul-06 4:14 
GeneralRe: Exception handling in .NET [modified] Pin
Ray Cassick14-Jul-06 4:31
Ray Cassick14-Jul-06 4:31 
GeneralRe: Exception handling in .NET Pin
Alois Kraus15-Jul-06 1:51
Alois Kraus15-Jul-06 1:51 
AnswerRe: Exception handling in .NET Pin
Colin Angus Mackay14-Jul-06 1:02
Colin Angus Mackay14-Jul-06 1:02 
GeneralRe: Exception handling in .NET Pin
Jun Du14-Jul-06 5:21
Jun Du14-Jul-06 5:21 
AnswerRe: Exception handling in .NET Pin
Kevin McFarlane17-Jul-06 0:56
Kevin McFarlane17-Jul-06 0:56 
QuestionControlling visibility of controls in a single web form for two different user groups Pin
Renukapadhamanaban13-Jul-06 21:00
Renukapadhamanaban13-Jul-06 21:00 
AnswerRe: Controlling visibility of controls in a single web form for two different user groups Pin
manish2414-Jul-06 1:18
manish2414-Jul-06 1:18 
GeneralRe: Controlling visibility of controls in a single web form for two different user groups Pin
Renukapadhamanaban16-Jul-06 14:10
Renukapadhamanaban16-Jul-06 14:10 
AnswerRe: Controlling visibility of controls in a single web form for two different user groups Pin
Jim Conigliaro17-Jul-06 1:58
Jim Conigliaro17-Jul-06 1:58 
QuestionSerial Port 2.0 to 1.1 backward compatibility Pin
Dominick O'Dierno13-Jul-06 6:39
Dominick O'Dierno13-Jul-06 6:39 
AnswerRe: Serial Port 2.0 to 1.1 backward compatibility Pin
Dan Neely13-Jul-06 7:01
Dan Neely13-Jul-06 7:01 
GeneralRe: Serial Port 2.0 to 1.1 backward compatibility Pin
Dominick O'Dierno13-Jul-06 7:20
Dominick O'Dierno13-Jul-06 7:20 
GeneralRe: Serial Port 2.0 to 1.1 backward compatibility Pin
Dan Neely13-Jul-06 8:41
Dan Neely13-Jul-06 8:41 
GeneralRe: Serial Port 2.0 to 1.1 backward compatibility Pin
Dominick O'Dierno13-Jul-06 8:47
Dominick O'Dierno13-Jul-06 8:47 
AnswerRe: Serial Port 2.0 to 1.1 backward compatibility Pin
Kurt _B17-Jul-06 10:06
Kurt _B17-Jul-06 10:06 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.