PHP throw Exception In Hindi | throw Custom Error In PHP In Hindi

📔 : PHP 🔗

पिछले topic में आपने program में आयी Exceptions को handle करना सीखा। इस topic में हम सीखेंगे कि exceptions को manually कैसे generate करते हैं और custom exceptions को कैसे generate करते हैं।

क्योंकि Program implement करते समय कई जगह हमें किसी particular condition पर exception generate करनी पड़ जाती है।

Throwing Exceptions in PHP

बाकी languages like Java , JavaScript , C++ की तरह ही PHP में भी Exception / Error throw करने के लिए throw keyword का use किया जाता है।

throw keyword , explicitly किसी error / exception को generate करता है। explicitly का मतलब है स्पष्ट रूप से या clearly . क्योंकि कई जगह हमें ऐसी need पड़ जाती हैं जहाँ पर हम अपनी तरफ से भी exception को throw / create कर सकें।

PHP throw Exception Syntax

throw new className("Error message");

PHP throw Exception Example

function divide($numerator, $denominator) { if ($denominator === 0) { throw new Exception("Division by zero is not allowed."); } return $numerator / $denominator; } try { $result = divide(10, 2); echo "Result: " . $result . "\n"; $result = divide(8, 0); // This will throw an exception. echo "Result: " . $result . "\n"; } catch (Exception $e) { echo "Caught exception: " . $e->getMessage() . "\n"; }

Output

Result: 5
Caught exception: Division by zero is not allowed.

Example में आप देख सकते हैं कि $denominator 0 होने पर किस तरह से throw keyword का use करके exception को throw कर दिया।

Important*

ध्यान रहे exception throw करते समय दिया गया class का name Exception या इसकी कोई sub class का name ही होना चाहिए। और अगर कोई user defined class use की है तो वह class Exception class को Inherit करनी चाहिए।

PHP Exception class

custom PHP exceptions को generate करने के लिए और किसी user defined class द्वारा Exception class को Inherit करने से पहले हम Exception class को समझ लेते हैं।

PHP में Exception class एक built-in base class जिसका use exceptions को handle और create करने के लिए किया जाता है। Exception class के कुछ key features और methods इस तरह से हैं -

class Exception { protected $message = 'Unknown exception'; // exception message private $string; // __toString cache. protected $code = 0; // user defined exception code. protected $file; // source filename of exception. protected $line; // source line of exception. private $trace; // backtrace. private $previous; // previous exception if nested exception. public function __construct($message = null, $code = 0, Exception $previous = null); final private function __clone(); // Inhibits cloning of exceptions. final public function getMessage(); // message of exception. final public function getCode(); // code of exception. final public function getFile(); // source filename. final public function getLine(); // source line. final public function getTrace(); // an array of the backtrace(). final public function getPrevious(); // previous exception. final public function getTraceAsString(); // formatted string of trace. // Overrideable public function __toString(); // formatted string for display. }

Throw custom PHP exception

// extend Exception class. class CustomException extends Exception { public function __construct($message = "Custom Exception", $code = 0, Throwable $previous = null) { parent::__construct($message, $code, $previous); } public function customFunction() { return "This is a custom function for the custom exception."; } } try { // now throw custom exception. $value = 0; if ($value === 0) { throw new CustomException("Value cannot be zero."); } } catch (CustomException $e) { echo "Caught custom exception: " . $e->getMessage() . "\n"; echo "Custom function result: " . $e->customFunction() . "\n"; } catch (Exception $e) { echo "Caught generic exception: " . $e->getMessage() . "\n"; }

Output

Caught custom exception: Value cannot be zero.
Custom function result: This is a custom function for the custom exception.

Related Topics :

Rahul Kumar

Rahul Kumar

Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)

Get connected with me. :) LinkedIn Twitter Instagram Facebook

b2eprogrammers