Python Forum
multi-line messages in raised exceptions?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
multi-line messages in raised exceptions?
#1
many of my functions accept many keyword arguments like:
def callme(*args,**opts):
    ...
they then have code to pop off the known keywords like:
def callme(*args,**opts):
    foo = opts.pop('foo',default_foo)
    bar = opts.pop('bar',default_bar)
    ...
then they check to see if any keywords are leftover like:
def callme(*args,**opts):
    foo = opts.pop('foo',default_foo)
    bar = opts.pop('bar',default_bar)
    if opts:
        raise ValueError(f'unknown keywords: {", ".join(x for x in opts.keys())}')
    ...
is it possible to output a multi-line message, one line for each keyword in this example, in the raised exception? i would also like to check the arguments, too, and combine messages related to this, too.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
You know, it probably would have been less effort to just try to raise a multiline exception message than to type out a whole post explaining your function design and why you want to do it.

In fact, I'm sure it would have been, because I tried it and it took less effort than typing this post up.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
do you already have a process capturing exceptions?
if you don't know what exceptions are being thrown, you can use a general capture
You can replace with exception names as indentified
try:
    ...
except:
    print("Unexpected error:", sys.exc_info()[0])
    print(f"Args:")
    for arg in args:
        print(f"{arg}")
    raise
Reply
#4
i'm not trying to capture the exception. it's a function. the caller that imported it may want to capture it. but if not, i want to have a line per keyword that remains in the dictionary.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to add multi-line comment section? Winfried 1 202 Mar-24-2024, 04:34 PM
Last Post: deanhystad
  PiCamera - print exceptions? korenron 2 834 Dec-15-2022, 10:48 PM
Last Post: Larz60+
  regex multi-line kucingkembar 6 1,556 Aug-27-2022, 10:27 PM
Last Post: kucingkembar
  Class exceptions DPaul 1 1,296 Mar-11-2022, 09:01 AM
Last Post: Gribouillis
  multi-line CMD in one-line python kucingkembar 5 3,971 Jan-01-2022, 12:45 PM
Last Post: kucingkembar
  is this a good way to catch exceptions? korenron 14 4,719 Jul-05-2021, 06:20 PM
Last Post: hussaind
  RuntimeError: generator raised StopIteration quest 1 5,808 Mar-28-2021, 08:11 PM
Last Post: quest
  Multi-line console input lizze 4 2,366 Dec-26-2020, 08:10 AM
Last Post: lizze
  Python, exceptions KingKhan248 6 3,041 Nov-15-2020, 06:54 AM
Last Post: buran
  Split string between two different delimiters, with exceptions DreamingInsanity 2 2,031 Aug-24-2020, 08:23 AM
Last Post: DreamingInsanity

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020