相关疑难解决方法(0)

是否有可能在电子主过程中捕获渲染器过程的异常?

我正在使用Electrons Quick Start Projekt(Commit dbef48ee7d072a38724ecfa57601e39d36e9714e)来测试异常.

index.html我从改变所需要的模块的名称renderer.jsrendererXXX.js.

require('./renderer.js')
Run Code Online (Sandbox Code Playgroud)

这导致了预期的Exeption(在该窗口的devtools中可见):

Uncaught Error: Cannot find module './rendererXXX.js'
Run Code Online (Sandbox Code Playgroud)

现在,如果main-process(请参阅main.js)知道一个渲染器进程失败,那将会很好.因此,我将窗口的instatiation包装成try-catch-block

try {
  app.on('ready', createWindow)
} catch (e) {
  console.log("Exception caught: " + e.message);
} finally {
  // nothing yet
}
Run Code Online (Sandbox Code Playgroud)

但我意识到,异常不会转发到主进程.那么处理渲染器进程异常的典型方法是什么?有没有办法从主进程处理它们?

编辑:

我还包装了加载index.html到try-catch的行,但我仍然无法处理错误:

  try {
    // and load the index.html of the app.
    mainWindow.loadURL(`file://${__dirname}/index.html`)
  } catch (e) {
    console.log("Exception caught in 'createWindow': " + e.message);
  }
Run Code Online (Sandbox Code Playgroud)

javascript exception-handling exception electron

6
推荐指数
2
解决办法
5843
查看次数