✍️
Today IL
  • Today I learned!
  • Deployment
    • Rolling, Canary, Blue-green deployments
    • Kubernetees Helm Charts
  • AI/ML
    • SeldonIO
    • Installing software in E2E cloud compute
    • Watching nvidia-smi
    • How does github copilot works?
    • composer library
    • Better to pass callback in fit_one_cycle
    • Eliza - demo
    • Helsinki-NLP Translation models
  • Fastai Learning
  • Python
    • Understanding get_image_files in fastai
    • Resizing an image to smaller size
    • Extracting a Json Object as List(regex)
    • f-strings debugging shortcut
    • Pytest
    • conda switch between python versions
    • Nested functions exception handling
  • Programming
    • Installing Linux Operating system
    • Certbots usage
    • Code highlighting in Google Docs
    • HTTP Methods
    • How to use vertical mouse?
    • HTTP Status Codes
    • Keycloak, Oauth, OpenID connect, SAML
    • Why should NPM packages be as small as possible?
    • Clean Architecture
    • REST vs gRPC
    • Keycloak concepts
    • what is proxy server and nginx?
    • Asymptotic Time Complexity
  • async/await
    • JavaScript Asynchronous operation
    • Lesson 2- Eventloops
    • Lesson 1- asyncio history
    • Lesson 3- using coroutines
    • Lesson 4- coroutines in hood
    • Python async/await
    • JavaScript
  • R Programming language
    • Facet_grid and Facet_wrap
    • geom_point
  • C programming language
    • Inputting String in C programming language
    • Checking if a element is passed as input or not?
  • Git/Github
    • give credits to other people
    • one time setting to easily use Github
    • Checkout to specific tag
    • git suggestions in PR
    • Using emojis in git commits
  • Databases
    • Postgres Database Dockercompose
    • TIL New SQL Operators - Except, UNION, Distinct
    • Analysing Performance of DB Queries
    • Querying Date ranges in postgres
    • Handling Database disconnects in SQLAlchemy
  • WITH NO EXCEPT
  • What is difference with JSON documents in postgres and Mongodb
Powered by GitBook
On this page

Was this helpful?

  1. Python

Nested functions exception handling

Previousconda switch between python versionsNextInstalling Linux Operating system

Last updated 3 years ago

Was this helpful?

Original question asked in Real Python slack group:

I have a question about chained exception handling method and how to return except conditions when I use it for one function which relies on another function. The code can be found below for more context: It looks to me from answers I got from experienced folks in Real python, there are two ways to do this:

In main function write try/except code and raise the exception from nested inner function Hello this is my rewriten code which required a custom class in Python: py class ModelNotSupported(Exception): pass

supported = ["a", "b", "c", "en", "ta"]


def load_model(source, target):
    if source == target:
        raise ModelNotSupported("Hello how are you")
    if source in supported:
        if source == "en":
            return ("t", "t")
        else:
            raise ModelNotSupported("Language pair not available")


def predict_raw():
    try:
        tokenizer, model = load_model("en", "hello")
        return model
    except ModelNotSupported as e:
        return {"inputerror": e}

This can be be implemented with a function which returns messages, and based on which return based on the conditions.

https://gist.github.com/kurianbenoy/59bc813b535adc3a739f87dd4d4f4fb7
https://gist.github.com/kurianbenoy/346090c325dcf4f8f77ec3fbda3f0f99