Sunday, September 30, 2018

Exception Handling and Custom Exception in PHP


Is article me ham parhen gen exceptions handling ke bare me ke PHP me exceptions kia hoti hain, Exceptions ko kis tarah handle kia jata hay aur apni khud ki custom exceptions kese create krte hain?

What is Exception?

Exception handling ko pharne se pehle hamen pata hona chaiye ke what is exception? Exception ke agar ham meaning dekhen to iska meaning hay alag krna ya rule se hat jana.

In terms of progamming agar ham bat karen to uper wali meaning se connect krke hi to iska matlab ye hua Ke, Koi aisa event jo nh hona chaiye tha lekin wo hogya aur hamare program ke normal flow ko disturb krdiya to ham kahen gen ke koi exception occured hogyi. 
Isliye Programming me exceptions ko handle krna bohat zarori hay, Aur isi handling ko ham kehte hain Exception handling.

Throwing Exception:

Ab jese hi koi exception occured hoti hay kisi method me to exception class ka ek object create hota hay, Phir wo exception object run time system ke hawale hojata hay, Exception object me hamare pas exception se related sari information ajati hay jese kis type ki exception hay, program ka status kia tha jab exception occurred hui etc.

Isi pure process ko jis me exception object create hota hay aur usko run time time system ke hawale kia jata hay ko ham kehte hain
  “Throwing exception”

Exception Handler:

Ab jese hi koi exception object run time system ke pas aya yani throw hua to ab us exception ko handle krna bhi zarori hay. Ab runtime system us exception ko handle krne ke liye koi block of code hoga wo dhonde ga aur is block of code ko hi ham kehten hain “Exception Handler”.

Jab bhi us exception ka handler mil jae ga us handler me likha code excecute hojae gaa, To jo handler current exception ke liye chale ga to ham kahen ke isne exception ko catch krliya.

Try/Catch Block:

Exceptions ko handle krne ke liye ham Try/Catch block ko use krte hain, Wo code jo koi exception throw krskta hay usey ham try block me likthe hain. Phir us exception ko handle krne liye yani us exception ko catch krne ke liye jo code hota hay wo ham catch block me likthe hain.

Exception implicitly bhi throw hoskti hay ya ham usko khud explicitly “throw” keyword use krke throw krskte hain, Yad rhe ham exception ke object ko throw kren gen jis me exception se related sari info hogi phir us exception ko appropriate catch block catch kare ga. Mazeed samajhne ke liye neeche code dekhen:

1.  <?php  
2.    
3.      class Basic{  
4.    
5.          public function divide($a){  
6.              if($a==0){  
7.                  throw new Exception("Division by zero not allowed");  
8.              }  
9.              return 2/$a;  
10.         }  
11.     }  
12.   
13.     $b=new Basic();  
14.     try{  
15.         echo $b->divide(1)."<br>";  
16.         echo $b->divide(0)."<br>";  
17.         echo $b->divide(3)."<br";  
18.     }  
19.     catch(Exception $e){  
20.         echo "Caught Exception: ".$e->getMessage()."<br>";  
21.     }  
22.   
23.     finally{  
24.         echo "Finally Block<br>";  
25.     }  
26.   
27.     echo "Continue Execution";  
28.   
29.   
30. ?>  


Example me hamne explicitly ek exception throw ki kay agar $a ki value 0 ho then ek exception throw ho, Exception object throw krte we ham us ke constructor me optionally koi messege pass krskte hain.


Ab jab ham arithmetic class ka object create Karen gen aur us ke constructor me 0 value pass Karen gen to ek exception throw hogi, Yani arithmetic class ke object ko create krte we chance hay ke koi exception throw hoskti hay, To is liye hamne arithmetic class ke object creation wale code ko try block me dal dia.

Ab agar exception throw hui to usey catch krna zarori hay isiliye ye lazm hay ke try block ke bad catch block ya finally block (Jo neeche parhe gen) me se aik ka hona zarori hay hona zarori hay.

Catch block me jo exception ka object throw hua hay wo as a argument pass hoga. Catch krte we hamen exception ki type dena zarori hay, uski waja hay ke hamare pas multiple catch block hoskta hain jo appropriate exception ko type ke hisab se catch Karen gen.


Finally:

Try/Catch ke ilawa hamare pas ek aur block hotaa hay “Finally” ka. Yeh block jab chalta hay jab either try ka ya catch ka block successfully chalta ha, Catch aur finally block optional hain bus try ke bad in me se kisi ek ka hona zarori hay.

Bas Try/Catch/Finally block use krrte we ek simple rule yad rkhen ke, “If you throw something, you have to catch it.”


Custom Exception Class:

Ab jesa ke hamen pata hay ke har exception object exception class ka instance hota hay, To ham apni ek custom exception class bhi banaskte hain jo exception class ko extend karey gi.

Apni custom exception class banana ka faida yeh hay ke ham ap khud ke koi method banaskte hain aur iske ilawa ham more specific hosktey hain different type ki exceptions banaskte hain, Example dekhen:

1.  <?php  
2.    
3.      class CustomException extends Exception{  
4.    
5.          public function emailError(){  
6.              $mesg=$this->getMessage()." Is Not a Valid Email";  
7.              return $mesg;  
8.          }  
9.      }  
10.   
11.     class CustomClass{  
12.   
13.         public function checkEmail($email){  
14.             if(filter_var($email,FILTER_VALIDATE_EMAIL)==false){  
15.                 throw new CustomException($email);  
16.             }  
17.             return "Valid Email";  
18.         }  
19.     }  
20.   
21.     $b=new CustomClass();  
22.     try{  
23.         echo $b->checkEmail("haniabidkz@gmail.com")."<br>";  
24.     }  
25.     catch(CustomException $e){  
26.         echo "Caught Exception: ".$e->emailError()."<br>";  
27.     }  
28.   
29.       
30.   
31. ?>  


Uper simply hamne ek class banayi hay “Custom class” jis ek method hay check email aur argument me ye ek email le ga aur usey verify kare ga ke email appropriate format me hay ya nh, Agar appropriate format me nhi hay to ek exception throw ho.


Hamne ek custom exception class banyi hay jo exception class ko extend karey gi, Ab exception class me jitney bhi method honge wo hamari custom exception class me inherit hojae gen, Hamne ek additional method bhi banaya ha custom exception class me email error ke name se,  Is method ko banana ka maqsad hay ke ham ek custom mesg show Karen, Although ye ek  proper practical example nhi hay lekin samjhne ke liye kafi hay.

Exception throw krte we ham php ke built in method filter_var ko use krte we check Karen gen ke email appropriate form me hay ya nh then ham constructor me email pass karden gen jo custom exception class ke get Messege method me ajae ga, Custom Exception class me ham get messege method ko emailError me call Karr he hain jis se hamen wo email mil jae ga then Ham agey koi error mesg likhden gen, aur usey kis variable me dal kr return krden gen.


Conclusion:


I hope apko samjh agya hoga ke PHP me exception kis tarah create krte hain aur Exception ko handle kis tarah krskte hain, Agar waqaye apko is article se faida hua then apni coding community me is article ko zaror share Karen taken ye article kisi aur ke liye bhi faida mand sabit hoske aur mera is blog ko create krne ke jo maqasid hain wo pure hosken.


No comments:

Post a Comment