Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

184
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda