如何在PHP中处理此ErrorException

I'm trying to format user input with the following code:

$userInput="blalalbla";//assume the user is inputing wrong data, they are supposed to input it like "12:30"
try{

  $timeStr=explode(":",$userInput);
  $time=(new Datetime())->setTime($timeStr[0],$timeStr[1]);
}catch(ErrorException $e){

}

However, if the input is not in the right format, laravel4 will always fire an ErrorException and I have no way of catching it. Since the user input can be wrong in different ways, I thought this was the most elegant way of handling validation. As ridiculous as it sounds like, ErrorExceptions seem to be un-catchable. What other options do I have?

set a global error handler

set_exception_handler (handler);

function handler($e) {
    if($e instanceof TheExceptionYouWantToHandle) {
        //then handle it
    }
}

The actual error received in your code is a PHP Notice. You can't catch it because it isn't an exception at that point in time. Laravel defines the class Illuminate\Exception\Handler and uses PHP's set_error_handler() to turn PHP errors into ErrorExceptions.

To use a try/catch block in your controller, you'll have to throw the exception yourself in that area (or use code that throws exceptions). However, as most have commented, you should be doing appropriate input checking and sanitation prior to actually using the input in any code. Whether or not poor input throws exceptions is totally up to you.