Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/redis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Exception handling 为什么在switch语句之后抛出一个异常,并返回一个默认的case?_Exception Handling_Redis_Switch Statement_Case - Fatal编程技术网

Exception handling 为什么在switch语句之后抛出一个异常,并返回一个默认的case?

Exception handling 为什么在switch语句之后抛出一个异常,并返回一个默认的case?,exception-handling,redis,switch-statement,case,Exception Handling,Redis,Switch Statement,Case,在它的一个函数中有一个奇怪的约定—一个switch语句,带有默认值大小写,后跟一个外部异常: static unsigned int zipIntSize(unsigned char encoding) { switch(encoding) { case ZIP_INT_8B: return 1; case ZIP_INT_16B: return 2; case ZIP_INT_24B: return 3; case ZI

在它的一个函数中有一个奇怪的约定—一个switch语句,带有
默认值
大小写,后跟一个外部异常:

static unsigned int zipIntSize(unsigned char encoding) {
    switch(encoding) {
        case ZIP_INT_8B:  return 1;
        case ZIP_INT_16B: return 2;
        case ZIP_INT_24B: return 3;
        case ZIP_INT_32B: return 4;
        case ZIP_INT_64B: return 8;
        default: return 0;
    }
    assert(NULL);
    return 0;
}

是否有编译器无法优化assert和ending return语句以及执行这些语句的环境?此编码模式试图防御什么?

用于调试工具的“assert”关键字(如果定义了NDEBUG,则禁用该关键字)。因此,我的猜测是,上面的模式是为了防止将来代码中的更改,这将使switch语句不能覆盖所有选项。

只有在删除了
默认值
的情况下,这才有意义吗。不管怎样,正如我所说,断言的目的似乎是为了找到未来的bug(就像删除默认情况一样)。我不会写这个断言——它对我来说没有多大意义。