检测是否在不使用自定义异常类的情况下手动抛出异常

I got a try-catch block in my php application like this:

try {
  if ($userForgotToEnterField) {
     throw new Exception('You need to fill in your name!');
  }
  ...
  doDifferentThingsThatCanThrowExceptions();
  ...
} catch (ExpectedException $e) {
  $template->setError('A database error occured.');
} catch (Exception $e) {
  $template->setError($e->getMessage());
}

I would like to only output $e->getMessage() for the exceptions I have manually thrown with a custom error text and not the ones that have been thrown by the other code as these might contain sensitive information or very technical info that the user should not see.

Is it possible to differentiate from a manually thrown exception and a random exception thrown by some method without using a custom exception class?

I've thought about this a bit and I'd say that what you are doing DOES call for a custom exception class. If you want to get around it (which in the end is going to be more confusing), you would basically create a global (or same-scope) variable that all exceptions can modify, and in your throw block flag it.

$threwCustomException = false;

try {
  if ($userForgotToEnterField) {
     throw new Exception('You need to fill in your name!');
     $threwCustomException = true;
  }
  ...
  doDifferentThingsThatCanThrowExceptions();
  ...
} catch (ExpectedException $e) {
  $template->setError('A database error occured.');
} catch (Exception $e) {
    if($threwCustomException){
        //Whatever custom exception handling you wanted here....
    }
  $template->setError($e->getMessage());
}

That's the best I can think of. However, this is a bad idea, and it's the whole reason you are allowed to create your own exception classes. I know you're not looking for this answer, but since you look like you're trying not to create a TON of extra code, I would just extend Exception to "CustomException" or some other name specific to your project, and throw that for all cases, and handle it that way. Hope that helps.

I agree that it might be best to just write your own exceptions. If for whatever reason you don't want to, you could set a custom error message and a custom error code (the second parameter for the Exception constructor.) Check each thrown Exception if the error code is yours, and display only those:

public Exception::__construct() ([ string $message = "" [,int $code = 0[, Exception $previous = NULL ]]] )

and then use getCode