C# exception handling in hindi-C# एक्सेप्शन हैंडलिंग क्या है?

हेल्लो दोस्तों आज के इस पोस्ट में आपको c# exception handling in hindi के बारे में आपको बताया जा रहा है की क्या होता है कैसे काम करता है तो चलिए शुरू करते है

introduction to c# exception handling

  • c# में run time error को exceptions के माध्यम से represent किया जाता है यानी की c# में run time error को representation exceptions है
  • आसान शब्दों में कहा जाए तो c# में अलग अलग तरह के run time error को categories किया गया है और उन्हें ही exceptions का नाम दिया गया है
  • exceptions ऐसे code के द्वारा throw(create) की जाती है जिसमे run time errors आती है और ऐसे code के द्वारा catch(handle) की जाती है जो उस error को सुधार सके
  • c# में सभी exceptions को classes होती है ये classes System.exception class से derived होती है इस class में ऐसी properties को available होती है जिनके माध्यम से exception के बारे में details से जानकारी प्राप्त कर दिया जा रहा है

c# में available exception को classes की list निचे दिया जा रहा है

  • IO.IOExeption –यह class सभी I/O errors को represent करती है
  • IndexOutOfRangeException –यह class के उन index को represent करती है जो array की index out of range होने से generate होती है
  • ArrayTypeMismatchException-यह class के उन errors को represent करती है जो किसी भी type के errors को represent करती है जो किसी type के array के type से mismatch होने पर generate होती है
  • NullReferenceException –यह class के उन errors को represent करती है जो null object को point करने से generate होती है
  • DivideByZeroException-यह class के उन errors को represent करती है जो किसी number को zero से divide करने पर generate होती है
  • InvalidCastException-यह class के उन errors को represent करती है जो invalid type के casting की वजह से generate होती है
  • OutOfMemoryException-यह class के उन errors को represent करती है जो मेमोरी की कमी के कारण generate होती है
  • StackOverflowException-यह class stack को overflow होने के कारण generate होने वाली errors को represent करती है
  • InvaliedOperationException-यह class के invalid argument के आलावा किसी भी दूसरी reason से method को call fail होने वाली errors को represent करती है
  • ArgumentException-यह class के उन errors को represent करती है जो किसी method को गलत argument pass करने की वजह से generate होती है
  • ArgumentNullExeption-यह class के उन errors को represent करती है जो argument के रूप में null pass करने से generate होती है
  • ArgumentOutOfRangeException-यह class के उन errors को represent करती है जो out of range argument pass करने की वजह से generate होती है
  • AccessVoilationException-यह class के उन errors को represent करती है जो protected members को inherit करने की वजह से generate होती है

exceptions को कोई errors आने पर CLR(common language runtime) के द्वारा automatically throw की जाती है या फिर से exception को प्रोग्रामर स्वय क्रिएट करके explicitly भी throw कर सकते है

handling exception in c#

c# में exceptions को handle करने के लिए try,catch और finally blocks का प्रयोग किये जाते है इनके बारे में आपको निचे दिया जा रहा है

try

try block में वे statements लिखे जाते है जो जिनसे run time error को generate हो सकती है इन ब्लाक का general syntax आपको निचे दिया जा रहा है

try
{
     //statements that may generate run time error
}

catch

catch block में वे statements को लिखे जाते है जो आप exception को generate होने पर execute करवाना चाहते है इस block को exception को handler भी कहा जाता है इसका syntax आपको निचे दिया जा रहा है

catch(exception-class obj)
{
    //statement to handle exception
}

जैसा की आप उपर दिए गए syntax में आप देख सकते है की catch block किसी भी function की तरह parameter को डिफाइन करते है असल में यह parameter उस class के object को represent करता है जो try ब्लाक में generate होने वाली errors को represent करती है

example के लिए आप यदि try block में divide by zero से सम्बंधित error की संभावना है तो parameter के रूप में आप divideByZeroException class का object को डिफाइन करेंगे

एक बात आपको ध्यान में रखनी चाहिए की जिस class में type का object आप parameter के रूप में डिफाइन करते है तो वह catch ब्लाक उन्ही errors को catch करता है यानी की DivideByException का object को डिफाइन करके आप IOExceptions को catch नहीं कर सकते है इसके लिए आपको अलग अलग से catch ब्लाक को डिफाइन करना होगा

आप एक से अधिक से catch block को डिफाइन कर सकते है लेकिन आप try block एक ही होता है जिस type की exception को generate होती है उससे संबधित catch blocks को skip कर दिया जाते है

finally

finally ब्लाक में वह code को डिफाइन किया जाता है जो exception को generate हो न हो लेकिन हर case में execute होगा इस ब्लाक में मुख्यत: network और मेमोरी resource को free करने के लिए code लिखा जाता है इसका syntax आपको निचे दिया जा रहा है

finally
{
    //code that will be executed regardless of exception occurs or not
}

example of c# exception handling

निचे दिए गए c# म exception को handle करना example आपको निचे समझाया गया है

Using System;
Class exceptionDemo
{
      Static void Main(string[] args)
       {
              Int num=4;
               Int result=0;
              Try
              {
                    Retrun=num/0;
               }
               Catch(DivideByZeroException e)
                {
                      Console.WriteLine(“following exception occurred{0}”,e.Message);
                 }
                 Finally
                  {
                         Console.WriteLine(“result is :{0}”,result);
                   }
           }
}

उपर दिए गए example में आप देख सकते है की इसका आउटपुट क्या होता है

Following exception occurred :attempted to divided by zero
result is: 0

Throwing custom exception

c# में custom को exception के throw करने के लिए आपको exception class को inherit करना होता है exception class को inherit करने वाली class और throw keyword के माध्यम से ही exception के throw की जाती है

exception class को inherit करने का syntax आपको निचे दिया जा रहा है

public class yourExceptionClassName:
Exception
{
      Public yourExceptionClassName(string message):base(message)  //constructor
       {
        }
}

जैसा की आप उपर दिए गए syntax में आप देख सकते है की exception class को inherit करने के बाद आप स्वयम की class में एक constructor को डिफाइन करते है जो class का object को क्रिएट करने पर call होता है यह constructor argument के रूप में string लेता है यह string base class यानी exception को class को pass की जाती है और बाद में catch ब्लाक में objectके द्वारा एक्सेस की जाती है

exception class को inherit करने वाली class को आपकी custom exception को represent करती है इस class का object के throw keyword के साथ ही प्रयोग किये जाने पर exception को generate करेंगे और argument के रूप में pass किये गए message को show करेगा

throw keyword के द्वारा exception के throw करने का syntax आपको निचे दिया जा रहा है

throw new
yourExceptionClassName(“message-here”);

throw और new keyword के द्वारा आप exception को generate करते है और उसमे application message को pass करते है

c# में यूजर को defined exception के throw करना निचे example के द्वारा समझ सकते है

using System;
public class invalidCountryException:
Exception
{
       Public invalidCountryException(string message):base(message)
       {
        }
}
Class customExceptionDemo
{
    Static void displayCountry(string country)
         {
              If(country!=”india”)
              {
                     Throw new
invalidCountryException(“invalid country exception :our services are only available in india”);
          }
          Console.WriteLine(“country is :{0}”,country);
          }
         Static void Main(string[] args)
          {
                String country=”Nepal”;
                Try
                 {
                      displayCountry(country);
                 }
                 Catch(invalidCountryException e)
                 {
                         Console.WriteLine(e.message)’;
                 }
              }
}

उपर दिए गए example में आप देख सकते है की इस प्रोग्राम का आउटपुट क्या generate होता है

Invalid Country Exception : Our service are only available in india

c# exception handling in hindi

 

reference-https://www.tutorialspoint.com/csharp/csharp_exception_handling.htm

c# exception handling in hindi

निवेदन:-आप सभी छात्र –छात्रों से निवेदन है की अगर आपको ये Topic(c# exception handling in hindi) अच्छा लगा हो तो कृपया आप इस वेबसाइट के बारे में अपने दोस्तों को जरुर बताये अगर कोई topic(c# exception handling in hindi) से संबधित प्रश्न हो तो कमेंट्स(comments) आपके लिए ही बना है और किसी Subject के लेकर भी कोई प्रश्न हो तो कमेंट करे “आपका comments हमारे लिए बहुमूल्य है “   *_*धन्यवाद

Leave a Comment