1

I'm porting a library I wrote for Java to .NET C# and I'm dealing with differences in exception handling between the two worlds.

In particular I have an abstract class that I use as base class to implement several services. In this class I have these methods:

public void MyConcreteMethod(){

    //...
    try {
        initialize();
    } catch(InitializationException ex){
        // ...
    }

    // ...

}

public abstract void initialize() throws InitializationException;

Every extending class must implement his way to initialize and I clearly know that I need to handle some possible InitializationException.

In C# I converted my code like this:

public void MyConcreteMethod(){

    //...

    Initialize();

    // ...

}

public abstract void Initialize();

but this way I haven't a clear information about possible initialization problems: in some classes I could throw exception, and in others I could not.

So should I hanlde them in MyConcreteMethod? Which exception shold I catch? A generic Exception?

1

1 Answer 1

3

The only difference is that Java has some checked exceptions, but C# doesn't.

Since the code you are porting was originally written in Java and assuming it had correct exception handling (a matter that should be fairly language agnostic), then just catch the exceptions in the C# code at the same place where the Java code catches them.


In the absence of signature-level exception clauses, the standard .NET practice for communicating possible exceptions to the user of an API is to simply document them. You can document them in the assembly documentation and in the web documentation (if applicable).

Not the answer you're looking for? Browse other questions tagged or ask your own question.