Error Handling
Error handling allows your programs to gracefully manage unexpected situations and failures without crashing. Common Pseudocode provides try-catch blocks for this purpose.
Basic Try-Catch
Section titled “Basic Try-Catch”try result = 10 / 0catch error output "An error occurred: {error}"The try block contains code that might fail, and the catch block handles the error.
Try-Catch-Finally
Section titled “Try-Catch-Finally”The finally block always executes, whether an error occurred or not:
try result = riskyOperation()catch error output "Error: {error}"finally output "This always executes"Common Error Scenarios
Section titled “Common Error Scenarios”Division by Zero
Section titled “Division by Zero”output "Enter numerator: "input numInputnumerator = convert numInput to number
output "Enter denominator: "input denInputdenominator = convert denInput to number
try { result = numerator / denominator output "Result: {result}"} catch error { output "Error: Cannot divide by zero" output "Please enter a non-zero denominator"}Invalid Type Conversion
Section titled “Invalid Type Conversion”output "Enter a number: "input userInput
try { number = convert userInput to number output "You entered: {number}" squared = number ^ 2 output "Squared: {squared}"} catch error { output "Error: '{userInput}' is not a valid number" output "Please enter numeric digits only"}Array Index Out of Bounds
Section titled “Array Index Out of Bounds”numbers = [10, 20, 30, 40, 50]
output "Enter index (0-4): "input indexInputindex = convert indexInput to number
try { value = numbers[index] output "Value at index {index}: {value}"} catch error { output "Error: Index {index} is out of bounds" output "Valid indices are 0 to {length of numbers - 1}"}File Operations with Error Handling
Section titled “File Operations with Error Handling”Reading a File
Section titled “Reading a File”try { open file "data.txt" for reading as fileHandle while not end of fileHandle { line = read line from fileHandle output line } close fileHandle} catch error { output "Error reading file: {error}" output "Make sure 'data.txt' exists"} finally output "File operation complete"Writing to a File
Section titled “Writing to a File”try { open file "output.txt" for writing as fileHandle write "Hello, World!" to fileHandle write "Second line" to fileHandle close fileHandle output "File written successfully"} catch error { output "Error writing file: {error}" output "Check file permissions"}Input Validation with Error Handling
Section titled “Input Validation with Error Handling”Validating Numeric Input
Section titled “Validating Numeric Input”function getValidNumber takes in prompt and min and max { valid = false result = 0
while NOT valid { output prompt input userInput
try { number = convert userInput to number
if number < min OR number > max output "Error: Number must be between {min} and {max}" else { result = number valid = true } } catch error output "Error: Please enter a valid number" }
return result}
// Usageage = getValidNumber("Enter your age (0-120): ", 0, 120)output "Age: {age}"Safe Array Access
Section titled “Safe Array Access”function safeArrayAccess takes in array and index { try { if index < 0 OR index >= length of array { output "Error: Index out of range" return null }
return array[index] } catch error { output "Error accessing array: {error}" return null }}
numbers = [10, 20, 30, 40, 50]value = safeArrayAccess(numbers, 2)
if value != null output "Value: {value}"Multiple Error Types
Section titled “Multiple Error Types”While Common Pseudocode doesn’t distinguish error types in the syntax, you can handle different scenarios:
function processData takes in data { try { // Check for null/empty if data equals null OR length of data equals 0 { output "Error: No data provided" return false }
// Process data result = performCalculation(data)
// Check result validity if result < 0 { output "Warning: Negative result" return false }
return true } catch error { output "Unexpected error: {error}" return false }}Nested Try-Catch
Section titled “Nested Try-Catch”try { output "Enter filename: " input filename
try { open file filename for reading as fileHandle content = read line from fileHandle close fileHandle
try number = convert content to number output "Number from file: {number}" catch error output "Error: File content is not a number" } catch error output "Error: Cannot open file '{filename}'"} catch error output "Fatal error: {error}"Calculator with Error Handling
Section titled “Calculator with Error Handling”function calculate takes in operation and num1 and num2 { try { switch operation case "+" return num1 + num2 case "-" return num1 - num2 case "*" return num1 * num2 case "/" if num2 equals 0 { output "Error: Division by zero" return null } return num1 / num2 default output "Error: Invalid operation '{operation}'" return null } catch error { output "Calculation error: {error}" return null }}
// Main programtry { output "Enter first number: " input num1Input num1 = convert num1Input to number
output "Enter operation (+, -, *, /): " input operation
output "Enter second number: " input num2Input num2 = convert num2Input to number
result = calculate(operation, num1, num2)
if result != null output "Result: {result}"} catch error output "Input error: Please enter valid numbers"Database Connection Example
Section titled “Database Connection Example”function connectToDatabase takes in connectionString { connection = null
try { output "Connecting to database..." connection = openConnection(connectionString) output "Connected successfully" return connection } catch error { output "Database connection failed: {error}" return null } finally output "Connection attempt complete"}
function queryDatabase takes in connection and query { try { if connection equals null { output "Error: No database connection" return null }
output "Executing query..." result = executeQuery(connection, query) output "Query successful" return result } catch error { output "Query failed: {error}" return null } finally output "Query execution complete"}
// Main programconnection = connectToDatabase("server=localhost;db=myapp")
if connection != null { data = queryDatabase(connection, "SELECT * FROM users")
if data != null output "Retrieved {length of data} records"
try { closeConnection(connection) output "Connection closed" } catch error output "Error closing connection: {error}"}Login System with Error Handling
Section titled “Login System with Error Handling”constant MAX_ATTEMPTS = 3constant CORRECT_PASSWORD = "secret123"
attempts = 0authenticated = false
try { while attempts < MAX_ATTEMPTS AND NOT authenticated { try { output "Enter password (Attempt {attempts + 1} of {MAX_ATTEMPTS}): " input password
if password equals "" { output "Error: Password cannot be empty" attempts++ skip }
if password equals CORRECT_PASSWORD { authenticated = true output "Login successful!" } else { attempts++ remaining = MAX_ATTEMPTS - attempts if remaining > 0 output "Incorrect password. {remaining} attempts remaining." } } catch error { output "Input error: {error}" attempts++ } }
if NOT authenticated { output "Login failed: Maximum attempts exceeded" output "Account locked for security" }} catch error { output "System error: {error}"}finally { if authenticated output "Welcome to the system!" else output "Access denied"}JSON Parser Example
Section titled “JSON Parser Example”function parseJSON takes in jsonString { try { // Simplified JSON parsing if NOT jsonString contains "{" { output "Error: Invalid JSON format" return null }
// Parse the JSON data = parse(jsonString) return data } catch error { output "JSON parsing error: {error}" output "Check JSON syntax" return null }}
// UsagejsonString = '{"name": "Alice", "age": 30}'
try { data = parseJSON(jsonString)
if data != null { output "Name: {data.name}" output "Age: {data.age}" }} catch error output "Failed to process data: {error}"Best Practices
Section titled “Best Practices”1. Be Specific in Error Messages
Section titled “1. Be Specific in Error Messages”// Badtry value = array[index]catch error output "Error"
// Goodtry value = array[index]catch error output "Error accessing index {index}" output "Array has {length of array} elements (valid: 0-{length of array - 1})"2. Always Use Finally for Cleanup
Section titled “2. Always Use Finally for Cleanup”fileHandle = null
try { open file "data.txt" for reading as fileHandle processFile(fileHandle)} catch error output "Error: {error}"finally { if fileHandle != null { close fileHandle output "File closed" }}3. Don’t Catch Errors You Can’t Handle
Section titled “3. Don’t Catch Errors You Can’t Handle”// Bad - catching but doing nothingtry criticalOperation()catch error // Empty catch - error is hidden
// Good - let error propagate or handle ittry criticalOperation()catch error { output "Critical error: {error}" output "Please contact support" exit}4. Validate Before Risky Operations
Section titled “4. Validate Before Risky Operations”// Prevent errors rather than catching themif denominator equals 0 output "Error: Cannot divide by zero"else result = numerator / denominator5. Provide Recovery Options
Section titled “5. Provide Recovery Options”try processData(data)catch error { output "Error: {error}" output "Would you like to retry? (yes/no)" input retry
if retry equals "yes" try processData(data) catch error output "Retry failed: {error}"}6. Log Errors for Debugging
Section titled “6. Log Errors for Debugging”function logError takes in errorMessage { try { open file "errors.log" for appending as logFile write "{getCurrentTime()}: {errorMessage}" to logFile close logFile } catch error output "Failed to log error"}
try riskyOperation()catch error { logError(error) output "An error occurred. Details logged."}When to Use Error Handling
Section titled “When to Use Error Handling”Use try-catch when:
- Working with user input
- Accessing files or external resources
- Performing calculations that might fail
- Working with arrays at dynamic indices
- Calling functions that might fail
Don’t use try-catch when:
- You can validate before the operation
- The error indicates a programming mistake
- Performance is critical (validation is faster)
// Unnecessary try-catchtry if x > 0 output "Positive"catch error output "Error"
// Better approachif x > 0 output "Positive"