Shouldn't a try-catch block catch the exception th...
# javascript
m
Shouldn't a try-catch block catch the exception throw by awaiting a
Promise
?
Copy code
try {
            val p = window.navigator.mediaDevices.asDynamic()
                .getDisplayMedia() as Promise<MediaStream>
            console.log(p)
            p.await()
        } catch (e: Exception) {
            console.error("Everything is fine")
            console.error(e)
        }
1
After looking into the generated Javascript code the problem is clear:
catch (e:Exception)
also checks that the thrown exception is type
Exception
. JS errors are not, so catching JS exceptions requires
catch (e: Throwable)
.
1
j
I'd be concerned the code above swallows exceptions rather than handling them.
m
Note that, as Throwable == Error in JavaScript, this does not fix all errors as people can throw other data types, such as strings (for some reason...)
@John Coleman it was just an example, don't worry 😄 .