捕获不同的异常类型 [英] Catch different Exception types

查看:158
本文介绍了捕获不同的异常类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常简单的功能来检查一个实体是否存在于一个bundle中:

  public function checkExists($ bundle, $ val)
{
try {
$ this-> em-> getRepository($ bundle。':'。$ val);
} catch(MappingException $ e){
return false;
}

返回true;
}

所以我有以下情况:

 输入|预期|实际
'AppBundle','公司'|真| true
'AppBundle','NONEXISTANT'|假| false(MappingException catch)
'NONEXISTANT','Company'|假| 500(ORMException未被捕获)
'NONEXISTANT','NONEXISTANT'|假| 500(ORMException未被捕获)

所以我看到问题是抛出不同的异常,但对于一部分不存在的情况,我怎么能返回假?有一种一般的方式来捕获symfony中的异常,因为 catch(Exception $ e)使用Symfony\Component\Config\Definition \Exception\Exception; 没有抓住它。

解决方案

有几件事情do:
你可以先捕获所有的异常,然后你可以不同地处理每个异常:

  public function checkExists($ bundle,$ val)
{
try {
$ this-> em-> getRepository($ bundle。':'。$ val);
} catch(\Exception $ e){// \Exception是全局范围异常
if($ e instanceof MappingException || $ e instanceof ORMException){
return false;
}
throw $ e; //如果您无法处理,请重新发送。
}

返回true;
}

替代方案有多个catch:


$ b $ ($ bundle,$ val)
{
try {
$ this-> em-> getRepository($捆绑 ':' $ VAL);
} catch(MappingException $ e){
return false;
} catch(ORMException $ e){
return false;
} //其他异常仍然未被处理。

返回true;
}

如果您使用的是PHP 7.1 +,那么您还可以: p>

  public function checkExists($ bundle,$ val)
{
try {
$ this- > EM-> getRepository($束。 ':' $ VAL);
} catch(MappingException | ORMException $ e){//捕获MappingException或ORMException
return false;
} //其他异常仍然未被处理。

返回true;
}


I have a very simple function to check whether an Entity exists in a bundle:

public function checkExists($bundle, $val)
{
    try{
       $this->em->getRepository($bundle.':'.$val);
    }catch (MappingException $e){
        return false;
    }

    return true;
}

So I have the following cases:

Input                        |    Expected    |    Actual
'AppBundle', 'Company'       |    true        |    true
'AppBundle', 'NONEXISTANT'   |    false       |    false (MappingException caught)
'NONEXISTANT', 'Company'     |    false       |    500 (ORMException not caught)
'NONEXISTANT', 'NONEXISTANT' |    false       |    500 (ORMException not caught)

So I see that the problem is that there are different exceptions thrown, but how could I return false for either of the cases of one part non-existant? Is there a "general" way to catch exceptions in symfony as catch (Exception $e) with use Symfony\Component\Config\Definition\Exception\Exception; does not catch it.

解决方案

There's a couple of things to do: You can catch all exceptions firstly, then you can handle each one differently:

public function checkExists($bundle, $val)
{
    try{
       $this->em->getRepository($bundle.':'.$val);
    } catch (\Exception $e){ // \Exception  is the global scope exception
        if ($e instanceof MappingException || $e instanceof ORMException) {
            return false;
        } 
        throw $e; //Rethrow it if you can't handle it here.
    }

    return true;
}

Alternatevely have multiple catches:

 public function checkExists($bundle, $val)
{
    try{
       $this->em->getRepository($bundle.':'.$val);
    } catch (MappingException $e){
       return false;
    } catch (ORMException $e) {
       return false;
    }  //Other exceptions are still unhandled.

    return true;
}

If you're using PHP 7.1 + then you can also do:

public function checkExists($bundle, $val)
{
    try{
       $this->em->getRepository($bundle.':'.$val);
    } catch (MappingException | ORMException $e){ //Catch either MappingException or ORMException 
       return false;
    }  //Other exceptions are still unhandled.

    return true;
}

这篇关于捕获不同的异常类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆