Multiple error handlers

Good day! I apologize if the topic duplicates a question. Tell me, please, is it possible to make a handler for one router using Twig, and the next one using JSON. Here is my first router:

// ....
if (1 < 0) {
     throw new Exception('This message must be passed to the template or json for ajax and stop the further work of the script');
}
// ....

Could you please add more details to your question?

Generally, Twig is a template engine and JSON a special format for structured data.
Do you mean: How to return HTML or JSON in a handler?

i mean return exception depending on router

Yes, this is possible.

The Slim ErrorMiddlware is doing something similar, depending on the request Accept header.

But you wrote, you want to define the response depending on the route.
So you could implement one Middleware for each mime type that catches all Exceptions and transforms the Exception into a proper response (HTML or JSON). For example a JsonExceptionMiddleware and a HtmlExceptionMiddleware. Then add this middleware to the routes or route groups you need.