Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

python exceptions

#Syntax:

try:
	statement
except Exception as varname:
	statement
   
"""
Some specific exceptions (Lengthy but time-saving )- 

ArithmeticError - Raised when an error occurs in numeric calculations

AssertionError	- Raised when an assert statement fails

AttributeError	- Raised when attribute reference or assignment fails

Exception - Base class for all exceptions

EOFError -	Raised when the input() method hits an "end of file" condition (EOF)

FloatingPointError -	Raised when a floating point calculation fails

GeneratorExit -	Raised when a generator is closed (with the close() method)

ImportError -	Raised when an imported module does not exist

IndentationError -	Raised when indendation is not correct

IndexError -	Raised when an index of a sequence does not exist

KeyError -	Raised when a key does not exist in a dictionary

KeyboardInterrupt -	Raised when the user presses Ctrl+c, Ctrl+z or Delete

LookupError -	Raised when errors raised cant be found

MemoryError -	Raised when a program runs out of memory

NameError -	Raised when a variable does not exist

NotImplementedError -	Raised when an abstract method requires an inherited class to override the method

OSError -	Raised when a system related operation causes an error

OverflowError -	Raised when the result of a numeric calculation is too large

ReferenceError -	Raised when a weak reference object does not exist

RuntimeError -	Raised when an error occurs that do not belong to any specific expections

StopIteration -	Raised when the next() method of an iterator has no further values

SyntaxError -	Raised when a syntax error occurs

TabError -	Raised when indentation consists of tabs or spaces

SystemError -	Raised when a system error occurs

SystemExit -	Raised when the sys.exit() function is called

TypeError -	Raised when two different types are combined

UnboundLocalError -	Raised when a local variable is referenced before assignment

UnicodeError -	Raised when a unicode problem occurs

UnicodeEncodeError -	Raised when a unicode encoding problem occurs

UnicodeDecodeError -	Raised when a unicode decoding problem occurs

UnicodeTranslateError -	Raised when a unicode translation problem occurs

ValueError -	Raised when there is a wrong value in a specified data type

ZeroDivisionError -	Raised when the second operator in a division is zero

"""
Comment

python exception

try:
    # raise a custom exception
    raise Exception("A custom exception")
except Exception as err:
    print(err)
finally:
    print('This execute no matter if an exception occurs or not')
Comment

python exception

try:
  # code block
except ValueError as ve:
  print(ve)
Comment

python exceptions

while True:
    try:
        funds = float(input("Enter amount to transfer: "))
        if funds < 0:
            raise Exception("Please enter a positive value,")
        break
    except ValueError:
        print("Please enter a numeric value,")
Comment

python exceptions

BaseException
 +-- SystemExit
 +-- KeyboardInterrupt
 +-- GeneratorExit
 +-- Exception
      +-- StopIteration
      +-- StopAsyncIteration
      +-- ArithmeticError
      |    +-- FloatingPointError
      |    +-- OverflowError
      |    +-- ZeroDivisionError
      +-- AssertionError
      +-- AttributeError
      +-- BufferError
      +-- EOFError
      +-- ImportError
      |    +-- ModuleNotFoundError
      +-- LookupError
      |    +-- IndexError
      |    +-- KeyError
      +-- MemoryError
      +-- NameError
      |    +-- UnboundLocalError
      +-- OSError
      |    +-- BlockingIOError
      |    +-- ChildProcessError
      |    +-- ConnectionError
      |    |    +-- BrokenPipeError
      |    |    +-- ConnectionAbortedError
      |    |    +-- ConnectionRefusedError
      |    |    +-- ConnectionResetError
      |    +-- FileExistsError
      |    +-- FileNotFoundError
      |    +-- InterruptedError
      |    +-- IsADirectoryError
      |    +-- NotADirectoryError
      |    +-- PermissionError
      |    +-- ProcessLookupError
      |    +-- TimeoutError
      +-- ReferenceError
      +-- RuntimeError
      |    +-- NotImplementedError
      |    +-- RecursionError
      +-- SyntaxError
      |    +-- IndentationError
      |         +-- TabError
      +-- SystemError
      +-- TypeError
      +-- ValueError
      |    +-- UnicodeError
      |         +-- UnicodeDecodeError
      |         +-- UnicodeEncodeError
      |         +-- UnicodeTranslateError
      +-- Warning
           +-- DeprecationWarning
           +-- PendingDeprecationWarning
           +-- RuntimeWarning
           +-- SyntaxWarning
           +-- UserWarning
           +-- FutureWarning
           +-- ImportWarning
           +-- UnicodeWarning
           +-- BytesWarning
           +-- ResourceWarning
Comment

python exception

#!/bin/python3

import sys


S = input().strip()

try:
    S = int(S)
    print(S)
except:
    print("Bad String")
Comment

PREVIOUS NEXT
Code Example
Python :: set the context data in django listview 
Python :: boto3 read excel file from s3 into pandas 
Python :: flask abort return json 
Python :: how to determine python project parent dir 
Python :: using df.astype to select categorical data and numerical data 
Python :: date colomn to datetime 
Python :: python to create pandas dataframe 
Python :: how to iterate over rows in a dataframe in pandas 
Python :: python how to check if first character in string is number 
Python :: install python in centos7 
Python :: increase a date in python 
Python :: delete pandas column 
Python :: anaconda snake 
Python :: np arange shape 
Python :: how to get a hyperlink in django 
Python :: api testing with python 
Python :: remove add button django admin 
Python :: python count characters 
Python :: twitter bot python 
Python :: pygame.rect 
Python :: python fillna with mode 
Python :: pandas change to numeric 
Python :: after groupby how to add values in two rows to a list 
Python :: pandas write to excel 
Python :: python tkinter fenstergröße 
Python :: axios django 
Python :: how to clean environment python 
Python :: how to make a program that identifies positives and negatives in python 
Python :: Simple Scatter Plot in matplotlib 
Python :: get instance of object python 
ADD CONTENT
Topic
Content
Source link
Name
9+6 =