Practice 50 PHP Exception Handling Programming Questions
Q1. Write a PHP program to demonstrate the basic use of try, catch, and finally.
Input: Divide 10 by 0
Expected Output: "Exception caught: Division by zero"
Q2. Write a PHP program to throw a custom exception when a negative number is passed to a function.
Input: -5
Expected Output: "Negative numbers are not allowed"
Q3. Write a PHP program to create a function that throws an exception when a string is empty.
Input: "" (empty string)
Expected Output: "Empty string not allowed"
Q4. Write a PHP program to handle multiple exceptions using multiple catch blocks.
Input: 10 / 0 and null variable access
Expected Output: "Division by zero error caught" and "Undefined variable error caught"
Q5. Write a PHP program to define a custom exception class and use it in a function.
Input: Age = 15
Expected Output: "Age must be 18 or above"
Q6. Write a PHP program to use throw to manually trigger an exception inside a loop.
Input: Values [1, 2, 3, -1, 5]
Expected Output: "Negative value found, terminating loop"
Q7. Write a PHP program to catch an exception inside a function and rethrow it.
Input: Invalid Email = "test@.com"
Expected Output: "Invalid email format exception rethrown"
Q8. Write a PHP program to handle exceptions inside a constructor.
Input: Invalid Age = -10
Expected Output: "Invalid age: Cannot be negative"
Q9. Write a PHP program to use finally block to close a database connection even when an exception occurs.
Input: Database connection fails
Expected Output: "Database connection closed"
Q10. Write a PHP program to define multiple exception classes and catch them separately.
Input: Invalid Username = "", Invalid Age = -1
Expected Output: "Invalid username exception" and "Invalid age exception"
Q11. Write a PHP program to log exceptions into a file using error_log().
Input: Divide 5 by 0
Expected Output: "Exception logged in error log file"
Q12. Write a PHP program to create a function that throws an exception when an array index is out of bounds.
Input: array[5] (index does not exist)
Expected Output: "Index out of bounds exception"
Q13. Write a PHP program to catch an exception and display a user-friendly error message.
Input: 10 / 0
Expected Output: "Something went wrong, please try again"
Q14. Write a PHP program to throw an exception when a file does not exist.
Input: File = "nonexistent.txt"
Expected Output: "File not found exception"
Q15. Write a PHP program to throw an exception if an API response is empty.
Input: API response = null
Expected Output: "API response is empty"
Q16. Write a PHP program to handle exceptions using the Exception class and get the exception message.
Input: new Exception("Invalid input")
Expected Output: "Exception: Invalid input"
Q17. Write a PHP program to catch and handle a DivisionByZeroError exception.
Input: 10 / 0
Expected Output: "Cannot divide by zero"
Q18. Write a PHP program to define a function that throws an exception if a string length exceeds a limit.
Input: "HelloWorld" (length > 5)
Expected Output: "String length exceeded limit"
Q19. Write a PHP program to implement exception handling in a file upload process.
Input: Invalid file type = .exe
Expected Output: "Invalid file type exception"
Q20. Write a PHP program to throw an exception when an array is empty.
Input: []
Expected Output: "Array cannot be empty"
Q21. Write a PHP program to throw an exception when a password is less than 6 characters long.
Input: "123"
Expected Output: "Password too short"
Q22. Write a PHP program to validate an email and throw an exception if it’s invalid.
Input: "invalidemail"
Expected Output: "Invalid email address"
Q23. Write a PHP program to handle exceptions using the getCode() method.
Input: Exception Code = 404
Expected Output: "Error Code: 404 - Page Not Found"
Q24. Write a PHP program to implement a try-catch block inside a loop to handle multiple errors.
Input: [5, 10, 0, 15]
Expected Output: "Cannot divide by zero at index 2"
Q25. Write a PHP program to use getTrace() to print exception trace details.
Input: Function calls exception
Expected Output: "Exception occurred in function XYZ at line 10"
Q26. Write a PHP program to handle exceptions in a recursive function.
Input: Factorial of -5
Expected Output: "Factorial of a negative number is not allowed"
Q27. Write a PHP program to catch an ErrorException.
Input: trigger_error("Custom error", E_USER_ERROR)
Expected Output: "Caught custom error exception"
Q28. Write a PHP program to throw and catch exceptions in an if-else condition.
Input: Score = -5
Expected Output: "Score cannot be negative"
Q29. Write a PHP program to check if an exception message contains a specific word.
Input: Exception Message = "Invalid Password"
Expected Output: "Exception contains the word 'Invalid'"
Q30. Write a PHP program to throw an exception when a user tries to withdraw more than their balance.
Input: Balance = 1000, Withdrawal = 1500
Expected Output: "Insufficient balance exception"
Here are more PHP programming questions on the topic Exception Handling at the easy difficulty level:
Q31. Write a PHP program to catch and display the exception line number using getLine().
Input: throw new Exception("Error found")
Expected Output: "Error found on line 12"
Q32. Write a PHP program to handle division by zero using an exception.
Input: 25 / 0
Expected Output: "Cannot divide by zero"
Q33. Write a PHP program to handle an exception if a JSON string is invalid.
Input: "{name: 'John', age: 30}" (missing double quotes)
Expected Output: "Invalid JSON format"
Q34. Write a PHP program to throw an exception if a string does not contain any digits.
Input: "abcdef"
Expected Output: "String must contain at least one digit"
Q35. Write a PHP program to validate a phone number and throw an exception if it’s not 10 digits long.
Input: "98765"
Expected Output: "Invalid phone number format"
Q36. Write a PHP program to catch an exception when accessing a private property of a class.
Input: Attempt to access private $name
Expected Output: "Cannot access private property"
Q37. Write a PHP program to catch an exception when calling a method on null object.
Input: $obj = null; $obj->method();
Expected Output: "Call to a member function on null"
Q38. Write a PHP program to create a function that throws an exception when a date is in the past.
Input: "2020-01-01"
Expected Output: "Date cannot be in the past"
Q39. Write a PHP program to validate a username and throw an exception if it contains special characters.
Input: "John@123"
Expected Output: "Username contains invalid characters"
Q40. Write a PHP program to catch and log database connection exceptions.
Input: Database connection fails
Expected Output: "Database error logged"
Q41. Write a PHP program to handle an exception when fopen() fails to open a file.
Input: "nonexistentfile.txt"
Expected Output: "File could not be opened"
Q42. Write a PHP program to throw an exception if a password does not contain an uppercase letter.
Input: "password123"
Expected Output: "Password must contain at least one uppercase letter"
Q43. Write a PHP program to throw an exception if a password does not contain a special character.
Input: "Password123"
Expected Output: "Password must contain at least one special character"
Q44. Write a PHP program to throw an exception when an email domain is not gmail.com.
Input: "user@yahoo.com"
Expected Output: "Only Gmail emails are allowed"
Q45. Write a PHP program to catch an exception when a function is called with missing parameters.
Input: Function expecting 2 parameters but 1 given
Expected Output: "Missing required function parameters"
Q46. Write a PHP program to throw an exception when an image file size exceeds 2MB.
Input: Image size = 5MB
Expected Output: "File size exceeds the limit"
Q47. Write a PHP program to catch an exception when a file extension is not allowed.
Input: "script.exe"
Expected Output: "Unsupported file format"
Q48. Write a PHP program to throw an exception if a string contains more than 3 spaces.
Input: "Hello World !"
Expected Output: "Too many spaces in input"
Q49. Write a PHP program to validate an integer and throw an exception if it is a float.
Input: 12.5
Expected Output: "Expected an integer, received a float"
Q50. Write a PHP program to throw an exception if a number is greater than 1000.
Input: 1500
Expected Output: "Number exceeds the allowed limit"
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!