Visual Studio 2015. If I write code in C #, then everything works:
private async void Form1_Load(object sender, EventArgs e) { try { await Task.Delay(1000); this.Text = "Text from try"; throw new Exception(); } catch (Exception) { await Task.Delay(1000); this.Text = "Text from catch"; } finally { await Task.Delay(1000); this.Text = "Text from finally"; } }
But if on VB.NET, then I get a compilation error:
Error BC36943 'Await' cannot be used inside a 'Catch' statement, a 'Finally' statement, or a 'SyncLock' statement.
Public Class Form1 Private Async Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load Try Await Task.Delay(1000) Me.Text = "Text from Try" Throw New Exception() Catch ex As Exception Await Task.Delay(1000) Me.Text = "Text from Catch" Finally Await Task.Delay(1000) Me.Text = "Text from Finally" End Try End Sub End Class
Why is it not compiled? Am I doing something wrong?
await
incatch
appeared only in C # 6 - maybe in VB it wasn’t a ride yet ? - VladD