【发布时间】:2014-03-10 17:50:39
【问题描述】:
我在一些电子邮件发送逻辑周围放置了一个 try/catch 块。如果邮件成功,它会给出一个确认信息,如果它失败,它会给出一个失败信息。 Visual Studio 警告我该函数不会在所有代码路径上返回值。我是否需要在每个 Try 和 Catch 块中放置 return 语句?如果我这样做了,例如将 False 或 Null 的 Return 语句放在 Try 和 Catch 的末尾,那么 Return 语句之前的其他代码还会执行吗?
Function Sendmail(ByVal subject As String, ByVal msg As String, ByVal fromAddress As String, ByVal toAddress As String)
Try
Dim message As New MailMessage
message.From = New MailAddress(fromAddress)
For Each s As String In toAddress.Split(New [Char]() {";"c})
message.To.Add(New MailAddress(s))
Next
message.Subject = subject
message.Body = msg
message.IsBodyHtml = False
Dim client As New SmtpClient
client.Send(message)
pnlEmailSuccess.Visible = True
Catch ex As Exception
pnlEmailSuccess.Visible = False
pnlEmailError.Visible = True
lblErrorMsg.Text = ex.ToString
End Try
End Function
【问题讨论】:
-
如果您不需要返回值,请将其更改为
Sub而不是Function。 -
你可以将返回添加到最后,所以它会一直运行(如果你需要返回)