- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我一直在寻找问题的答案,但没有找到太多答案,因此我在这里发帖,希望更有经验的开发人员能够启发我。在子结构中构建异常处理的正确方法是什么?我应该将 Try、Catch、End Try 包装在整个代码块周围,还是可以将 Try、Catch、End Try 包装在子程序中的一个代码块周围,然后在另一 block 代码周围包装 Try、Catch、End Try在同一个子?这是我所要求的示例:
这样更好吗?:(选项 1)
Try
'check if user has selected a file
If ListBoxPrePublish.SelectedItems.Count = 0 Then
MessageBox.Show("Please select a file to delete.")
Exit Sub
End If
'check if user has selected a Memo to publish, if so, move file to Publish folder; if user hasn't selected a Memo, move to next If statement
If ListBoxPrePublish.SelectedItem.Contains("-M.xls") Then
Dim resultMemo = MessageBox.Show("Are you sure you want to delete this Memo?", "", MessageBoxButtons.YesNo)
If resultMemo = DialogResult.Yes Then
Dim filePath As String = "W:\TOM\ERIC\Award_Letters\Excel pre_publish\"
Dim movePath As String = "W:\TOM\ERIC\Award_Letters\Publish\"
Dim filenm As String = ListBoxPrePublish.SelectedItem
Dim FileToMove = filePath & filenm
Dim MoveToLocation = movePath & filenm
'move the file
System.IO.File.Move(FileToMove, MoveToLocation)
ListBoxPublished.Items.Add(ListBoxPrePublish.SelectedItem)
ListBoxPrePublish.Items.Remove(ListBoxPrePublish.SelectedItem)
Exit Sub
Else
Exit Sub
End If
End If
'ask user if they're sure they want to publish selected file and corresponding files...remember, only if the selected item is not a Memo
Dim result = MessageBox.Show("This action will publish all corresponding files for the selected student except for Memos. Memos must be deleted individually. Do you want to continue?", "", MessageBoxButtons.YesNo)
If result = DialogResult.No Then
Exit Sub
Else
If result = DialogResult.Yes Then
'find ID for student in unapprovd accounting file, move the record to the Deleted Today file, delete record from unapproved accounting file
Dim UnapprovedAcctFile As String = "W:\TOM\ERIC\Award_Letters\Accounting Files\Unapproved\" & StatVar.xlApp.Sheets("New Calculator Input").Range("D30").Value & ".xlsx"
Dim ApprovedAcctFile As String = "W:\TOM\ERIC\Award_Letters\Accounting Files\Approved\" & StatVar.xlApp.Sheets("New Calculator Input").Range("D30").Value & " Import" & ".xlsx"
Dim searchString As String = StatVar.xlApp.Sheets("Cross Checking").Range("J6").Text 'this is the ID that will be searched for in the accounting file
Dim rng
'make sure accounting file for school exists
If Not IO.File.Exists(UnapprovedAcctFile) Then
MessageBox.Show("Could not locate the accounting file for this school. Please contact an administrator for assistance.")
Exit Sub
End If
'open accounting file and check if it's read only, if so, another user is editing it so close it and tell the user to try again in a few seconds, if it's not read only, see next comment
StatVar.xlWBUnapprovedAcctFile = StatVar.xlApp.Workbooks.Open(UnapprovedAcctFile)
If StatVar.xlApp.ActiveWorkbook.ReadOnly = True Then 'check if file is read only, if so close it and exit sub
StatVar.xlApp.ActiveWorkbook.Close(False)
MessageBox.Show("The system is busy. Please try again in a few seconds.")
Exit Sub
End If
'search unapproved accounting file for ID (SSN-award year example: 555555555-13) - use searchString variable above
rng = StatVar.xlApp.ActiveWorkbook.Sheets("Sheet1").Range("BD2:BD30002").Find(searchString, , Excel.XlFindLookIn.xlValues, Excel.XlLookAt.xlWhole, Excel.XlSearchOrder.xlByRows, Excel.XlSearchDirection.xlNext, False)
'if searchString is found
If rng IsNot Nothing Then
rng.EntireRow.Select() 'select the row searchString was found in
'check if approved accounting file for this school exists, if not, create it
If Not IO.File.Exists(ApprovedAcctFile) Then
StatVar.xlApp.Workbooks.Add()
StatVar.xlApp.ActiveWorkbook.SaveAs(ApprovedAcctFile)
StatVar.xlApp.ActiveWorkbook.Close(True)
End If
StatVar.xlWBApprovedAcctFile = StatVar.xlApp.Workbooks.Open(ApprovedAcctFile) 'open the Approved file
If StatVar.xlApp.ActiveWorkbook.ReadOnly = True Then 'check if the Approved file is read only, if so, close it and exit sub because it needs to be writable
StatVar.xlApp.ActiveWorkbook.Close(False) 'close the Approved file, don't save changes
StatVar.xlApp.ActiveWorkbook.Close(False) 'close unapproved accounting file, don't save changes
MessageBox.Show("The system is busy. Please try again in a few seconds.")
Exit Sub
End If
StatVar.xlApp.ActiveWorkbook.ActiveSheet.Columns("AV").NumberFormat = "@" 'format column AV of the Approved file as text so "13/14" remains formatted correctly
StatVar.xlApp.ActiveWorkbook.ActiveSheet.Rows("1:1").Insert(Excel.XlInsertShiftDirection.xlShiftDown) 'insert a row at the top of the Approved file
StatVar.xlApp.ActiveWorkbook.ActiveSheet.Rows("1:1").Value = rng.EntireRow.Value 'set the 1st row of the Approved file equal to the row selected earlier in the unapproved accounting file
StatVar.xlApp.ActiveWorkbook.ActiveSheet.UsedRange.RemoveDuplicates(Columns:=(56)) 'search column 56 in Approved file for duplicates and remove entire row if a duplicate ID is found
rng.EntireRow.Delete() 'delete row the searchString was found in from the unapproved accounting file
StatVar.xlApp.ActiveWorkbook.Close(True) 'close the Approved file, save changes
StatVar.xlApp.ActiveWorkbook.Close(True) 'close unapproved accounting file, save changes
'MessageBox.Show("Eureka!")
Else
'if searchString is not found, close unapproved accounting file, inform user and exit sub
StatVar.xlApp.ActiveWorkbook.Close(False)
MessageBox.Show("Could not locate this record on the accounting file. Your files were not deleted.")
Exit Sub
End If
'move selected file and corresponding non-Memo files to Publish folder
Dim filePath As String = "W:\TOM\ERIC\Award_Letters\Excel pre_publish\"
Dim movePath As String = "W:\TOM\ERIC\Award_Letters\Publish\"
Dim filenm As String
'check if each item in listbox contains the string extracted from the selected item via Excel formula; listbox select event sends the filename to Excel and a formula extracts the string to find in each listbox item...look for this string
For Each c In ListBoxPrePublish.Items
If c.ToString.Contains(StatVar.xlApp.Sheets("Cross Checking").Range("H6").Text) Then
'make sure the current listbox item in this loop is not a Memo, if it's not, move it
If Not c.ToString.Contains("-M.xls") Then
filenm = c
Dim FileToMove = filePath & filenm
Dim MoveToLocation = movePath & filenm
System.IO.File.Move(FileToMove, MoveToLocation)
ListBoxPublished.Items.Add(c) 'add files files that are being published to the published files listbox
End If
End If
Next
'refresh prepublish list, if user has a school selected, REMOVE all list items that don't contain the school code being generated via VLOOKUP in Excel using the school name from comboboxschool; look for this string in the loop surrounded with "-"
Call ListFiles()
If ComboBoxSchool.Text <> "" Then
For i As Integer = ListBoxPrePublish.Items.Count - 1 To 0 Step -1
'if current loop item does not contain the school code for the selected school, remove the item from the listbox
If Not ListBoxPrePublish.Items(i).Contains("-" & StatVar.xlApp.Sheets("Cross Checking").Range("E3").Text & "-") Then
ListBoxPrePublish.Items.RemoveAt(i)
End If
Next
End If
'refresh deleted files listbox
Call ListDeletedTodayFiles()
End If
End If
Catch exc As Exception
MessageBox.Show("Unable to publish the selected file. Please verify this file or any of its corresponding files are not open by you or another user. If the problem persists, contact an administrator for assistance." & vbNewLine & vbNewLine & "Error: " & exc.Message)
End Try
或者这样更好吗?:(选项 2)
Try
'check if user has selected a file
If ListBoxPrePublish.SelectedItems.Count = 0 Then
MessageBox.Show("Please select a file to delete.")
Exit Sub
End If
'check if user has selected a Memo to publish, if so, move file to Publish folder; if user hasn't selected a Memo, move to next If statement
If ListBoxPrePublish.SelectedItem.Contains("-M.xls") Then
Dim resultMemo = MessageBox.Show("Are you sure you want to delete this Memo?", "", MessageBoxButtons.YesNo)
If resultMemo = DialogResult.Yes Then
Dim filePath As String = "W:\TOM\ERIC\Award_Letters\Excel pre_publish\"
Dim movePath As String = "W:\TOM\ERIC\Award_Letters\Publish\"
Dim filenm As String = ListBoxPrePublish.SelectedItem
Dim FileToMove = filePath & filenm
Dim MoveToLocation = movePath & filenm
'move the file
System.IO.File.Move(FileToMove, MoveToLocation)
ListBoxPublished.Items.Add(ListBoxPrePublish.SelectedItem)
ListBoxPrePublish.Items.Remove(ListBoxPrePublish.SelectedItem)
Exit Sub
Else
Exit Sub
End If
End If
Catch exc As Exception
MessageBox.Show("An error occured and the selected file was not published. Please contact an administrator if the problem continues." & vbNewLine & vbNewLine & "Error: " & exc.Message)
End Try
Try
'ask user if they're sure they want to publish selected file and corresponding files...remember, only if the selected item is not a Memo
Dim result = MessageBox.Show("This action will publish all corresponding files for the selected student except for Memos. Memos must be deleted individually. Do you want to continue?", "", MessageBoxButtons.YesNo)
If result = DialogResult.No Then
Exit Sub
Else
If result = DialogResult.Yes Then
'find ID for student in unapprovd accounting file, move the record to the Deleted Today file, delete record from unapproved accounting file
Dim UnapprovedAcctFile As String = "W:\TOM\ERIC\Award_Letters\Accounting Files\Unapproved\" & StatVar.xlApp.Sheets("New Calculator Input").Range("D30").Value & ".xlsx"
Dim ApprovedAcctFile As String = "W:\TOM\ERIC\Award_Letters\Accounting Files\Approved\" & StatVar.xlApp.Sheets("New Calculator Input").Range("D30").Value & " Import" & ".xlsx"
Dim searchString As String = StatVar.xlApp.Sheets("Cross Checking").Range("J6").Text 'this is the ID that will be searched for in the accounting file
Dim rng
'make sure accounting file for school exists
If Not IO.File.Exists(UnapprovedAcctFile) Then
MessageBox.Show("Could not locate the accounting file for this school. Please contact an administrator for assistance.")
Exit Sub
End If
'open accounting file and check if it's read only, if so, another user is editing it so close it and tell the user to try again in a few seconds, if it's not read only, see next comment
StatVar.xlWBUnapprovedAcctFile = StatVar.xlApp.Workbooks.Open(UnapprovedAcctFile)
If StatVar.xlApp.ActiveWorkbook.ReadOnly = True Then 'check if file is read only, if so close it and exit sub
StatVar.xlApp.ActiveWorkbook.Close(False)
MessageBox.Show("The system is busy. Please try again in a few seconds.")
Exit Sub
End If
'search unapproved accounting file for ID (SSN-award year example: 555555555-13) - use searchString variable above
rng = StatVar.xlApp.ActiveWorkbook.Sheets("Sheet1").Range("BD2:BD30002").Find(searchString, , Excel.XlFindLookIn.xlValues, Excel.XlLookAt.xlWhole, Excel.XlSearchOrder.xlByRows, Excel.XlSearchDirection.xlNext, False)
'if searchString is found
If rng IsNot Nothing Then
rng.EntireRow.Select() 'select the row searchString was found in
'check if approved accounting file for this school exists, if not, create it
If Not IO.File.Exists(ApprovedAcctFile) Then
StatVar.xlApp.Workbooks.Add()
StatVar.xlApp.ActiveWorkbook.SaveAs(ApprovedAcctFile)
StatVar.xlApp.ActiveWorkbook.Close(True)
End If
StatVar.xlWBApprovedAcctFile = StatVar.xlApp.Workbooks.Open(ApprovedAcctFile) 'open the Approved file
If StatVar.xlApp.ActiveWorkbook.ReadOnly = True Then 'check if the Approved file is read only, if so, close it and exit sub because it needs to be writable
StatVar.xlApp.ActiveWorkbook.Close(False) 'close the Approved file, don't save changes
StatVar.xlApp.ActiveWorkbook.Close(False) 'close unapproved accounting file, don't save changes
MessageBox.Show("The system is busy. Please try again in a few seconds.")
Exit Sub
End If
StatVar.xlApp.ActiveWorkbook.ActiveSheet.Columns("AV").NumberFormat = "@" 'format column AV of the Approved file as text so "13/14" remains formatted correctly
StatVar.xlApp.ActiveWorkbook.ActiveSheet.Rows("1:1").Insert(Excel.XlInsertShiftDirection.xlShiftDown) 'insert a row at the top of the Approved file
StatVar.xlApp.ActiveWorkbook.ActiveSheet.Rows("1:1").Value = rng.EntireRow.Value 'set the 1st row of the Approved file equal to the row selected earlier in the unapproved accounting file
StatVar.xlApp.ActiveWorkbook.ActiveSheet.UsedRange.RemoveDuplicates(Columns:=(56)) 'search column 56 in Approved file for duplicates and remove entire row if a duplicate ID is found
rng.EntireRow.Delete() 'delete row the searchString was found in from the unapproved accounting file
StatVar.xlApp.ActiveWorkbook.Close(True) 'close the Approved file, save changes
StatVar.xlApp.ActiveWorkbook.Close(True) 'close unapproved accounting file, save changes
'MessageBox.Show("Eureka!")
Else
'if searchString is not found, close unapproved accounting file, inform user and exit sub
StatVar.xlApp.ActiveWorkbook.Close(False)
MessageBox.Show("Could not locate this record on the accounting file. Your files were not deleted.")
Exit Sub
End If
'move selected file and corresponding non-Memo files to Publish folder
Dim filePath As String = "W:\TOM\ERIC\Award_Letters\Excel pre_publish\"
Dim movePath As String = "W:\TOM\ERIC\Award_Letters\Publish\"
Dim filenm As String
'check if each item in listbox contains the string extracted from the selected item via Excel formula; listbox select event sends the filename to Excel and a formula extracts the string to find in each listbox item...look for this string
For Each c In ListBoxPrePublish.Items
If c.ToString.Contains(StatVar.xlApp.Sheets("Cross Checking").Range("H6").Text) Then
'make sure the current listbox item in this loop is not a Memo, if it's not, move it
If Not c.ToString.Contains("-M.xls") Then
filenm = c
Dim FileToMove = filePath & filenm
Dim MoveToLocation = movePath & filenm
System.IO.File.Move(FileToMove, MoveToLocation)
ListBoxPublished.Items.Add(c) 'add files files that are being published to the published files listbox
End If
End If
Next
'refresh prepublish list, if user has a school selected, REMOVE all list items that don't contain the school code being generated via VLOOKUP in Excel using the school name from comboboxschool; look for this string in the loop surrounded with "-"
Call ListFiles()
If ComboBoxSchool.Text <> "" Then
For i As Integer = ListBoxPrePublish.Items.Count - 1 To 0 Step -1
'if current loop item does not contain the school code for the selected school, remove the item from the listbox
If Not ListBoxPrePublish.Items(i).Contains("-" & StatVar.xlApp.Sheets("Cross Checking").Range("E3").Text & "-") Then
ListBoxPrePublish.Items.RemoveAt(i)
End If
Next
End If
'refresh deleted files listbox
Call ListDeletedTodayFiles()
End If
End If
Catch exc As Exception
MessageBox.Show("Unable to publish the selected file. Please verify this file or any of its corresponding files are not open by you or another user. If the problem persists, contact an administrator for assistance." & vbNewLine & vbNewLine & "Error: " & exc.Message)
End Try
这些方法中的哪一种比另一种更好实践?或者这取决于我并且取决于我在程序中所做的事情?很想听到对此的不同意见。
最佳答案
将 Try-Catch-Finally
block 包裹在较小的代码部分中,这样您将有更好的机会更快地找到错误,并且还可能从异常中恢复并继续处理该部分你的逻辑。在您的一个异常处理程序场景中,如果发生任何错误,则不会执行其余的逻辑,这也使得查找错误发生的位置变得更加困难。这提出了最终的建议,让你的方法做得更少;需要滚动多个页面的方法做得太多,并且非常难以理解和维护,并且几乎肯定不可能在其他地方重用。
您也不应该只使用基本Exception
,因为这是一种包罗万象的异常类型,并且无法深入了解实际发生的更具体的异常类型。
关于vb.net - VB.NET 中正确的异常处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17844231/
我有几个问题。我是 Visual Basic 这个领域的新手,所以不要取笑我。 1.) VB.NET之间有什么区别和 VB ? 2.) 我需要为 Windows 开发基本的应用程序。(如记事本)我应该
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我是框架 3.5 的新手。我注意到,在创建 Web 内容表单时,除了 aspx.vb 页面之外,它还会创建一个 aspx.designer.vb 页面。谁能向我解释一下它们之间的区别以及它们的用途吗?
我只是想知道 VB.NET 和 VB 2010 是否相同。 我只是想知道。 最佳答案 VB 2010 是 VB.Net 的最新版本。 Microsoft 在 VB 2005 版本中删除了 VB 的“.
我是框架 3.5 的新手。我注意到,在创建 Web 内容表单时,除了 aspx.vb 页面之外,它还会创建一个 aspx.designer.vb 页面。谁能向我解释一下它们之间的区别以及它们的用途吗?
我正在尝试将 VB 函数移植到 VB.NET,但我无法使该函数正常工作并正确更新。 rFormat = Format(Format(Value, fmt), String$(Len(fmt), "@"
如何在VB中注释多行代码/代码块? 最佳答案 VB 在语言级别上没有这样的构造。它有使用撇号字符的单行注释: ' hello world ' this is a comment Rem this is
我正在使用我在 VB2005 中创建的表单在按下按钮时打开程序,然后在文本字段中显示进程 ID(再次按下按钮时)。当我运行它时,表单将打开程序 (Notepad.exe) 但当我单击按钮查看进程 ID
我正在尝试添加一个从 vb.net 创建的 dll,并且想将其导入到现有的 vb 6 项目中,但它给了我错误“无法添加对指定文件的引用”。 。有人知道如何解决这个问题吗? 最佳答案 需要遵循以下步骤:
我有一个数据 GridView 。右键单击它会显示一个上下文菜单,但它始终位于右上角。我想要它,以便菜单出现在用户右键单击的单元格上。它可能是单元格 1 或单元格 2 或其他。 谢谢福尔坎 最佳答案
我只是在 Visual Studio 2010 中使用 Visual Basic。有人知道我将如何制作“浏览文件夹(或文件)”按钮吗?我对 VB 真的很陌生,我只是在寻找一些简单的帮助:) 最佳答案
这次感到困惑... 最简单的代码行有时可能起作用,有时却没有。首先,我认为问题在于我试图读取DWORD的值,但是由于我可以从某些键读取DWORD值,所以这一定不是问题。现在的问题似乎是,如果 key
我的代码中有此方法: Private Sub Display() Received.AppendText(" - " & RXArray) End Sub 这两个调用之间有什么区别:
我正在创建一个宏程序来记录和回放鼠标和键盘输入。录制效果很好,鼠标播放也一样,但是我在播放键盘输入时遇到了麻烦——特别是在释放之前按住一个键几秒钟。这不等同于重复按键。这是我尝试过的: 技巧 1:Me
我最近刚刚了解了 VB.NET 中静态局部变量的使用,并想知道它在延迟加载属性中的潜在用途。 考虑以下示例代码。 Public Class Foo Implements IFoo End Clas
VB 有一个 C# 没有的特性,在项目级别导入命名空间(我的项目>引用>导入命名空间)。当新人在源代码控制之外检查项目时,我们的自定义导入不包括在内。这个 VB 特定的导入命名空间存储在哪里? 最佳答
我已将我的问题缩小到这个简单的案例,但似乎无法找到发生了什么: 我有两个表单,一个只有一个按钮,另一个是空的。 单击按钮时,form1 隐藏和显示 form2 出现时,form2隐藏,form1再次显
为什么下面的简单代码会失败?无论我使用 LinearGradientMode 的哪个值,这段代码总是用从左到右的渐变填充路径。 graphPath 是在别处创建的 GraphicPath 对象(基本上
我可以多快替换字符串中的字符? 所以这个问题的背景是这样的:我们有几个应用程序通过套接字相互通信并与客户端的应用程序通信。这些套接字消息包含不可打印的字符(例如 chr(0)),需要用预定的字符串(例
如何从任何文件中读取原始字节数组... Dim bytes() as Byte ..然后将该字节数组写回新文件? 我需要它作为一个字节数组来做一些处理。 我目前正在使用: 阅读 Dim fInfo
我是一名优秀的程序员,十分优秀!