小编zhk*_*yth的帖子

PHP异常被捕获,但仍然出现错误信息

我使用 PHP 手册中的一些代码进行异常测试,但收到一些奇怪的消息。

这是代码:

function inverse($x) {
    if (!$x) {
        throw new Exception('Division by zero.');
    }
    else return 1 / $x;
}

try {
    echo inverse(5) . "\n";
    echo inverse(0) . "\n";
} catch (Exception $e) {
    echo 'Caught exception: ',  $e->getMessage(), "\n";
}

// Continue execution
echo 'Hello World';
Run Code Online (Sandbox Code Playgroud)

这是输出:

0.2
( ! ) Exception: Division by zero. in /var/www/OOPlearing/slash.php on line 10Call Stack#TimeMemoryFunctionLocation10.0002330188{main}(  )../slash.php:020.0002330232inverse( $x = 0 )../slash.php:17Dump $_SERVER$_SERVER['HTTP_HOST'] =string 'localhost' (length=9)$_SERVER['SERVER_NAME'] =string 'localhost' (length=9)Dump $_GETVariables in local …
Run Code Online (Sandbox Code Playgroud)

php error-handling exception

5
推荐指数
1
解决办法
3290
查看次数

更改Javascript的getTimezoneOffset以返回不同的时区偏移量?

有没有办法更改Javascript的getTimezoneOffset以返回不同的时区偏移量?

Fox的例子,我当地的时区是GMT-8,但我希望Date.getTimezoneOffset()返回GMT-1,所以我这样做如下:

var timeZone1 = new Date(Date.parse("2011-01-01T00:00:00-01:00"));
document.write("The local time zone is: GMT " + timeZone1.getTimezoneOffset()/60);
//show:The local time zone is: GMT-8
Run Code Online (Sandbox Code Playgroud)

那么,如何让它显示GMT-1?

参考链接:

https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date/getTimezoneOffset

javascript

3
推荐指数
2
解决办法
2万
查看次数

AttributeError:'模块'对象没有属性'TemporaryFile'

我是python的新手,我正在学习标准库。

每当我运行下面的代码时,它总会引发,AttributeError... 并且import命令似乎出了点问题。

另外,我尝试在交互式解释器上运行它,并且效果很好。

样例代码

import tempfile
import os

#temp = tempfile.TemporaryFile()
temp = tempfile.mktemp()

print "tempfile","=>",temp

file = open(temp,"w+b")
file.write("*" * 1000)
file.seek(0)
print len(file.read()),"byte"
file.close()

try:
   os.remove(temp)
except OSError:
   pass
Run Code Online (Sandbox Code Playgroud)

错误输出

Traceback (most recent call last):
  File "tempfile.py", line 1, in <module>
    import tempfile
  File "/home/zhkzyth/codeRep/pytest/tempfile.py", line 5, in <module>
    tempfile = tempfile.mktemp()
AttributeError: 'module' object has no attribute 'mktemp'
Error in sys.excepthook:
Traceback (most recent call last):
  File "/usr/lib/python2.7/dist-packages/apport_python_hook.py", line 66, in apport_excepthook
    from apport.fileutils import …
Run Code Online (Sandbox Code Playgroud)

python import

2
推荐指数
1
解决办法
1万
查看次数

标签 统计

error-handling ×1

exception ×1

import ×1

javascript ×1

php ×1

python ×1