Practice 50 Swift Error Handling Programming Questions
Q1. Write a Swift program that defines a function divide(a: Int, b: Int) which throws an error if b is zero and handles the error using a do-catch block.
Input:
a = 10, b = 2
Expected Output:
Result: 5
Q2. Write a Swift program that defines an enum DivisionError to represent a divide-by-zero error, and uses it in a function that throws an error when division by zero occurs.
Input:
a = 15, b = 0
Expected Output:
Error: Division by zero
Q3. Write a Swift program to handle an array index out of bounds error by throwing a custom error and handling it using do-catch.
Input:
array = [1, 2, 3], index = 5
Expected Output:
Error: Index out of bounds
Q4. Write a Swift program that defines a function parseNumber(string: String) which throws an error if the string cannot be converted to an integer.
Input:
string = "abc"
Expected Output:
Error: Invalid number format
Q5. Write a Swift program to demonstrate the usage of try? when calling a function that might throw an error.
Input:
numberString = "abc"
Expected Output:
Error: Invalid number format
Q6. Write a Swift program that uses try! to call a function that throws an error, and demonstrates what happens when an error is thrown.
Input:
string = "abc"
Expected Output:
Fatal error: unexpectedly found nil while unwrapping an Optional value
Q7. Write a Swift program that catches multiple types of errors using a do-catch block.
Input:
a = -5, b = 10
Expected Output:
Error: Invalid negative value
Q8. Write a Swift program that demonstrates how to throw a custom error in a function and catch it with a do-catch block.
Input:
age = -1
Expected Output:
Error: Invalid age
Q9. Write a Swift program that defines a function checkPassword(password: String) which throws an error if the password is shorter than 6 characters.
Input:
password = "12345"
Expected Output:
Error: Password too short
Q10. Write a Swift program that demonstrates how to create a custom error type with associated values and use it for error handling.
Input:
username = "john_doe"
Expected Output:
Error: Username already taken
Q11. Write a Swift program that demonstrates handling multiple errors in a do-catch block with specific error messages for each case.
Input:
value = -3
Expected Output:
Error: Value must be positive
Q12. Write a Swift program to demonstrate how to propagate errors from one function to another using throws and try.
Input:
a = 10, b = 0
Expected Output:
Error: Cannot divide by zero
Q13. Write a Swift program to show the usage of defer to ensure code executes regardless of whether an error occurs or not.
Input:
array = [1, 2, 3]
Expected Output:
Defer executed
Q14. Write a Swift program that demonstrates how to handle an error with a do-catch block and prints the error description.
Input:
number = "abc"
Expected Output:
Error: Invalid number format
Q15. Write a Swift program that demonstrates using try? to handle an optional return value and check for error handling.
Input:
filePath = "invalid/path"
Expected Output:
Error: File not found
Q16. Write a Swift program that defines a function createAccount(username: String) which throws an error if the username already exists.
Input:
username = "admin"
Expected Output:
Error: Username already taken
Q17. Write a Swift program to handle a custom error in a function that opens a file and throws an error if the file is not found.
Input:
filePath = "missing_file.txt"
Expected Output:
Error: File not found
Q18. Write a Swift program that throws an error if a string exceeds a certain length and catches the error in a do-catch block.
Input:
string = "This is a very long string that exceeds the limit"
Expected Output:
Error: String too long
Q19. Write a Swift program to define a function withdraw(amount: Double) which throws an error if the withdrawal amount exceeds the account balance.
Input:
balance = 100, withdrawalAmount = 150
Expected Output:
Error: Insufficient funds
Q20. Write a Swift program that demonstrates throwing an error inside a closure and handling it outside the closure.
Input:
closure = { throw SomeError.someError }
Expected Output:
Error: Some error occurred
Q21. Write a Swift program that shows how to use a guard statement for early error handling in a function.
Input:
age = -5
Expected Output:
Error: Invalid age
Q22. Write a Swift program that demonstrates catching an error thrown by a method inside a class and handling it with do-catch.
Input:
amount = -100
Expected Output:
Error: Invalid amount
Q23. Write a Swift program that throws an error if the email is invalid and catches the error.
Input:
email = "invalidemail.com"
Expected Output:
Error: Invalid email address
Q24. Write a Swift program to catch a specific error from a do-catch block and display a custom message.
Input:
value = -1
Expected Output:
Error: Negative value not allowed
Q25. Write a Swift program that defines a function parseJSON(data: Data) which throws an error if the JSON is malformed.
Input:
data = malformed JSON data
Expected Output:
Error: JSON parsing failed
Q26. Write a Swift program that demonstrates error handling for invalid user input in a user registration system.
Input:
username = "short"
Expected Output:
Error: Username too short
Q27. Write a Swift program to handle errors while parsing an integer from a string, using a do-catch block.
Input:
string = "abc"
Expected Output:
Error: Invalid integer format
Q28. Write a Swift program to handle an invalid file path error using a do-catch block.
Input:
filePath = "invalid/path.txt"
Expected Output:
Error: File not found at invalid/path.txt
Q29. Write a Swift program that defines an error enum FileError to handle file errors like not found and permission denied.
Input:
filePath = "secure/path"
Expected Output:
Error: Permission denied
Q30. Write a Swift program that demonstrates the use of try? for error handling and returns nil if the error is thrown.
Input:
input = "abc"
Expected Output:
Error: Invalid input
Q31. Write a Swift program that demonstrates custom error handling for a network request failure.
Input:
requestStatus = "failure"
Expected Output:
Error: Network request failed
Q32. Write a Swift program to handle an error while trying to read from a file that doesn't exist.
Input:
filePath = "missing_file.txt"
Expected Output:
Error: File not found
Q33. Write a Swift program to demonstrate error handling with a do-catch block for a temperature conversion function.
Input:
temperature = -500
Expected Output:
Error: Invalid temperature value
Q34. Write a Swift program that catches and logs an error thrown from a function and then rethrows the error.
Input:
number = -5
Expected Output:
Error: Negative number not allowed
Q35. Write a Swift program to handle errors thrown by a method that parses a file and displays an error message if the file is corrupted.
Input:
filePath = "corrupted.txt"
Expected Output:
Error: File is corrupted
Q36. Write a Swift program that defines a function performOperation(a: Int, b: Int) which throws an error for division by zero.
Input:
a = 10, b = 0
Expected Output:
Error: Cannot divide by zero
Q37. Write a Swift program to handle a network error thrown when making an API request.
Input:
requestStatus = "failure"
Expected Output:
Error: Network request failed
Q38. Write a Swift program to throw an error when an input value exceeds the specified limit.
Input:
input = 200
Expected Output:
Error: Input exceeds limit
Q39. Write a Swift program that throws an error if an invalid age is provided and handles it using do-catch.
Input:
age = -1
Expected Output:
Error: Invalid age
Q40. Write a Swift program to handle errors while parsing an integer from a string and prints a custom error message.
Input:
string = "invalid"
Expected Output:
Error: Invalid format
Q41. Write a Swift program to handle errors thrown during JSON decoding with a custom error message.
Input:
jsonString = "{\"key\": \"value\"}"
Expected Output:
Error: JSON decoding failed
Q42. Write a Swift program to demonstrate error handling while performing a file read operation and handle file not found errors.
Input:
filePath = "nonexistent.txt"
Expected Output:
Error: File not found
Q43. Write a Swift program to handle division by zero error gracefully by using a custom error type.
Input:
a = 10, b = 0
Expected Output:
Error: Cannot divide by zero
Q44. Write a Swift program to demonstrate how error handling works when an invalid URL is provided.
Input:
url = "invalid://url"
Expected Output:
Error: Invalid URL
Q45. Write a Swift program to handle errors when trying to access a dictionary key that does not exist.
Input:
dict = ["name": "John"], key = "age"
Expected Output:
Error: Key not found
Q46. Write a Swift program that throws an error when trying to add a negative value to a bank account balance.
Input:
balance = 100, amount = -50
Expected Output:
Error: Cannot add negative balance
Q47. Write a Swift program to handle errors when a user attempts to withdraw more money than their balance allows.
Input:
balance = 100, withdrawal = 200
Expected Output:
Error: Insufficient funds
Q48. Write a Swift program to handle an error thrown during a network request by printing a custom error message.
Input:
requestStatus = "failure"
Expected Output:
Error: Network request failed
Q49. Write a Swift program to throw a custom error when an invalid file path is provided, and catch that error in a do-catch block.
Input:
filePath = "invalid/path"
Expected Output:
Error: Invalid file path
Q50. Write a Swift program to handle a failed attempt to parse a date using a do-catch block and display an appropriate error message.
Input:
date = "invalid-date-string"
Expected Output:
Error: Invalid date format
Bikki Singh
Hi, I am the instructor of TechnoVlogs. I have a strong love for programming and enjoy teaching through practical examples. I made this site to help people improve their coding skills by solving real-world problems. With years of experience, my goal is to make learning programming easy and fun for everyone. Let's learn and grow together!