try catch in Java

try catch in Java: हालांकि जावा का Default Exception Handler Debugging के लिए काफी उपयोगी है, फिर भी किसी भी Program में Generate होने वाले किसी भी Exception को अक्सर हम स्वयं ही Handle करते हैं। ऐसा करने से दो फायदे होते हैं। पहला ये कि हमें Error को Fix करने की सुविधा मिल जाती है और दूसरा ये कि इससे Program Automatically Terminate नहीं होता।

जब हमें किसी Code Statement को Exception के लिए Monitor करना होता है, तब हम उस Statement Block को try Block में लिखते हैं और इस Block के Just आगे catch Block लिखते हैं, जिसमें हम उस Exception के Type को Specify करते हैं, जिसे हम catch Block में Catch करना चाहते हैं। इस प्रक्रिया को समझने के लिए निम्न Program देखिए, जिसमें Division By Zero Exception को Handle किया गया है:

// Program
class ExceptionDivisionByZero
{
	public static void main(String args[])
	{
		int number, divider;

		try
		{
			divider = 0;
			number = 10 / divider;
			
			System.out.println(“This will not be printed.”);
		}
		catch(ArithmeticException ae)
		{
			System.out.println(“Division by zero.”);
		}
	System.out.println(“After catch statement”);
	}
}

// Output 
   Division by zero.
   After catch statement

हम देख सकते हैं कि try Block में लिखा गया println() Statement Execute नहीं हो पाया है। ऐसा इसलिए होता है, क्योंकि जैसे ही Exception Generate होता है, try Block एक Exception Object Throw करता है, जिसे catch Block Catch करता है। यानी Program Control Exception Object के साथ ही try Block से बाहर निकल कर catch Block में आ जाता है, इसीलिए try Block का println() Statement Execute नहीं हो पाता है। trycatch Block एक Unit के रूप में काम करते हैं। catch Block तभी Execute होता है, जब try Block में कोई Exception Generate होता है।

Throwable Class में Object Class द्वारा Define किए गए toString() Method को Override किया गया है, ताकि Throwable Class Exception की Description को String के रूप में Display किया जा सके। Exception की Description को एक Argument के रूप में println() Method में Pass करके Display करवा सकते हैं। यानी try Block द्वारा Throw किए जाने वाले किसी Exception Object को catch Block में हम निम्नानुसार किसी println() Statement द्वारा भी Display करवा सकते हैं:

// Program
	class ExceptionDivisionByZero{
		public static void main(String args[]){
			int number, divider;
	
			try{
				divider = 0;
				number = 10 / divider;
				
				System.out.println("This will not be printed.");
			}
			catch(ArithmeticException ae){
				System.out.println("Exception : " + ae );
			}
			System.out.println("After catch statement");
		}
	}

// Output 
   Exception : java.lang.ArithmeticException: / by zero
   After catch statement

हालांकि इस प्रकार के Description का User के लिए कोई महत्व नहीं होता है। लेकिन फिर भी जब हम किसी Program को Debug कर रहे होते हैं, उस समय इस प्रकार के Description का एक Programmer के लिए काफी उपयोग होता है।

Java Programming Language in Hindiये Article इस वेबसाईट पर Selling हेतु उपलब्‍ध EBook Java in Hindi से लिया गया है। इसलिए यदि ये Article आपके लिए उपयोगी रहा, तो निश्चित रूप से ये पुस्तक भी आपके लिए काफी उपयोगी साबित होगी। 

Java Programming Language in Hindi | Page: 682 | Format: PDF

BUY NOW GET DEMO REVIEWS