Message Parsing Error Python
Contents |
you have probably seen some. There are (at least) two distinguishable kinds of errors: syntax errors and exceptions. 8.1. Syntax Errors¶ Syntax errors, also known as parsing errors, are perhaps the most common kind of complaint you get while python error types you are still learning Python: >>> while True print 'Hello world' File "
Python Exception Message
while True print 'Hello world' ^ SyntaxError: invalid syntax The parser repeats the offending line and displays a little ‘arrow' pointing at
Python Raise Custom Exception
the earliest point in the line where the error was detected. The error is caused by (or at least detected at) the token preceding the arrow: in the example, the error is detected at the keyword print,
Syntax For Generic Except Clause In Python
since a colon (':') is missing before it. File name and line number are printed so you know where to look in case the input came from a script. 8.2. Exceptions¶ Even if a statement or expression is syntactically correct, it may cause an error when an attempt is made to execute it. Errors detected during execution are called exceptions and are not unconditionally fatal: you will soon learn how to handle them in Python python exception stack trace programs. Most exceptions are not handled by programs, however, and result in error messages as shown here: >>> 10 * (1/0) Traceback (most recent call last): File "
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and python print exception policies of this site About Us Learn more about Stack Overflow the python try except else company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags is nested try block possible in python Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 6.2 million programmers, just like you, helping each other. Join them; it only https://docs.python.org/2/tutorial/errors.html takes a minute: Sign up Python: Error message parsing up vote 0 down vote favorite I'm trying to handle IOError generated when trying to open a non existent file. I do: try: inputFile = open('nosuchfile', 'r') except IOError as exception: print 'error: %s' % (exception) This gives me: error: [Errno 2] No such file or directory: 'nosuchfile' But I'm only interested http://stackoverflow.com/questions/12664190/python-error-message-parsing in the message and not the [Errno 2] part. So I change my code. print 'error: %s' % (exception.strerror) But now I get: error: No such file or directory Where did the name of the file go? I know I could just print the file name separately, but I would really like how (if at all) the name was stored in the exception, but not in either of its arguments (printing exception.errno gives 2). I am using version 2.7.3. python exception exception-handling python-2.7 share|improve this question asked Sep 30 '12 at 19:11 Jayanth Koushik 5,3102039 add a comment| 1 Answer 1 active oldest votes up vote 4 down vote accepted The filename is stored in, well, the filename property: try: inputFile = open('nosuchfile', 'r') except IOError as exception: print ('error: %s: %r' % (exception.strerror, exception.filename)) share|improve this answer answered Sep 30 '12 at 19:14 phihag 139k33264316 Oh got it. But why is this not revealed when I print exception.args? –Jayanth Koushik Sep 30 '12 at 19:17 NB: exception.strerror is not an Exception attribute –Mr_and_Mrs_D Sep 28
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings http://stackoverflow.com/questions/9276663/what-exception-class-to-use-for-file-parsing-error and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 6.2 million programmers, just like you, helping each other. Join them; python exception it only takes a minute: Sign up What exception class to use for file parsing error? up vote 8 down vote favorite 1 I'm writing a parser for a certain file format. If a file is not correctly formatted (and can not be parsed) then the parser throws an exception. What exception class, in the Python 2 exception hierarchy, should message parsing error I use? python exception share|improve this question edited Feb 14 '12 at 15:58 asked Feb 14 '12 at 12:13 user763305 8,570114084 1 See also the section on user-defined exceptions in the Python tutorial. –Sven Marnach Feb 14 '12 at 15:37 @Sven Marnach: The parser is part of an extension module written using Boost.Python. And I have no idea how to derive new exception classes using the Python C-API or Boost.Python. Therefore I prefer to use one of the existing exception classes. Possible candidates seem to be Exception, ValueError and IOError. –user763305 Feb 14 '12 at 15:58 See stackoverflow.com/questions/2261858/…. If your actual problem is how to create a custom exception class with boost.python, you have asked the wrong question. :) –Sven Marnach Feb 14 '12 at 16:05 See also stackoverflow.com/questions/9620268/… –user763305 Mar 13 '12 at 19:26 add a comment| 2 Answers 2 active oldest votes up vote 9 down vote accepted How about class XyzParseError(Exception): pass where XyzParser is the name of your parser class? That's also what HTMLParser in the standard