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