我需要将 indexeddb 事务包装在 try/catch 中吗?

Do I need to wrap indexeddb transactions in a try/catch?

看起来 indexeddb 有一个 onerror 事件,所以我是否也需要将它包装在一个 try catch 中?

try {
var reqst = window.db.transaction('xyz')
  .objectStore('xyz')
  .get(1234) 
reqst.onsuccess = success
reqst.onerror = error
} catch(err) {
  console.log(err.stack)
}

function success(response) {}
function error(response) {}

我能想到的第一个场景是如果 xyz 不存在,但这是在开发时会被捕获的那种错误。

IDBDatabase.transaction can synchronously throw an error in various situations,所以如果你想在其中一个错误发生时做某事,你需要一个 try/catch 块。

其他方法也一样,比如IDBTransaction.objectStore, IDBObjectStore.get,等等