相关疑难解决方法(0)

如何在Python中打印异常?

try:
    something here
except:
    print('the whatever error occurred.')
Run Code Online (Sandbox Code Playgroud)

如何在except:块中打印错误/异常?

python error-handling exception

609
推荐指数
9
解决办法
70万
查看次数

python异常消息捕获

import ftplib
import urllib2
import os
import logging
logger = logging.getLogger('ftpuploader')
hdlr = logging.FileHandler('ftplog.log')
formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
hdlr.setFormatter(formatter)
logger.addHandler(hdlr)
logger.setLevel(logging.INFO)
FTPADDR = "some ftp address"

def upload_to_ftp(con, filepath):
    try:
        f = open(filepath,'rb')                # file to send
        con.storbinary('STOR '+ filepath, f)         # Send the file
        f.close()                                # Close file and FTP
        logger.info('File successfully uploaded to '+ FTPADDR)
    except, e:
        logger.error('Failed to upload to ftp: '+ str(e))
Run Code Online (Sandbox Code Playgroud)

这似乎不起作用,我得到语法错误,这是什么正确的方法来记录文件的所有类型的异常

python logging exception except

444
推荐指数
14
解决办法
56万
查看次数

如何在Python中找到引发异常的位置

如何确定引发了什么函数异常.例如,存在两个函数:'foo'和'bar'.在'foo'中,异常将随机提出.

import random


def foo():
    if random.randint(1, 10) % 2:
        raise Exception
    bar()


def bar():
    raise Exception

try:
    foo()
except Exception as e:
    print "Exception raised in %s" % ???
Run Code Online (Sandbox Code Playgroud)

python exception

4
推荐指数
2
解决办法
1528
查看次数

标签 统计

exception ×3

python ×3

error-handling ×1

except ×1

logging ×1