错误处理问题,似乎无法识别错误

时间:2019-04-10 01:09:49

标签: python python-3.x python-3.7

您好,问题是当我尝试在python中运行try Expect块时,似乎无法捕获我的错误。

该代码捕获了一个错误,但没有捕获到我设置的错误,我尝试了if else语句捕获该错误,现在尝试捕获,但是似乎它没有被传递到那些块中。

Option Explicit
Sub sortAlphaNum()
    Dim ws As Worksheet, r As Range
    Dim wsSort As Worksheet
    Dim vSrc As Variant, vToSort As Variant
    Dim RE As Object, MC As Object
    Const sPat As String = "(\d+)-?(\D*)" 'note that some do not have a hyphen
    Dim I As Long, V As Variant

'input data to variant array
Set ws = Worksheets("Telecom")
With ws
    vSrc = .Range(.Cells(1, 2), .Cells(.Rows.Count, 2).End(xlUp)).Resize(columnsize:=2)
End With

'create array of ColB, and Col C split into Numeric, Alpha & len(alpha) for column c
'cannot split column 2 on the hyphen since not all requiring a split contain a hyphen.

ReDim vToSort(1 To UBound(vSrc, 1), 1 To 7)

Set RE = CreateObject("vbscript.regexp")
With RE
    .Global = False
    .ignorecase = False 'or could be true
    .Pattern = sPat
End With
For I = 1 To UBound(vSrc, 1)
    Set MC = RE.Execute(vSrc(I, 2))
        vToSort(I, 1) = vSrc(I, 1)
            V = Split(vSrc(I, 1), "-")
        vToSort(I, 2) = V(0)
        vToSort(I, 3) = V(1)
    Set MC = RE.Execute(vSrc(I, 2))
        vToSort(I, 4) = vSrc(I, 2)
        vToSort(I, 5) = MC(0).submatches(0)
        vToSort(I, 6) = MC(0).submatches(1)
        vToSort(I, 7) = Len(vToSort(I, 6))
Next I

'write to hidden sheet for sorting
Set wsSort = Worksheets.Add
With wsSort
    .Visible = xlSheetHidden
    Set r = .Cells(1, 1).Resize(UBound(vToSort, 1), UBound(vToSort, 2))
    r.Value = vToSort
End With

'sort on the hidden sheet
wsSort.Sort.SortFields.Clear
    wsSort.Sort.SortFields.Add2 Key:=r.Columns(2) _
        , SortOn:=xlSortOnValues, Order:=xlAscending, CustomOrder:="BMC,CSR,MC,LC" _
        , DataOption:=xlSortNormal
    wsSort.Sort.SortFields.Add2 Key:=r.Columns(3) _
        , SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
    wsSort.Sort.SortFields.Add2 Key:=r.Columns(5) _
        , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    wsSort.Sort.SortFields.Add2 Key:=r.Columns(7) _
        , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    wsSort.Sort.SortFields.Add2 Key:=r.Columns(6) _
        , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    With wsSort.Sort
        .SetRange r
        .Header = xlNo
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With

'create results array with the needed columns
Dim vRes1 As Variant, vRes2 As Variant
Set r = Union(r.Columns(1), r.Columns(4))

vRes1 = r.Areas(1)
vRes2 = r.Areas(2)

'write back to the original sheet
'but offset for now for trouble shooting
Set r = Worksheets("Telecom").Cells(1, 5).Resize(UBound(vRes1, 1), 2)
With Application
    .ScreenUpdating = False
With r
    .EntireColumn.Clear
    .Columns(1).Value = vRes1
    .Columns(2).Value = vRes2
    .EntireColumn.HorizontalAlignment = xlCenter
    .EntireColumn.AutoFit
End With

'delete the hidden sheet
    .DisplayAlerts = False
        wsSort.Delete
    .DisplayAlerts = True
    .ScreenUpdating = True
End With

End Sub

我希望输出import sys INVALID_NUM_ARGS_ERROR = "At least two arguments not provided" INVALID_NUM_ERROR = "Invalid number provided" def your_main_program(args): # Call your methods from here, using the # relevant items in args num1 = int(sys.argv[1]) num2 = int(sys.argv[2]) try: if len(sys.argv) == 3: multiply(num1, num2) elif len(sys.argv) == 4: if(num1 or num2 == 0): raise_string(num1, num2) formula = '' formula += (str(num1) + ' * ') * (num2 - 1) formula += str(num1) print(formula) raise_string(num1, num2) except ValueError: print(INVALID_NUM_ERROR) ,但实际输出却是[Invalid number provided]

提高字符串代码

[Traceback (most recent call last):]

跟踪*

def raise_string(num1, num2):
    "Prints our the two numbers to the power of itself"
    answer = num1**num2
    print(answer)
    return answer

1 个答案:

答案 0 :(得分:0)

您的代码可能如下所示:

${filename}.pdf