-1

I have got follow Dart code:

try 
{
  print("!1111111111");
  await db.KVToTableInsert(tableToInsert);
  print("!2222222222");
  return dbEnums.success;
} on PostgreSQLException 
{
  print("Fail is here");
}

In console I am getting:

!1111111111
Unhandled exception:
PostgreSQLSeverity.unknown 23505

I tried to: on Exception {print("Base Exception");} but this code was not executed.

in KVToTableInsert I am getting Exception:

KVToTableInsert(String tablename) async { 
 // ... 
 List<List<dynamic>> results = await connection.query(sql); // PostgreSQLException (PostgreSQLSeverity.unknown 23505
}

As I understand the exception is should be handled on top level.

Why handling exception do not work?

postgresql 13

Screenshot: https://images.guru/i/dBWpe

KVToTableInsert with added await-async https://gist.github.com/bubnenkoff/4045c578497e9a9b108a7d62a8ae0ad5#file-foo-dart-L189 (same problem)

Dmitry Bubnenkov
  • 9,415
  • 19
  • 85
  • 145

2 Answers2

1

try this:

try {
  // ···
} on Exception catch (e) {
  print('Exception details:\n $e');
} catch (e, s) {
  print('Exception details:\n $e');
  print('Stack trace:\n $s');
}

See more at: https://dart.dev/guides/language/language-tour#catch

Son Tran
  • 122
  • 1
  • 7
0

At line 9. add await like this.

 KVToTableInsert(String tablename, Map jsonMap) async {
        // print(jsonMap);
        List insertNoticeKeys = [];
        List insertNoticeValues = [];
        jsonMap.forEach((key, value) {
          if (value is List) // nested
          {
            value.forEach((item) async { //add async here

            //KVToTableInsert(key, item); // your code
            await KVToTableInsert(key, item); // add await here
            });
          } else {
    
           if(tablename == "protocolLots" || tablename == "lots")
           {
            if(key == "lotNumber") {
                if(value == null)
                {
                  savedValue = 0; // если номер лота не указан, то он ноль
                  value = 0;
                }
              }
           }
Son Tran
  • 122
  • 1
  • 7