Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

178
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!