@@ -86,6 +86,36 @@ C/C++ standard.::
8686 }
8787 });
8888
89+ The ``ConsoleEvents::EXCEPTION `` Event
90+ --------------------------------------
91+
92+ **Typical Purposes **: Handle exceptions thrown during the execution of a
93+ command.
94+
95+ Whenever an exception is thrown by a command, the ``ConsoleEvents::EXCEPTION ``
96+ event is dispatched. A listener can wrap or change the exception or do
97+ anything useful before the exception is thrown by the application.
98+
99+ Listeners receive a
100+ :class: `Symfony\\ Component\\ Console\\ Event\\ ConsoleExceptionEvent ` event::
101+
102+ use Symfony\Component\Console\Event\ConsoleExceptionEvent;
103+ use Symfony\Component\Console\ConsoleEvents;
104+
105+ $dispatcher->addListener(ConsoleEvents::EXCEPTION, function (ConsoleExceptionEvent $event) {
106+ $output = $event->getOutput();
107+
108+ $command = $event->getCommand();
109+
110+ $output->writeln(sprintf('Oops, exception thrown while running command <info>%s</info>', $command->getName()));
111+
112+ // get the current exit code (the exception code or the exit code set by a ConsoleEvents::TERMINATE event)
113+ $exitCode = $event->getExitCode();
114+
115+ // change the exception to another one
116+ $event->setException(new \LogicException('Caught exception', $exitCode, $event->getException()));
117+ });
118+
89119The ``ConsoleEvents::TERMINATE `` Event
90120--------------------------------------
91121
@@ -124,34 +154,4 @@ Listeners receive a
124154 It is then dispatched just after the ``ConsoleEvents::EXCEPTION `` event.
125155 The exit code received in this case is the exception code.
126156
127- The ``ConsoleEvents::EXCEPTION `` Event
128- --------------------------------------
129-
130- **Typical Purposes **: Handle exceptions thrown during the execution of a
131- command.
132-
133- Whenever an exception is thrown by a command, the ``ConsoleEvents::EXCEPTION ``
134- event is dispatched. A listener can wrap or change the exception or do
135- anything useful before the exception is thrown by the application.
136-
137- Listeners receive a
138- :class: `Symfony\\ Component\\ Console\\ Event\\ ConsoleExceptionEvent ` event::
139-
140- use Symfony\Component\Console\Event\ConsoleExceptionEvent;
141- use Symfony\Component\Console\ConsoleEvents;
142-
143- $dispatcher->addListener(ConsoleEvents::EXCEPTION, function (ConsoleExceptionEvent $event) {
144- $output = $event->getOutput();
145-
146- $command = $event->getCommand();
147-
148- $output->writeln(sprintf('Oops, exception thrown while running command <info>%s</info>', $command->getName()));
149-
150- // get the current exit code (the exception code or the exit code set by a ConsoleEvents::TERMINATE event)
151- $exitCode = $event->getExitCode();
152-
153- // change the exception to another one
154- $event->setException(new \LogicException('Caught exception', $exitCode, $event->getException()));
155- });
156-
157157.. _`reserved exit codes` : http://www.tldp.org/LDP/abs/html/exitcodes.html
0 commit comments