exception handling?

  • Thread starter Thread starter Flip
  • Start date Start date
F

Flip

I'm moving from java to C# and have a lot of fun doing it. But something
I've noticed I want to double check on in case I'm not learning properly is
checked exceptions. Does C#/.NET have such a concept? If I create a class
that throws some exceptions, how can I ensure any other classes calling my
method (like my own :>) catch and either rethrow or deal with the exceptions
at compile time?

Thanks.
 
Flip said:
I'm moving from java to C# and have a lot of fun doing it. But something
I've noticed I want to double check on in case I'm not learning properly
is checked exceptions. Does C#/.NET have such a concept? If I create a
class that throws some exceptions, how can I ensure any other classes
calling my method (like my own :>) catch and either rethrow or deal with
the exceptions at compile time?

Thanks.

C# does not have the same concept as java where you can define methods with
exceptions in such a way that a compile time check can be made to insure
that anyone calling your method has coded to handle your exceptions.
 
C# does not have the same concept as java where you can define methods
with
exceptions in such a way that a compile time check can be made to insure
that anyone calling your method has coded to handle your exceptions.

Thank you Tom for the confirmation. I'm not that new then?! :> Is there a
patter/format you (or others?) code to help guarantee things don't fall
through the cracks, or bubble up to the top level?
 
Flip said:
Thank you Tom for the confirmation. I'm not that new then?! :> Is there
a patter/format you (or others?) code to help guarantee things don't fall
through the cracks, or bubble up to the top level?

I think most folk take a different approach to exceptions in C#. I only
throw exceptions if something truly unexpected occurs. So it is never a
requirement that anyone specifically catch an exception that I might throw.
If there is information that I want to make sure any caller at least writes
code to deal with, whether they look at it or not, I would define an object
to hold the information and either return an instance of that object or
require the caller to send me a reference to an instance of that object for
me to populate.

Short, then is that you cannot use exceptions as a way to force a caller to
deal with some condition in your code.

As I am not an experienced java programmer, only familiar with the language,
there may certainly be some nuance to that mechanism in java that escapes
me.
 
Flip said:
I'm moving from java to C# and have a lot of fun doing it. But something
I've noticed I want to double check on in case I'm not learning properly is
checked exceptions. Does C#/.NET have such a concept? If I create a class
that throws some exceptions, how can I ensure any other classes calling my
method (like my own :>) catch and either rethrow or deal with the exceptions
at compile time?
No, it does not have such a concept like Java.

However, you can do the following...

class MyErrorClass {
public void ThrowMethod() {
throw new Exception();
}
}

class MyMiddleClass {
public void SomeMethodInTheMiddle() {
try {
MyErrorClass cls = new MyErrorClass();
cls.ThrowMethod();
}
finally {
Console.WriteLine( "Yes you will get an error");
}
}
}

class Caller {
public void CallerMethod() {
try {
MyMiddleClass cls = new MyMiddleClass();
cls.SomeMethodInTheMiddle();
}
catch( Exception ex) {
Console.WriteLine( "oooweee finally worked");
}
}
}

The finally keyword is used to catch any exceptions thrown in the
associated try block. At that point you can do something with the error
like log it, etc. Once the finally block has finished executing the
generated exception continues to find a catch block.

This is not like Java, but it gets the same effect and it is optional.

Christian
 
Christian Gross said:
No, it does not have such a concept like Java.

However, you can do the following...

The finally keyword is used to catch any exceptions thrown in the
associated try block. At that point you can do something with the error
like log it, etc. Once the finally block has finished executing the
generated exception continues to find a catch block.

This is not like Java, but it gets the same effect and it is optional.

It doesn't get the same effect as Java's checked exceptions at all - it
gets the same effect as Java's finally block, unsurprisingly.

There is *no* equivalent to checked exceptions in .NET - you have to
check the documentation to find out which exceptions a method might
throw, and hope that it's correct...
 
Back
Top