Exclusion of exceptions from the class base

I have a base class and I would like to catch all the exceptions of the derived class in the base class, is this possible?

You will not know what methods are from a derived class.

+4
source share
2 answers

You need to provide more details about your specific scenario. However, if, for example, you have a base abstract class that provides a contract, and you want to catch all the possible exceptions caused by derived classes when invoking a base class contract, you can do something like this:

public abstract class Base { protected abstract void InternalFoo(); protected abstract void InternalBar(); public void Foo() { try { this.InternalFoo(); } catch { /* ... */ } } public void Bar() { try { this.InternalBar(); } catch { /* ... */ } } } 
+2
source

By calling a class, do you mean a derived class or methods of calling a class from a class received from your base that are not related to it?

I think you can do this by turning your database into a proxy class. See Dynamic Proxy Example.

+1
source

Source: https://habr.com/ru/post/1306460/


All Articles