Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8602321
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T02:03:28+00:00 2026-06-12T02:03:28+00:00

class MyException(Exception): def __str__(self): return self.args[0] def DoWork(): raise MyException(My hoverboard is full of

  • 0
class MyException(Exception):
    def __str__(self):
        return self.args[0]


def DoWork():
    raise MyException("My hoverboard is full of eels")
    pass

if __name__ == '__main__':
    try:
        DoWork()
    except MyException as ex:
        print("This will get printed: " + str(ex)) #Line1
        print("This will not get printed and will raise exception: " + ex) #Line2

Why does an exception needs an explicit conversion in line 1. Ideally it should work as shown in Line2.
Note: I have tried both – the removal of str from MyException class and addition of str; neither of them worked.
Please help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T02:03:30+00:00Added an answer on June 12, 2026 at 2:03 am

    You have to convert it to a string because x + y ends up calling x.__add__(y), so when x is a string you call str.__add__, which operates on two strings, not on a string and an Exception. Python’s built in operators and types generally don’t automatically try to force things to be the right type (like javascript, for example), you have to be explicit. This is no different to having to use int() in x = 1 + int("12"), for example.

    Note that if you don’t want to use str(), you can use % formatting:

    print("This will also work: %s" % ex)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

class A: pass def b(self): print('b') A.b = b a = A() At this
I wanted to create my own Python exception class, like this: class MyException(BaseException): def
I'm confused by the following code: class MyException extends Exception {} class AnotherException extends
class StartAction(argparse.Action): def __call__(self, parser, namespace, values, option_string=None): print "Hello" start.add_argument('-s', '--start', action=StartAction) I
class Test { public static void main(String[] args) throws Exception { Test t =
I have derived a class from Exception , basically like so: class MyException extends
Suppose I have a MyException class that subclasses Exception. I'm using this class to
This code prints out MyUrgentException . Could anybody explain why? class MyException extends Exception{
I have an exception class: class MyException : public std::exception { public: MyException( char*
I have a test that expects a particular exception, for example: @Test(expected=MyException.class) public void

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.