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

183
Visualizações
How to fix "missing catch or finally after try" if i already put catch?

Heres my code

const {getAudioUrl} = require("google-tts-api") 
const langlist = ["en", "id"]

let language = args[0]
if(langlist.find(c => c === language)) {
if(!language) return message.reply("Please enter the language") 

if(!args[1]) return message.reply("Please enter the text")
const text = args.slice(1).join(" ")
if(text.length > 200) return message.reply('You cant input a text with over 200 characters')
const voiceChannel = message.member.voice.channel
if(!voiceChannel) return message.reply(" Please enter a voice channel") 

                                    
const audioURL = await getAudioUrl(text, {
lang: language, 
slow: false, 
host: 'https://translate.google.com', 
timeout: '150000'
 })


 try {
   voiceChannel.join().then(connection => {
     const dispatcher = connection.play(audioURL)
     dispatcher.on('finish', () => {
       voiceChannel.leave()
     })
   })
 }
} else { return message.reply(`That language is not supported, the current supported language : [${langlist.join(", ")}]`); }
   
catch(err){
console.log(err)
message.channel.send("There is an error")}
}
}

I'm searching on internet but their problem if the try don't have catch, but i already have one.

about 4 years ago · Santiago Gelvez
1 Respostas
Responde à pergunta

0

Your catch statement is outside the if block that has the try block. This is what I did to fix the error:

  1. Move the catch block closer to the try.
  2. Remove extra } that were causing errors.
  3. Fix the indentation.

Here's the code:

const {getAudioUrl} = require("google-tts-api") 
const langlist = ["en", "id"]

let language = args[0]
if(langlist.find(c => c === language)) {
    if(!language) return message.reply("Please enter the language") 

    if(!args[1]) return message.reply("Please enter the text")
    const text = args.slice(1).join(" ")
    if(text.length > 200) return message.reply('You cant input a text with over 200 characters')
    const voiceChannel = message.member.voice.channel
    if(!voiceChannel) return message.reply(" Please enter a voice channel") 

                                        
    const audioURL = await getAudioUrl(text, {
        lang: language, 
        slow: false, 
        host: 'https://translate.google.com', 
        timeout: '150000'
    })


    try {
        voiceChannel.join().then(connection => {
            const dispatcher = connection.play(audioURL)
            dispatcher.on('finish', () => {
            voiceChannel.leave()
            })
        })
    }
    catch(err){
        console.log(err)
        message.channel.send("There is an error")
    }
} else {
    return message.reply(`That language is not supported, the current supported language : [${langlist.join(", ")}]`);
}

A tip to avoid this kind of error: indent properly!

If you had indented all of your code, it would have been so much easier to read and therefore finding the error would have been really quick (and you wouldn't even need to ask Stack Overflow).

"Programs must be written for people to read, and only incidentally for machines to execute." - Harold Abelson

If you make your code easier to read, debugging, rewriting, maintaining, understanding, etc. will become much easier - for you, and everyone else.

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