python的异常捕捉堆栈信息stacktrace,traceback

打印报错的行列数与位置

def by2by2(num):
    """

    :param num:
    """
    try:
        num2 = int(num)
        for bb in range(1, num2 + 1):

            yy2 = bb
            for bb2 in range(1, bb + 1):
                result2 = yy2 * bb2
                print(str(bb2) + "*" + str(yy2) + "=" + str(result2) + " ", end="")  # end表示不换行

            print()
    except Exception as e:
        print("输入错误,请输入数字:", e)
        import traceback
        traceback.print_stack()
        traceback.print_exc()

 

posted @ 2023-12-27 09:34  txwtech  阅读(94)  评论(0编辑  收藏  举报