- 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
Manogna has Published 40 Articles
Manogna
236 Views
We write a unittest that fails only if a function doesn't throw an expected exception.We also test if a Python function throws an exception.For example, see the sample code we paste into the Python shell to test Python's type-safety:Exampleimport unittest class MyTestCase(unittest.TestCase): def test_1_cannot_add_int_and_str(self): with self.assertRaises(TypeError): ... Read More
Manogna
1K+ Views
Python IdentifiersIdentifier is the name given to entities like class, functions, variables etc. in Python. It helps in knowing one entity from another.Rules for writing identifiersIdentifiers can be a combination of lowercase letters (a to z) or uppercase letters (A to Z) or digits (0 to 9) or an underscore ... Read More
Manogna
264 Views
A ValueError is used when a function receives a value that has the right type but an invalid value.The given code can be rewritten as follows to handle the exception and find its type.Exampleimport sys try: n = int('magnolia') except Exception as e: print e print sys.exc_typeOutputinvalid literal for int() ... Read More
Manogna
82 Views
A function is a callable object in Python, i.e. can be called using the call operator. However other objects can also emulate a function by implementing __call__method. exampledef a(): pass # a() is an example of function print a print type(a)OutputC:/Users/TutorialsPoint/~.py A method is a special class of function, ... Read More
Manogna
443 Views
In Python, variables are never explicitly typed. Python figures out what type a variable is and keeps track of it internally. In Java, C++, and other statically-typed languages, you must specify the datatype of the function return value and each function argument.If we explicitly define datatype in a python function, ... Read More
Manogna
186 Views
For the given code above the solution is as followsExampleclass CustomValueError(ValueError): def __init__(self, arg): self.arg = arg try: a = int(input("Enter a number:")) if not 1 < a < 10: raise CustomValueError("Value must be within 1 and 10.") except CustomValueError as e: print("CustomValueError Exception!", e.arg)OutputEnter a number:45 CustomValueError Exception! Value ... Read More
Manogna
413 Views
In exception handling in Python, we use the try and except statements to catch and handle exceptions. The code within the try clause is executed statement by statement.If an exception occurs, the rest of the try block is skipped and the except clause is executed.Exampletry: 'apple' + 6 except Exception: ... Read More
Manogna
676 Views
A KeyError is raised when a value is not found as a key of a dictionary. The given code is rewritten as follows to catch the exception and find its type.Exampleimport sys try: s = {'a':5, 'b':7}['c'] except: print (sys.exc_info())Output(, KeyError('c',), )
Manogna
192 Views
In given code, a custom exception FooException has been created which is a subclass of the super class Exception. We will pass a string object to the custom exception as followsExample#foobar.py class FooException(Exception): def __init__(self, text, *args): super ( FooException, self ).__init__ ( text, *args ) self.text = text try: ... Read More
Manogna
1K+ Views
Here we are passing a variable to given exception. We are defining a custom exception ExampleException which is a subclass of base class Exception and also defining the __init__ method. We use a try-except block to raise the exception and pass the variable to the exception as follows.Exampleclass ExampleException(Exception): def ... Read More