Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

189
Visualizações
return in catch block fails to be executed

Basic example that throws an error because tl was not specified:

function allmatches() {
  SpreadsheetApp.getActive().getSheetByName('data').getRange('A1').setValue(tl)
}

And to know if there were any errors in the execution in the Web App, I added a try catch:

function doGet(e) {
  const lock = LockService.getDocumentLock();
  if (lock.tryLock(360000)) {
    try {
      var okgo = e.parameter.okgo;
      allmatches();
    } catch (e) {
      lock.releaseLock();
      return ContentService.createTextOutput('Error!');
    } finally {
      lock.releaseLock();
      return ContentService.createTextOutput('Ok!');
    }
  } else {
    return ContentService.createTextOutput('Timeout!');
  }
}

I call this Web App by Python and in theory the answer should be Error!:

import requests

okgo = 'go'
webAppsUrl = "https://script.google.com/macros/s/XXXXXXXXXX/exec"
url = webAppsUrl + "?okgo=" + okgo
web_app_response = requests.get(url)
print(web_app_response.text)

But the response is:

Ok!

What am I missing?

Add, when run without web app it goes into catch:

function test() {
    try {
      allmatches();
    } catch (e) {
      Logger.log(e)
    }
}

Output:

ReferenceError: tl is not defined
about 4 years ago · Santiago Gelvez
1 Respostas
Responde à pergunta

0

As you can see by the following examples the finally block is run regardless if an error occurs or not. So in the case of the OP ContentService.createTextOutput('Error!'); is quickly replaced by ContentService.createTextOutput('Ok!'); and the user is unaware of the error.

function testTry() {
  try {
    let x = a;
    console.log(x);
  }
  catch(err) {
    console.log(err);
  }
  finally {
    console.log("finally");
  }
}

7:52:56 AM  Notice  Execution started
7:52:58 AM  Info    [ReferenceError: a is not defined]
7:52:58 AM  Info    finally
7:52:57 AM  Notice  Execution completed

function testTry() {
  try {
    let x = 1;
    console.log(x);
  }
  catch(err) {
    console.log(err);
  }
  finally {
    console.log("finally");
  }
}

7:54:37 AM  Notice  Execution started
7:54:38 AM  Info    1
7:54:38 AM  Info    finally
7:54:38 AM  Notice  Execution completed
about 4 years ago · Santiago Gelvez Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda