throw and throws keyword in Java in Hindi

Last Updated on May 23, 2023 by RAJENDRAPRASAD

throw and throws keyword in java in hindi – Hello दोस्तों rajhindime.in में आपका स्वागत है |

दोस्तों, पिछले post Exception Handling in Java in Hindi में आपने Exception, उसके types और उसे कैसे handle करते हैं इत्यादि के बारे में जाना |

आज के इस post में आप दो नए keywords throw और throws के बारे में विस्तार से जानेंगे जो कि, Exception Handling में help करते हैं |

दोस्तों अब तक आपने जाना कि Exception object हमेशा उस method द्वारा ही create किया जाता है जिसमें, exception occur हुआ हो | परन्तु, अगर programmer चाहता है कि वह खुद manually exception object create करे, उस case में throw keyword का use किया जाता है |

throw keyword in Java:

throw keyword का use explicitly किसी भी exception को throw करने के लिए किया जाता है | कहने का सीधा-सीधा मतलब यह है कि अगर programmer चाहता है कि, वह किसी भी program में manually किसी भी exception को throw करे तब, उस case में वह throw keyword का use करता है |

अब आप सोच रहे होगे कि, इसका real time example क्या हो सकता है, तो मैं आपको बताना चाहुँगा कि इसका अधिकतर use testing के लिए किया जाता है | Example के लिए मान लो, किसी building में fire sensor लगा है, अब उसे test करने के लिए आप क्या करोगे, क्या आप पूरी building में आग लगा दोगे ? नहीं | आप ठीक उस sensor के नीचे थोड़ा सा धुंआ या फिर आग जलाओगे, और check करोगे कि sensor आपना काम कर रहा है या नहीं |

ठीक उसी तरह अगर कोई programmer जानना चाहता है कि, उसने किसी exception को अच्छी तरह से handle किया है या नहीं, उसे check करने के लिए वह manually उस program में exception पैदा/create करेगा और check करेगा |

throw keyword एक बार में एक ही exception throw करता है | इसे हम किसी भी method अथवा किसी भी block में use कर सकते हैं | यह checked तथा unchecked दोनों ही exception को throw कर सकता है |

ध्यान दो, throw keyword का काम केवल programmer द्वारा manually exception object को create करना है, यह कभी भी exception handle नहीं करता |

Exception handle करने के लिए हमें, try catch use करना ही होगा | अगर हम exception handle करते हैं, उस case में वह exception object का information, catch() {} द्वारा print किया जाता है| यदि exception handle नहीं किया तो वह object, उस method द्वारा(जिसमें exception occur हुआ हो) JVM को दे दिया जाता है और JVM उस object को Default Exception handler को दे देता है और Default Exception handler उस object information को print कर देता है |

Syntax

throw new ExceptionClassName(“AnyMessage”)

Program 1:

public class Throw_demo1 {

	public static void main(String[] args) {

		throwException();
		System.out.println("Hello");
	}

	public static void throwException() {
		throw new ArithmeticException("exception occurred");
	}
}

OutPut:

Exception in thread "main" java.lang.ArithmeticException: exception occurred
	at com.exceptionHandling.Throw_demo1.throwException(Throw_demo1.java:10)
	at com.exceptionHandling.Throw_demo1.main(Throw_demo1.java:5)

Explanation:

ऊपर program 1 में, throw का use करके programmer द्वारा manually ArithmeticException throw किया गया है |

ध्यान दो, यहाँ हमने केवल exception throw किया है, उसे handle नहीं किया है, इसलिए यहाँ program का termination abnormal हुआ है, जिसके कारण System.out.println(“Hello”); यह code execute ही नहीं हुआ |

Program 2:

public class Throw_demo2 {

	public static void main(String[] args) {

		throwException();
		System.out.println("Hello");
	}

	public static void throwException() {
		try {
			throw new ArithmeticException("exception occurred");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

OutPut:

java.lang.ArithmeticException: exception occurred
	at com.exceptionHandling.Throw_demo2.throwException(Throw_demo2.java:11)
	at com.exceptionHandling.Throw_demo2.main(Throw_demo2.java:5)
Hello

Explanation:

ऊपर program 2 में, हमने try catch block का Use करके exception handle किया है इसलिए program का normal termination हुआ और System.out.println(“Hello”); यह code भी execute हुआ |

ध्यान दो, यहाँ हमने method throwException()  में try catch का use किया है |

Exception को या तो हम ,जिस method में occur हो उसमे handle करे या फिर caller method में भी कर सकते हैं |

Program 3:

public class Throw_demo3 {

	public static void main(String[] args) {

		try {
			throwException();
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println("Hello");
	}

	public static void throwException() {
		throw new ArithmeticException("exception occurred");
	}
}

OutPut:

java.lang.ArithmeticException: exception occurred
	at com.exceptionHandling.Throw_demo3.throwException(Throw_demo3.java:14)
	at com.exceptionHandling.Throw_demo3.main(Throw_demo3.java:6)
Hello

Explanation:

ऊपर program 3 में, हमने exception को caller method में handle किया है(यहाँ main() caller method है, क्योंकि उसने throwException() method को call किया है)

throw keyword के बाद कभी भी कोई statement नहीं लिखना चाहिए, क्योंकि वह execute ही नहीं होगा |

अगर ऐसा करते हैं तो program compile नहीं होगा और “Unreachable statement” का error occur होगा |

Program 4:

public class Throw_demo1 {

	public static void main(String[] args) {

		throwException();
	}

	public static void throwException() {
		throw new ArithmeticException("exception occurred");
		System.out.println("unreachable code,it will not execute");
	}
}

OutPut:

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
	Unreachable code

	at com.exceptionHandling.Throw_demo4.throwException(Throw_demo4.java:9)
	at com.exceptionHandling.Throw_demo4.main(Throw_demo4.java:5)

Explanation:

ऊपर program 4 में हमने System.out.println(“unreachable code,it will not execute”);

 throw के बाद लिखा है, जिससे यह statement execute नहीं हुआ और Unresolved compilation problem: Unreachable code मिला |

throw keyword को कभी भी predefined exception के case में नहीं use करना चाहिए |यह basically custom exception अथवा user define exception के case में ही Use किया जाता है |

Custom exception के बारे में हम आने वाले post में जानेंगे |

throws keyword in Java :

throws keyword का use उन exception को declare करने के लिए किया जाता है, जो program के execution के दौरान आ सकते हैं |

throws keyword हमेशा उस method signature के साथ Use होता है, जिसमें runtime पर exception आ सकते हैं |

जब भी किसी method के साथ throws की सहायता से यह declare किया जाता है कि इस method में यह यह exception आ सकता है तो उस case में, declared exception को handle करने की  जरुरत नहीं पड़ती | जो उस  method को call करेगा, या तो  वह उस exception को try catch का use करके handle करे या फिर वह भी throw का use करके अपने caller method को handle करने के लिए कहे, यह सिलसिला चलता रहता है और program का flow बना रहता है |

मैंने आप लोगो को थोड़ा confuse कर दिया, आइए सारी बातों को एक example के द्वारा समझे |

मान लो, हम किसी file को read करना चाहते हैं, तो हम FileInputStream का Use करेंगे लेकिन यह तो checked exception “FileNotFoundException”throw करता है , अब इस case में अगर मैं इसे handle नहीं करता अर्थात try catch का use नहीं करता तो मेरा code compile ही नहीं होगा और execution तो दूर की बात है|

मैं चाहता हूँ कि मेरा code compile हो लेकिन मैं exception handle न करू, बल्कि जो method इसे call करे वह ही exception handle करे अपने हिसाब से, इस case में मैं throws keyword Use करूँगा |

throws keyword एक indication provide करता है Caller method को, कि यह method एक exception throw करता है, जिसे उसने handle नहीं किया है और यह तुम्हे (caller method को) handle करना है |

अब caller method पर निर्भर करता है कि या तो वह उसे handle करे या वह भी अपने आगे throws लगा दे, जिससे उसका caller method उसे handle करे |इस तरह चलता रहता है |

throws keyword द्वारा multiple exception को declare किया जा सकता है, जिन्हे comma का Use करके लिखा जाता है |

Syntax :

returnType methodName (arguments) throws Exception1, Exception2, … {  }

Program 5:

import java.io.FileInputStream;

public class Throws_Demo1 {

	public static void main(String[] args) {

		FileInputStream fis = new FileInputStream("d:/abc.txt");
		System.out.println("file read");
	}
}

OutPut:

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
	Unhandled exception type FileNotFoundException

	at com.exceptionHandling.Throws_Demo1.main(Throws_Demo1.java:7)

Explanation:

ऊपर program 5 में, हमने FileInputStream का Use किया है जो कि checked exception FileNotFoundException throw करता है, यहाँ हमने exception को handle नहीं किया इसलिए, Unresolved compilation problem:

     Unhandled exception type FileNotFoundException  मिला |

अब यहाँ पर programmer के पास दो options है या तो वह try catch use करके exception handle करे या फिर throws keyword का Use करे |

Program 6:

import java.io.FileInputStream;
import java.io.FileNotFoundException;

public class Throws_Demo2 {

	public static void main(String[] args) throws FileNotFoundException {

		FileInputStream fis = new FileInputStream("d:/abc.txt");
		System.out.println("file read");
	}
}

OutPut:

file read

Explanation:

ऊपर program 6 में, हमने throws keyword का use करके checked Exception FileNotFoundException को declare किया जिससे, हमें कोई भी compile time exception नहीं मिला |

जब भी किसी method के साथ throws का use करके exception declare किया जाता है तो, caller method को us exception को handle करना होता है |

Program 7:

import java.io.FileInputStream;
import java.io.FileNotFoundException;

public class Throws_Demo3 {
	
	static void readfile() throws FileNotFoundException {
		FileInputStream fis = new FileInputStream("d:/abc.txt");
		System.out.println("file read");
	}

	public static void main(String[] args) {
		readfile();
	}
}

OutPut:

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
	Unhandled exception type FileNotFoundException

	at com.exceptionHandling.Throws_Demo3.main(Throws_Demo3.java:12)

Explanation:

ऊपर program 7 में, method readfile() ने throws keyword का use करके checked exception FileNotFoundException declare तो किया, परन्तु caller method main() ने उसे handle नहीं किया, इसलिए Unresolved compilation problem:

     Unhandled exception type FileNotFoundException  मिला |

Caller method main() के पास भी दो options हैं, या तो वह भी throws का use करे या फिर try..catch का use करके Exception handle करे |

Program 8:

import java.io.FileInputStream;
import java.io.FileNotFoundException;

public class Throws_Demo4 {

	static void readfile() throws FileNotFoundException {
		FileInputStream fis = new FileInputStream("d:/abc.txt");
		System.out.println("file read");
	}

	public static void main(String[] args) {
		try {
			readfile();
		} catch (FileNotFoundException e) {
			System.out.println("program is unable to locate Filepath, please correct the path");
		}
	}
}

OutPut:

file read

Explanation:

ऊपर program 8 में, caller method main() ने try..catch का use करके method readfile() के द्वारा declared exception को handle कर लिया |

throw और throws में अंतर:

throw keywordthrows keyword
इसका use programmer द्वारा manually exception object create करने के लिए किया जाता है |इसका use Exception को declare करने के लिए किया जाता है |throws keyword एक indication provide करता है Caller method को कि यह method एक exception throw करता है, जिसे उसने handle नहीं किया है और यह तुम्हे handle करना है |
इसका use मुख्यतः Unchecked अर्थात runtime Exception को throw करने के लिए किया जाता है |इसका use मुख्यतः checked exception को declare करने के लिए किया जाता है |
यह केवल एक समय पर एक ही exception throw कर सकता है |यह एक साथ multiple exception declare कर सकता है |
throw keyword method के अंदर या फिर किसी भी block {} में लिखा जा सकता है |इसे method signature के साथ लिखा जाता है |
throw keyword के बाद हमेशा new keyword होता है |throws keyword के बाद हमेशा ClassName आता है |
throw keyword के बाद, कोई और statement नहीं लिख सकते |throws के साथ ऐसा कुछ भी नहीं होता |

Conclusion – आज आपने क्या सीखा

इस post में आपने throw और throws keyword के बारे में विस्तृत जानकारी प्राप्त की |

आशा है कि, आपको मेरा यह Blog throw and throws keyword in Java in Hindi जरूर पसंद आया होगा |

अगर आप इस post से related कोई सवाल पूँछना चाहते हैं अथवा कोई सुझाव देना चाहते हैं तो comment करके जरूर बताएं, मैं उसका reply जरूर दूँगा |

इस post को अपना कीमती समय देने के लिए बहुत बहुत धन्यवाद् | फिर मिलेंगें |

Leave a Comment