Python exception handling within module - python

Stackoverflow posts helped me a lot with Python, however I stuck on this one. I cannot figure out how to simply skip module if it has exceptions within it. Also, if it happens I like to alter one variable so rest of code would know to skip functionality related with that module.
I have main.py which is loading my module sms.py. Here you also see my attempt which does not work:
try:
import sms
except ImportError:
print "Ok, lets skip that module"
dont_use_sms = 1
Part of sms.py which causes exception looks following:
import gammu
sm = gammu.StateMachine()
sm.ReadConfig()
try:
sm.Init() # this one to be exact
except:
raise
when I run this I get following:
Traceback (most recent call last):
File "./main.py", line 10, in <module>
import sms
File "/path/to/sms.py", line 7, in <module>
sm.Init()
gammu.ERR_DEVICENOTEXIST: {'Text': u"Error opening device, it doesn't exist.", 'Code': 4, 'Where': 'Init'}
I have tried to alter exception by putting gammu.ERR_DEVICENOTEXIST as argument, however it didn't help.
I feel that that exception should be somehow handled by sms.py and properly forwarded to main.py, but cannot figure out how.
By the way, I know what causes gammu.ERR_DEVICENOTEXIST and that is not a problem. Question is about how to continue with rest of program if it appears.
That you for suggestions.

You can also change your main.py.
Instead of:
except ImportError:
you can say:
except:
And then it should continue.
PS: Naked except statements are not good style

Related

Python logging in file

There is a simple way how can I write a log where it shows the custom error message
# importing teh module
import logging
try:
printf("GeeksforGeeks")
except Exception as Argument:
# creating/opening a file
f = open("demofile2.txt", "a")
# writing in the file
f.write(str(Argument))
# closing the file
f.close()
the error comes here
name 'printf' is not defined
though if I run this code without try & Exception:
printf("GeeksforGeeks")
the actual error jupyter showing me as
--------------------------------------------------------------------------- NameError Traceback (most recent call
last) C:\WINDOWS\TEMP/ipykernel_21560/2330216246.py in
----> 1 printf("GeeksforGeeks")
NameError: name 'printf' is not defined
So I have two questions:
I want to write this error in my file instead of the custom one.
Is there any way to use it without try:, I have a long code and I scheduled it, however if this fails I don't know why this is actually failing. I don't want to write too many try: exception: or maintain this indent issue while writing this code.
I want when this is schedule I can get the actual reason why it failed in a log file. Please help...

How to generate java like stack trace in python?

In my experience programming with Java, I have become quite fond of the stack traces it generates when my code goes awry, but I feel that the traces generated by python are a bit lacking by comparison. For example, a trace in java might look like this:
java.lang.RuntimeException
at test.package.Example.c(Example.java:20)
at test.package.Example.b(Example.java:15)
at test.package.Example.a(Example.java:10)
Whereas a python trace might look like this:
Traceback (most recent call last):
File "example.py", line 10, in <module>
a()
File "example.py", line 2, in a
b()
File "example.py", line 5, in b
c()
File "example.py", line 8, in c
raise Exception
Exception
While both of these traces convey basically the same information, I personally find that the trace from java is easier to follow.
Is there a means to change the format python uses for printing its stack traces, or would that sort of change require me to create a custom exception handler at the root of my program?
using traceback module
import traceback
try:
x= 1/0
except Exception as e:
print(e)
traceback.print_exc()
There is a means to change the format Python uses to format its stack traces, and that is that you write your own formatter instead. There is only one built-in format.
You can assign your own function to sys.excepthook and it will act as a top-level exception handler that will get access to exceptions that were about to rise uncaught and cause the program to exit. There you can make use of the traceback object to format things however you like. Triptych's answer shows how to use the traceback module to get the info for each stack frame. extract_tb returns a 4-tuple of the filename, line number, function, and source text of the offending line, so if you want to not display the source text you could just throw that away and concatenate the rest. But you'll have to do the work of constructing whatever output you want to see.
If you really want to, you can reformat exception tracebacks with the traceback.extract_tb method.
ref: https://docs.python.org/2/library/traceback.html#traceback.extract_tb

How to know which line gives error in python?

I am new to Python so please excuse my rudimentary question.
When I get an error I can usually figure out what line caused the error, but sometimes from the error message itself I can't decide which line is responsible. So I add some messages between the lines to track the issue. Is there any more effective solution to that?
I am running my codes form ArcGIS toolbox script and I am not sure if I can trace the errors from there.
I always use print statements (okay, function in Py3). It's the most standard way. Just use it to track where are you now in your program, and what are you doing.
However, if your application processes a large data, or if it's a large application, print statements may be not enough. Sometimes, you'll need try and except statements, just to narrow the search of the error.
More on error handling? Here!
This may also be useful.
If youre trying to do this with a excepted error do this:
import traceback
import sys
try:
raise Exception("foo")
except:
for frame in traceback.extract_tb(sys.exc_info()[2]):
fname,lineno,fn,text = frame
print "Error in %s on line %d" % (fname, lineno)
otherwise just read the traceback

Any way to save a traceback object in Python

I was looking to possibly try and save a traceback object and somehow pickle it to a file that I can access. An example of a use case for this is if I am submitting some python code to a farm computer to run and it fails, it would be nice to be able to open a session and access that traceback to debug the problem rather than just seeing a log of the traceback. I do not know if there is any sort of way to do this but thought it would be worth asking why it couldn't if so.
okay so you can use traceback.print_exception(type, value, traceback[, limit[, file]]) and save it in a text or json or you can refer to docs
if you find it helpful please mark it correct or upvote thanx..:)
Depending on how you've written your code, the try statement is probably your best answer. Since any error is just a class that inherits Python's builtin Exception, you can raise custom errors everywhere you need more information about a thrown error. You just need to rename your errors or pass in an appropriate string as the first argument. If you then try your code and use the except statement except CustomError as e, you can pull all the information you want out of e in the except statement as a regular instance. Example:
Your code would be:
def script():
try: codeblock
except Exception as e: raise Error1('You hit %s error in the first block'% e)
try: codeblock 2
except Exception as e: raise Error2('You hit %s error in the second block' % e)
try: script()
except Exception as e:
with open('path\to\file.txt','w') as outFile:
outFile.write(e)
The last part is really nothing more than creating your own log file, but you have to write it down somewhere, right?
As for using the traceback module mentioned above, you can get error information out of that. Any of the commands here can get you a list of tracebacks:
http://docs.python.org/2/library/traceback.html
On the otherhand, if you're trying to avoid looking at log files, the traceback module is only going to give you the same thing a log file would, in a different format. Adding your own error statements in your code gives you more information than a cryptic ValueError about what actually happened. If you print the traceback to your special error, it might give you still more information on your issue.

save Exceptions to file in python

I want to save all following Exceptions in a file. The reason why I need this is because the IDLE for python 3.1.1 in Ubuntu raises an Exception at calltipps, but close to fast, that it isn't readble. Also I need this for testing. The best, would be if I just call a function which saves all Exception to a file. Thank you! ;)
// edit:
i had looked first for a more general way! so that you do not have to place your whole code in a function or indentation. but now that worked wery well for me. although I would be still grateful, if you find a way!
thanks!
If you have a convenient main() function (whatever it's called), then you can use the logging module:
import logging
def main():
raise Exception("Hey!")
logging.basicConfig(level=logging.DEBUG, filename='/tmp/myapp.log')
try:
main()
except:
logging.exception("Oops:")
logging.exception conveniently gets the current exception and puts the details in the log:
ERROR:root:Oops:
Traceback (most recent call last):
File "C:\foo\foo.py", line 9, in <module>
main()
File "C:\foo\foo.py", line 4, in main
raise Exception("Hey!")
Exception: Hey!

Categories

Resources