Consider the following code
```c# static async Task Main(string[] args) { if (File.Exists("Log.txt")) File.Delete("Log.txt");
File.AppendAllText("Log.txt", "Main 1\r\n");
var task = AnAsyncOperation();
File.AppendAllText("Log.txt", "Main 2\r\n");
Thread.Sleep(TimeSpan.FromSeconds(1));
File.AppendAllText("Log.txt", "Main 3\r\n");
await task;
File.AppendAllText("Log.txt", "Main 4\r\n");
}
public static async Task AnAsyncOperation() { throw new ApplicationException("An unexpected exception."); } ```
Do you think the entry Main 3 gets written to the log file, or will the exception be throw before that line of code gets reached?
As you should have just found, the exception isn't thrown until you await t.
You can think of the await keyword working as follows. Assume that Task
await could then be written as
c#
t.ExtractValue (
((System.AggregateException ae) => throw ae.innerException),
((T result) => return result )
);
These functions are then only called once the task has completed (either successfully or with a fault).
Note, this is a way of thinking about it, rather than how it is actually implemented.