- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
OSError serves as the error class for the os module, and is raised when an error comes back from an os-specific function.We can re-write the given code as follows to handle the exception and know its type.#foobar.py import os import sys try: for i in range(5): print i, os.ttyname(i) except Exception as e: print e print sys.exc_typeIf we run this script at linux terminal$ python foobar.pyWe get the following outputOUTPUT0 /dev/pts/0 1 /dev/pts/0 2 /dev/pts/0 3 [Errno 9] Bad file descriptor
The given code is rewritten to catch the exceptionimport sys import threading import time import Queue def thread(args1, stop_event, queue_obj): print "start thread" stop_event.wait(12) if not stop_event.is_set(): try: raise Exception("boom!") except Exception: queue_obj.put(sys.exc_info()) pass try: queue_obj = Queue.Queue() t_stop = threading.Event() t = threading.Thread(target=thread, args=(1, t_stop, queue_obj)) t.start() time.sleep(15) print "stop thread!" t_stop.set() try: exc = queue_obj.get(block=False) except Queue.Empty: pass else: exc_type, exc_obj, exc_trace = exc print exc_obj except Exception as e: print "It took too long"OUTPUTC:/Users/TutorialsPoint1/~.py start thread stop thread! boom!Read More
In Python 3.x, the code is subject to exception chaining and we get the output as followsC:/Users/TutorialsPoint1/~.py Traceback (most recent call last): File "C:/Users/TutorialsPoint1/~.py", line 2, in 1/0 ZeroDivisionError: division by zeroThe above exception was the direct cause of the following exception:Traceback (most recent call last): File "C:/Users/TutorialsPoint1/~.py", line 4, in raise ValueError ( "Sweet n Sour grapes" ) from e ValueError: Sweet n Sour grapes
The HTML onoffline event attribute is triggered when the browser starts to work offline.SyntaxFollowing is the syntax −Let us see an example of HTML onoffline event Attribute−Example Live Demo body { color: #000; height: 100vh; background: linear-gradient(62deg, #FBAB7E 0%, #F7CE68 100%) no-repeat; text-align: center; } .show { font-size: 1.2rem; color: #fff; } HTML onoffline/ononline Event Attribute Demo Try to disable/enable your network. function onlineFn() { alert("Hey! You are online"); } function offlineFn() { alert("Hey! You are offline"); } OutputNow try to enable/disable your network to observe how onoffline/ononline event attribute works−
In this article, we will learn about the fundamentals of software testing by the help of unittest module available in Python 3.x. Or earlier. It allows automation, sharing of the setup and exit code for tests, and independent tests for every framework.In unit test, we use a wide variety of object oriented concepts. We will be discussing some majorly used concepts here.Testcase − It is response specific base class in accordance with a given set of inputs. We use base class of unittest i.e. “TestCase” to implement this operation.Testsuite − It is used to club test cases together and execute ... Read More
It is a general thumb rule that though you can catch all exceptions using code like below, you shouldn’t:try: #do_something() except: print "Exception Caught!"However, this will also catch exceptions like KeyboardInterrupt we may not be interested in. Unless you re-raise the exception right away – we will not be able to catch the exceptions:try: f = open('file.txt') s = f.readline() i = int(s.strip()) except IOError as (errno, strerror): print "I/O error({0}): {1}".format(errno, strerror) except ValueError: print "Could not convert data to an integer." except: ... Read More
The HTML onload event attribute is triggered when an object has been loaded in an HTML document.SyntaxFollowing is the syntax −Let us see an example of HTML onload event Attribute−Example Live Demo body { color: #000; height: 100vh; background-color: #FBAB7E; background-image: linear-gradient(62deg, #FBAB7E 0%, #F7CE68 100%); text-align: center; padding: 20px; } function get() { alert("Hi! you just refresh the document."); } HTML onload Event Attribute Demo Now try to refresh this document. OutputNow try to reload the document to observe how onload event attribute works.
Only a single except clause in a try block is invoked. If you want the exception to be caught higher up then you will need to use nested try blocks.Let us write 2 try...except blocks like this:try: try: 1/0 except ArithmeticError as e: if str(e) == "Zero division": print ("thumbs up") else: raise except Exception as err: print ("thumbs down") raise errwe get the following outputthumbs down Traceback (most recent call last): File "C:/Users/TutorialsPoint1/~.py", line 11, in raise err File "C:/Users/TutorialsPoint1/~.py", line 3, in 1/0 ZeroDivisionError: division by zeroAs per python tutorial there is one and only one ... Read More
The problem is that thread_obj.start() returns immediately. The child thread that you started executes in its own context, in its own stack. Any exception that occurs there is in the context of the child thread. You have to communicate this information to the parent thread by passing some message.The code can be re-written as follows:import sys import threading import Queue class ExcThread(threading.Thread): def __init__(self, foo): threading.Thread.__init__(self) self.foo = foo def run(self): try: raise Exception('An error occurred here.') except Exception: self.foo.put(sys.exc_info()) def main(): foo = Queue.Queue() thread_obj = ExcThread(foo) thread_obj.start() while True: try: exc = foo.get(block=False) except Queue.Empty: pass else: exc_type, ... Read More
The code can be rewritten to catch the exception as follows:try: with open("myFile.txt") as f: print(f.readlines()) except: print('No such file or directory')We get the following outputC:/Users/TutorialsPoint1/~.py No such file or directory
To Continue Learning Please Login
Login with Google