INPUT VALIDATION
WHY VALIDATE INPUT
 Validating user input is fundamental in
application development.
 It prevents users/systems from inadvertently
entering incorrect values or values that
should not be allowed into the application.
 Incorrect input can lead to user frustration or
serious security concerns
EXCEPTIONS
 An exception is a signal that a specific condition has
occurred that can’t be easily handled by normal
program flow.
 Ex. Trying to perform a calculation on a string
 Typically your application will error and crash,
however with exception handling we can catch the
error and safely exit the application, fix the issue, or
try again.
 Exceptions are preprogrammed conditional
statements for error catching, however you can
create your own error messages.
COMMON EXCEPTIONS
 ImportError – module not found
 ValueError – Inappropriate value
 KeyboardInterupt – When user terminates
application
 IOError – file cannot be opened
TRY-EXCEPT
 Code:
 try:
statement 1 to attempt
statement 2 to attempt
.
.
.
except: OPTIONAL EXCEPTION
USER CREATED EXCEPTION
TRY-CATCH EXAMPLE
 try:
val = int(input(“Enter a number”)
except: ValueError
 try:
val = int(input(“Enter a number”)
except: ValueError
print(“Not a valid number”)
 try:
val = int(input(“Enter a number”)
except: MyException
print(“Not a valid number”)
quit

Input Validation

  • 1.
  • 2.
    WHY VALIDATE INPUT Validating user input is fundamental in application development.  It prevents users/systems from inadvertently entering incorrect values or values that should not be allowed into the application.  Incorrect input can lead to user frustration or serious security concerns
  • 3.
    EXCEPTIONS  An exceptionis a signal that a specific condition has occurred that can’t be easily handled by normal program flow.  Ex. Trying to perform a calculation on a string  Typically your application will error and crash, however with exception handling we can catch the error and safely exit the application, fix the issue, or try again.  Exceptions are preprogrammed conditional statements for error catching, however you can create your own error messages.
  • 4.
    COMMON EXCEPTIONS  ImportError– module not found  ValueError – Inappropriate value  KeyboardInterupt – When user terminates application  IOError – file cannot be opened
  • 5.
    TRY-EXCEPT  Code:  try: statement1 to attempt statement 2 to attempt . . . except: OPTIONAL EXCEPTION USER CREATED EXCEPTION
  • 6.
    TRY-CATCH EXAMPLE  try: val= int(input(“Enter a number”) except: ValueError  try: val = int(input(“Enter a number”) except: ValueError print(“Not a valid number”)  try: val = int(input(“Enter a number”) except: MyException print(“Not a valid number”) quit