English 中文(简体)
PHP 每一 lo的例外情况
原标题:PHP exceptions in foreach loop

哪一种是正确的方式,在一种看上去的原样中执行“加拿大”例外(国名)。

foreach ($apis as $api)
{                 
     $api =  _ .$api;
     $searchResults[$api] = $this->$api($parameters);
}

如果$ this->api(); 回归错误信息,而不是追赶错误信息,如果在捕获中显示正确的错误信息的话,我就希望实施“php”的例外情况。

Edit: Also, when capturing the error and if the error message is 1 (for example) is it a good way to do:

$searchResults[ api ] = $this->_api($parameters);

因此,它试图再次履行这一职能,看它是否提供了有效数据?

最佳回答
foreach ($apis as $api)
{                 
     $api =  _ .$api;
     try {
         $searchResults[$api] = $this->$api($parameters);
     }
     catch(ParameterException $e) {
         // parameterexception handling here
         echo "A ParameterException was thrown";
     }
     catch(Exception $e) {
         // All other exceptions
         echo "Some other Exception was thrown";
     }
}

你们也可以区分较为例外的作物。

问题回答

由于捕获区只有在例外情况下才会执行,如果你在试场内划出每个游艇,或者如果你将试捕捕获物放在鱼体内,那么实际上不会有任何区别。

你们应当更加清晰。 然而,如果将来需要,则在休息室内这样做将使你能够处理与休息院有关的更具体的例外。

此外,由于例外情况被归类,如果只是规定不同的渔获条款,你就不需要做:

try {
    :
} catch (FirstExceptionType $e) {
    :
} catch (SecondExceptionType $e) {
    :
}

The code you use will always fail, because you are trying to use a variable ($this->$api($params);) as a function. How ever, you could implement your try-catch as follows:

foreach ($apis as $api)
{                 
     $api =  _ .$api;
     try {
         $searchResults[$api] = $this->$api($parameters);
     }
     catch(Exception $e) {
         // handle exception
     }
}

您还可处理不同种类的多种例外情形,在其中添加另一个例外类别fishs>

foreach ($apis as $api)
{                 
     $api =  _ .$api;
     try {
         $searchResults[$api] = $this->$api($parameters);
     }
     catch(OtherException $e) {
         // Handle it
     }
     catch(Exception $e) {
         // Handle it
     }

}




相关问题
handling exceptions IN Action Filters

Is there a better way to handle exceptions that occur inside an Action Filter itself in ASP .NET MVC? There re 2 ways I can think of at the moment. Using a try catch and setting the HTTP Status ...

既可捕获,又可举出例外。

我有一种办法,可以进入亚洲开发银行,因此,我国的亚行在多瑙河航道中的所有 st子都位于一个试捕区。 它正在追捕Kexception

Cross compiler exception handling - Can it be done safely?

I am doing some maintenance on a C++ windows dll library that is required to work with different VC++ compilers (as I don’t want to address different mangling schemes). I have already eliminated any ...

File Handling Issue

I am developing a tool in c#, at one instance I start writing into a xml file continuously using my tool,when i suddenly restart my machine the particular xml file gets corrupted, what is the reason ...

Watch a memory location/install data breakpoint from code?

We have a memory overwrite problem. At some point, during the course of our program, a memory location is being overwritten and causing our program to crash. the problem happens only in release mode. ...

Unit Test for Exceptions Message

Is there a simple (Attribute-driven) way to have the following test fail on the message of the exception. [TestMethod()] [ExpectedException(typeof(ArgumentException))] public void ExceptionTestTest() ...

热门标签