0

I have read various threads on the subject but have not found an answer( or I do not understand the problem)

The case looks like this: main.py:

from a import aa


aa()

a.py

from b import bb
from c import cc

class aError(Exeption):
...

def aa():
  try:
    bb()
    cc()
  except aError:
    ...
...

b.py

from a import aError


class bError(aError):
...


def bb():
  raise bError
...

c.py

from a import aError

class cError(aError):
...

def cc():
  raise cError
...

as you can see, I have circular import a -> b/c -> a. Is there any nice way to solve this problem without throwing aError from a.py ?

  • You can write all those functions to be used in `a` in `a` itself or write all functions used by `b/c` in `b/c` itself and import those in `a`. – adityanithariya Jan 16 '23 at 15:51
  • but in b/c it only needs from 'a' aError which is inherited (so all exceptions can be caught) – MattyOstrowsky Jan 16 '23 at 16:08
  • *"Explicit is better than implicit"* - If the function raises `cError`, you should catch `cError`... – Tomerikoo Jan 16 '23 at 16:13
  • Anyway, if you must inherit from `aError` and want to only catch that, simply move `aError` to a shared module to resolve the problem. It makes logical sense as everyone is using it... – Tomerikoo Jan 16 '23 at 16:15

0 Answers0