PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, November 15, 2022

[FIXED] Why can't I catch the exception in this code?

 November 15, 2022     error-handling, exception, python     No comments   

Issue

I'm trying to detect foreign languages in a dataframe using 'langdetect' library for Python.

for e in food['product_name'].dropna():
    if detect(e) == 'zh':
        print e

Here im trying to print every chinese word found in a specific column.

However, at some point I get this error message:

LangDetectException: No features in text.

I understand this happens when a number, a blank space or a string that is not a word (reference code, mail address...) is found.

All I want is to catch an exception and handle the situation accordingly BUT i don't know how to do it. Here's my attempt:

for e in food['product_name'].dropna():
    if detect(e) == 'zh':
        try:
            print e
        except LangDetectException:
            pass

Can someone please help me fix this poorly written snippet ? Obviously there's something wrong with it but I don't know what exactly !


Solution

As mentioned in the comments above the exception is being raised by detect, so you need to wrap that call in your try block:

for e in food['product_name'].dropna():
    try:
        if detect(e) == 'zh':
            print e
    except LangDetectException:
        pass


Answered By - Luke Smith
Answer Checked By - Dawn Plyler (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing