Thursday 2 December 2021

Dec-2019(2) How Exceptional handling is done in VB. Net?

An exception is a problem that arises during the execution of a program. An exception is a response to an exceptional circumstance that arises while a program is running, such as an attempt to divide by zero.

अपवाद एक समस्या है जो किसी प्रोग्राम के निष्पादन के दौरान उत्पन्न होती है। एक अपवाद एक असाधारण परिस्थिति की प्रतिक्रिया है जो एक Program के चलने के दौरान उत्पन्न होती है, जैसे कि शून्य से विभाजित करने का प्रयास।

Exceptions Handling provide a way to transfer control from one part of a program to another. VB.Net exception handling is built upon four keywords - TryCatchFinally and Throw.

अपवाद प्रबंधन प्रोग्राम के एक भाग से दूसरे भाग में नियंत्रण स्थानांतरित करने का एक तरीका प्रदान करते हैं। VB.Net अपवाद प्रबंधन चार कीवर्ड - ट्राई, कैच, अंत में और थ्रो पर आधारित है।

·        Try − A Try block identifies a block of code for which particular exceptions will be activated. It's followed by one or more Catch blocks.

ट्राई ब्‍लॉक में अपवाद को लिखा जाता है उसका प्रबंधन किया जाता है। उसके बाद एक या एक से अधिक केच ब्‍लॉक द्वारा समस्‍या का प्रबंधन किया जाता है।

·        Catch − A program catches an exception with an exception handler at the place in a program where you want to handle the problem. The Catch keyword indicates the catching of an exception.

    यह ब्‍लॉक अपवाद होने पर अपवाद को संभालता है।

·        Finally − The Finally block is used to execute a given set of statements, whether an exception is thrown or not thrown. For example, if you open a file, it must be closed whether an exception is raised or not.

फाइनली ब्लॉक का उपयोग लिखे गये विभिन्‍न कोड स्‍टेटमेंटों को निष्पादित करने के लिए किया जाता है, चाहे कोई अपवाद फेंका गया हो या नहीं फेंका गया हो। उदाहरण के लिए, यदि आप कोई फ़ाइल खोलते हैं, तो उसे बंद किया जाना चाहिए, चाहे कोई अपवाद उठाया गया हो या नहीं।

·        Throw − A program throws an exception when a problem shows up. This is done using a Throw keyword.

जब कोई समस्या दिखाई देती है तो एक प्रोग्राम अपवाद फेंकता है। यह थ्रो कीवर्ड का उपयोग करके किया जाता है।

Syntax

Try
   [ tryStatements ]
   [ Exit Try ]
 
[ Catch [ exception [ As type ] ] [ When expression ]
   [ catchStatements ]
   [ Exit Try ] ]
 
[ Catch ... ]
 
[ Finally
   [ finallyStatements ] ]
 
End Try

 

Handling Exceptions

VB.Net provides a structured solution to the exception handling problems in the form of try and catch blocks. Using these blocks the core program statements are separated from the error-handling statements.

वीबी डोट नेट अपवादों से निपटने के लिए ट्राई व कैच ब्‍लॉक के रूप में एक स्‍ट्रक्‍चर्ड सॉल्‍यूशन उपलब्‍ध कराता है। इन ब्‍लॉकों का इस्‍तेमाल करके मुख्‍य प्रोग्राम को इरर हैडलिंग स्‍टेटमेंटस से अलग रखा जाता है।

These error handling blocks are implemented using the TryCatch and Finally keywords.

Following is an example of throwing an exception when dividing by zero condition occurs −

 

Module exceptionProg
   Sub division(ByVal num1 As Integer, ByVal num2 As Integer)
      Dim result As Integer
      Try
         result = num1 \ num2
      Catch e As DivideByZeroException
         Console.WriteLine("Exception caught: {0}", e)
      Finally
         Console.WriteLine("Result: {0}", result)
      End Try
   End Sub
   Sub Main()
      division(25, 0)
      Console.ReadKey()
  End Sub
End Module

When the above code is compiled and executed, it produces the following result −

Exception caught: System.DivideByZeroException: Attempted to divide by zero. 
at ...
Result: 0

 

No comments:

Post a Comment