try-catch
Tratamento de excecao moderno em TLPP. Substitui o ErrorBlock antigo do AdvPL. Captura erros runtime com objeto detalhado.
Assinatura: try ... catch tExceptionError oErr ... endtry
Retorna: construct
TLPP traz try/catch nativo, similar ao C#/Java. Muito mais limpo que o ErrorBlock antigo do AdvPL.
Sintaxe
try
// codigo que pode falhar
nValor := nA / nB
oCliente := FwHttpClient():new(cUrl)
oCliente:get()
catch tExceptionError oErr
// tratamento
conout('Erro: ' + oErr:getDescription())
conout('Stack: ' + oErr:getStack())
endtryPropriedades do oErr
catch tExceptionError oErr
cMsg := oErr:getDescription() // mensagem
cStack := oErr:getStack() // stack trace
nCode := oErr:getErrorCode() // codigo
cSubsys := oErr:getSubSystem() // subsystem
cSource := oErr:getSource() // fonte que gerouRe-throw
try
DoSomething()
catch tExceptionError oErr
FwLogger():getLogger():error('Falhou: ' + oErr:getDescription())
UserException(oErr:getDescription()) // re-lanca
endtryfinally (com fimsemerro)
TLPP nao tem finally nativo, mas pode simular:
local lOk as logical
lOk := .F.
try
Operacao()
lOk := .T.
catch tExceptionError oErr
LogErro(oErr)
endtry
// cleanup sempre roda
FecharRecursos()
if !lOk
Return
endif
Exemplos
Capturar erro de API
try
oClient:get()
catch tExceptionError oErr
FwLogger():getLogger():error(oErr:getDescription())
endtry