0

I would like to "centralize" the exception handler. For example:

// login_view.dart: the login view throws an exception
throw new LoginException("Invalid username or password");

// exception_handler.dart: in some point of my application the exception is captured 
void exceptionHandler(Exception e) {
  if (e is LoginException) {
    showModalDialog(e.toString()).then(() => redirectToLoginView());
  }
}

Is that possible? I have read about the ExceptionHandler class, but I'm not sure if that class is suitable for this specific situation. Thanks.

Cequiel
  • 3,505
  • 6
  • 27
  • 44
  • 1
    See also http://stackoverflow.com/questions/37793276/angular-2-custom-exceptionhandler-change-detection-lag It's better to catch exceptions as closely as possible where they happen. The custom global exception handler should only be a last resort, for example to centrally log uncaught exceptions and guide the user back to a point where the application is in a stable state (reloade, ...). – Günter Zöchbauer Jul 16 '16 at 13:32

1 Answers1

1

You can define a method that wraps actions with centralized exception handling, such as:

void safely(action) { 
  try {action();}
  catch (ex) { /*exception handling here*/ }
}

//use 'safely' throughout your code like this
safely(() => doSomething());
Star Ford
  • 68
  • 5