المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : [SOLVED] Catching several (hierarchically unrelated) exceptions in the same catch block



C# Programming
03-10-2010, 03:15 PM
Hi everyone.

This is a trivial question, but I can't find the answer anywhere (even though it's been asked before in these forums, but it wasn't solved in a way that satisfies me): is it possible to catch several hierarchically unrelated exceptions in the same catch block? By hierarchically unrelated I mean one isn't a subclass of the other, etc.

Basically, I'd like to do this:

try {
// Code.
} catch (ExceptionType1, ExceptionType2) {
// Exception handling. I want to handle both types of exceptions the same way.
}
Is it possible, or do I need to do it as follows?

try {
// Code.
} catch (ExceptionType1) {
handleMyException();
} catch (ExceptionType2) {
handleMyException();
}

public void handleMyException() {
// Exception handling. I want to handle both types of exceptions the same way.
}
I know Java allows the first format (ExceptionType1, ExceptionType2), but C# doesn't seem to like it. However, having to create a method each time this happens doesn't seem to promote code-cleanliness.

So... is it possible to do it as in Java?

Thanks!modified on Wednesday, March 10, 2010 6:10 AM