php exception handling technology the top exception handler

  • 2020-05-17 04:57:27
  • OfStack

The function used to define the top-level exception handler is
set_exception_handler("My_exception");
Here, My_expection is the developer's custom exception handler, which is the top-level exception handler. Only when there is no function in the program to handle the exception will there be a top-level exception handler to handle the exception. If there is no defined top-level exception handler, the system default exception handler will handle the exception

Examples:
 
<meta http-equiv="content-type" content="text/html;charset=utf-8" /><?php 
set_exception_handler("My_expection"); 
function My_expection(){ 
echo " Here is the top-level exception handler "; 
} 
try{ 
nohello("hello"); 
}catch(Exception $e){ 
throw $e; 
} 
  function nohello($nohello){ 
   if($nohello == "hello"){ 
   throw new Exception(" Can't input hello"); 
   }else{ 
echo " Input success "; 
} 
} 
?> 

A question about PHP exception handling
 
<?php 
try { 
$a=10/0; 
}catch (Exception $e){ 
echo " An exception is thrown "; 
} 
?> 

The above code does not output "throw exception", but an error message: Warning: Division by zero in...

Note: an exception is not thrown in the try block, but is handled according to the system's default error handling mechanism

So whether or not you catch an exception depends on whether or not you actually throw an exception

Related articles: