dongmei5168 2013-01-31 20:10
浏览 20
已采纳

PHP:如何在下面的情况下处理异常

I don't have enough experience with PHP and programming at all, so I want to know, what the best decision in my situation is. Here is my code sample:

public static function isAscii($string, $type, $length = -1)
    {
        if (!is_string($string))
            throw new InternalException(ExceptionMessage::invalidFunctionParameterType(__CLASS__, __METHOD__));

        if ($length !== -1 && !Str::isValidLength($string, $length))
            return FALSE;

        $ascii_array = Constants::asciiRegex();

        if (!preg_match($ascii_array[$type], $string))
            return FALSE;

        return TRUE;
    }

I want to ensure that key $type exists for array $ascii_array. What is the best way to do that?

I can offer two decisions:

  1. The simplest. Just check key existence manually with array_key_exists() function. If key doesn't exist - throw an exception, also manually. But I think that is not a good idea and checking this condition is redundant, because PHP compiler will generate warning if I don’t write anything (Notice: Undefined index:) and I want to use this somehow. I found there are some predefined exceptions like OutOfBoundsException and OutOfRangeException, it would be easy just to use them like in Java, but it seems that PHP doesn’t throw these exceptions, only warnings and errors.
  2. Use set_error_handler() function to 'convert' errors and warnings to exceptions. But there is another problem - how can I handle all possible errors? I tried to find the complete list of PHP errors and warnings, but it doesn't exist. So I have no idea how to solve this problem, please help me. Thanks in advance, hope you will understand my question even with my poor English level.

P.S. this is an array with regular expressions for different types of ascii strings.enter image description here

  • 写回答

1条回答 默认 最新

  • dongque1958 2013-01-31 20:22
    关注

    I hope I'm understanding this question correctly - Are you basically asking if there is a natural internal exception to adopt instead of running your own exception?

    Or are you just not aware you can do something like:

    try
    {
        //some code to try
        if (!in_array($type, $ascii_array)) {
            throw new Exception('type not in ascii array');
        }
    }
    catch(Exception $e) {
        die ('my custom exception message: ' . $e->getMessage());
    }
    

    if i missed the point, let me know and I may update.

    As for a reason to not throw your own exceptions... I can't say I know of a real benefit to this. It would seem to me that in_array() is perfectly acceptable and throwing exceptions may just be personal preference and not necessarily a worthwhile concern for performance.

    EDIT

    Considering my answer and the question, it is noteworthy that this answer was accepted likely due to the conclusion in the comments that PHP does not allow you to declare functions that throw exceptions like Java does. Simply put.

    So this answer is to serve only that purpose. Can PHP declare a function that throws an exception? No, not by default. You have to try and catch the exceptions yourself within the function.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题