问题描述
MessageBox.Show("Unknown Error:" & Err().ToString & " " & Error(Err),6 "Call MIS")
这是我的代码,我在“ Error(Err)”部分遇到预期的错误,并且我一直在寻找大约一天的时间,但无法弄清。
解决方法
假设您的Err()
和Error(...)
是有效的东西(我想不出它们可能是什么; Err()看起来像一个函数,Error看起来像一个函数/数组,但不能使用单词Error
作为标识符...)
从此开始,简单:
MessageBox.Show($"Unknown Error: {Err()} {Error(Err)}")
然后添加标题:
MessageBox.Show($"Unknown Error: {Err()} {Error(Err)}","Call the police!")
然后更改按钮:
MessageBox.Show($"Unknown Error: {Err()} {Error(Err)}","Call the police?",MessageBoxButtons.YesNo)
然后捕获用户按下的按钮,并对其进行操作:
Dim r = MessageBox.Show($"Unknown Error: {Err()} {Error(Err)}",MessageBoxButtons.YesNo)
If r = DialogResult.Yes Then Call(police)
,
您对我的其他答案发表评论,这是我们在VB.net中进行错误处理的方式:
Try
'this code explodes with NullReferenceException
Dim x As String = Nothing
Dim y = x.Length
'this code explodes for different reasons (IndexOutOfRange)
Dim a As String = "a"
Dim b = a(100) 'get 101st character
Catch NullReferenceException nrex
MessageBox.Show("Something was nothing: " & nrex.Message)
Catch IndexOutOfRangeException ioorex
MessageBox.Show(string.Format("Something went too far: {0}",ioorex.Message))
Catch Exception ex
MessageBox.Show($"I've no idea what went wrong {ex.Message}")
End Try
我使用了3种不同的方式来生成错误消息(字符串连接,字符串格式,字符串插值),并展示了如何在同一代码中捕获多种不同类型的异常。我们可以是特定的(catch null ref)或通用的(exception)-通用将捕获任何属于异常子类的东西
,MessageBox.Show("Unknown Error:" & Err().ToString & " " & Error(Err),6 "Call MIS")
这可能是老式的VB6风格,无法在VB.NET中使用
MessageBox.Show("Unknown Error:" & Err.Description & " " & Err.Number,"Call MIS")
这是代码在VB.NET中的实际外观