User Defined Exception in Python Theory In Python, an exception is an error that occurs during program execution and interrupts the normal flow of the program. Sometimes the built-in exceptions are not enough for a program. In such cases, programmers can create their own exceptions called User Defined Exceptions . A user defined exception is created by defining a new class that inherits from the Exception class . This helps programmers handle custom errors based on specific conditions in the program . Syntax class ExceptionName ( Exception ): pass Basic Example Program # User Defined Exception Example class AgeError ( Exception ): pass age = int ( input ( "Enter your age: " )) try : if age < 18 : raise AgeError else : print ( "You are eligible to vote" ) except AgeError : print ( "Error: Age must be 18 or above" ) Sample Output Case 1 Enter your age: 20 You are eligible to vote Case 2 En...
Database Operations in Python Creating Tables, Insert, Update, Delete, Read and Transaction Control Python can interact with databases using modules like sqlite3 . It allows programmers to perform database operations such as creating tables, inserting records, updating data, deleting data, and reading records . These operations are commonly called CRUD operations : Create – Create tables and insert data Read – Retrieve data from tables Update – Modify existing records Delete – Remove records from tables Python also supports Transaction Control , which ensures database operations are completed safely. 1. Creating a Table A table is created using the CREATE TABLE SQL command. import sqlite3 conn = sqlite3 . connect( "college.db" ) cursor = conn . cursor() cursor . execute( """ CREATE TABLE IF NOT EXISTS student( id INTEGER PRIMARY KEY, name TEXT, department TEXT ) """ ) conn . commit() 2. Insert Data ...