Siksha Sarovar

Siksha Sarovar (sikshasarovar.com) is a free educational web application that helps students in India learn programming and prepare for academic and competitive exams. The platform offers structured coding courses (C, C++, Python, Java, HTML, CSS, PHP, Power BI, AI, Machine Learning, Data Science), complete university curriculum notes for BCA/MCA students with previous year question papers, Class 10 and Class 12 CBSE/HBSE school notes, and dedicated preparation material for SSC, UPSC, Banking, Railway and other government exams. Browsing the site is completely free and requires no account. Users may optionally sign in with Google solely to save their learning progress, quiz scores and personal preferences across devices.

Privacy Policy | Terms of Service | Contact Siksha Sarovar | About Siksha Sarovar

v4.0.9 · PWA
Siksha Sarovar logo
Siksha Sarovar
Your Learning Universe

Siksha Sarovar is a free e-learning platform for coding courses, BCA university notes and competitive exam preparation. Optional Google sign-in saves your learning progress across devices.

Initializing knowledge base…
Compiling modules 0%

26. Error & Exception Handling

Lesson 23 of 29 in the free PHP Programming notes on Siksha Sarovar, written by Rohit Jangra.

Error and Exception Handling in PHP

Error and exception handling helps in detecting, managing, and debugging runtime problems safely.

Error Logging

Error logging stores error messages in a log file instead of showing to users.

Enable Error Logging

log_errors = On
error_log = error.log

Log Error Manually

error_log("Custom Error Message");
Exam Definition: Error logging records errors for debugging and monitoring.

---

Configuration Directives (php.ini)

DirectiveDescription
display_errorsShow errors
log_errorsLog errors
error_reportingError level
max_execution_timeScript time
memory_limitMemory limit

Example

ini_set("display_errors", 1);
error_reporting(E_ALL);

---

PHP’s Exception Class

PHP provides a built-in Exception class to handle runtime issues.

Basic Exception Handling

try {
   throw new Exception("Error Occurred");
}
catch(Exception $e){
   echo $e->getMessage();
}

Common Exception Methods

MethodPurpose
getMessage()Error message
getCode()Error code
getFile()File name
getLine()Line number

---

Throw New Exception

Exceptions are raised using the throw keyword.

function checkAge($age){
   if($age < 18){
      throw new Exception("Not Eligible");
   }
   return "Eligible";
}

Usage

try{
   echo checkAge(15);
}
catch(Exception $e){
   echo $e->getMessage();
}
Exam Line: throw is used to generate exceptions manually.

---

Custom Exceptions

Custom exceptions are created by extending the Exception class.

Example: Custom Exception

class MyException extends Exception {
   public function errorMsg(){
      return "Custom Error: ".$this->getMessage();
   }
}

Using Custom Exception

try{
   throw new MyException("Invalid Operation");
}
catch(MyException $e){
   echo $e->errorMsg();
}
Exam Tip: Custom exceptions provide application-specific error handling.

---

Difference: Error vs Exception

FeatureErrorException
OccurrenceRuntime issueLogical issue
Handlingerror_reportingtry–catch
ControlLimitedFull control
User FriendlyNoYes

---

Advantages of Exception Handling

  • Clean error handling
  • Better debugging
  • Secure applications
  • Prevents system crash
  • Improves maintainability