0

It appears I cannot throw exception in async methods:

void Start ()
{
    ReadAndThrow(null);
}

public static async Task ReadAndThrow(string path)
{
    if(string.IsNullOrEmpty(path) == true) 
    { 
         Debug.Log("Wrong"); 
         throw new Exception("Wrong"); 
    }
    using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read))
    {
        using (StreamReader reader = new StreamReader(fs, Encoding.UTF8))
        {
            string line = null;
            while ((line = await reader.ReadLineAsync()) != null) { }
        }
    }
}

With this, I can see the debug but the exception does not print in the console. It does stop the application, basically, the exception happens but is not printed in the console.

I can use a try catch and print the error, but why is the exception omitted from console? It does not show in the error section, I checked I would miss it from there.

How could I get the exception to print?

EDIT:

Latest used code, not printing:

async Task Start()
{
    await ReadAndThrow(null);
}
async Task ReadAndThrow(string path)
{
    Debug.Log("Start");
    try
    {
        if (string.IsNullOrEmpty(path) == true) { Debug.Log("Wrong"); throw new Exception("Wrong in"); }
    }catch(Exception e)
    {
        Debug.Log("Method" + e.Message);
        throw;
    }
    using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read))
    {
        using (StreamReader reader = new StreamReader(fs, Encoding.UTF8))
        {
            string line = null;
            while ((line = await reader.ReadLineAsync()) != null)
            {

            }
        }
    }
}
Everts
  • 10,408
  • 2
  • 34
  • 45
  • 5
    Did you forget to await `ReadAndThrow`? – mjwills Mar 13 '19 at 08:49
  • Converted Start to async Task, added await to ReadAndThrow and still nothing. – Everts Mar 13 '19 at 09:01
  • `string.IsNullOrEmpty(path) == true` waht? [Don't compare to literal boolean values](https://stackoverflow.com/questions/2661110/is-it-bad-to-explicitly-compare-against-boolean-constants-e-g-if-b-false-i). – Erik Philips Mar 13 '19 at 09:01
  • Could you show us that code? Did you try putting a `try catch` inside `Start`? – mjwills Mar 13 '19 at 09:03
  • Try/catch in start won't do, try/catch in method will get the exception and print a debug message. Throw in catch will not do. – Everts Mar 13 '19 at 09:12
  • @Everts, try my answer below, becuase its give me an exception. – er-sho Mar 13 '19 at 09:13
  • 1
    You keep describing the results **without showing the code**. Every time you try something, copy and paste the code into a new dotnetfiddle, so we can see the code you are trying. – mjwills Mar 13 '19 at 09:13
  • @Everts, it give me an exception `System.AggregateException: 'One or more errors occurred.'` and in that inner exception is `Wrong` – er-sho Mar 13 '19 at 09:15
  • I'm thinking it could be a setting in the debug of VS since others are getting it. – Everts Mar 13 '19 at 09:16
  • Back to this, as mentioned on this video https://www.youtube.com/watch?v=7eKi6NKri6I at 9:20, exception can be hidden. I guess that's what I had. Special thanks to whoever downvoted my question, probably thinking I misused the feature while it was a Unity limitation. – Everts Oct 15 '19 at 11:00

3 Answers3

3

ReadAndThrow will finish before the exception is printed. Need to await it.

Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
Joep
  • 41
  • 4
  • I placed a "Done" debug after the method in Start and it shows that the "Wrong" debug is first printed then the "Done" but the exception is missing. – Everts Mar 13 '19 at 09:08
  • Minor typo may someone move the back tick so we have `await` instead of `awai`t – xdtTransform Mar 15 '19 at 08:14
0

You need to await your method by modifying the signature of Start() method

async Task Start()
{
    await ReadAndThrow(null);
}
er-sho
  • 9,581
  • 2
  • 13
  • 26
0

Fixes need to make to get it work. The magic is you need to wait until ReadAndThrow will be finished. Also I recommend to read https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/async/

public Task StartAsync ()
{
    return ReadAndThrow(null); //and await StartAsync method
    //or
    await ReadAndThrow(); // do not forget to make method async 
}
Yauhen Sampir
  • 1,989
  • 15
  • 16