← Back to Core Java Course | Chapter 10: Exception Handling | Lesson 5 of 8

Java में throw और throws

throw वह है जिससे आप खुद एक alarm raise करते हैं जब कुछ गलत हो, और throws एक method पर एक warning है जो कहती है यह शायद एक raise कर सकता है। साथ में वे problems को जो handle कर सके उसे पास करते हैं।
Syntax
java
throw new ExceptionType("message");

returnType methodName() throws ExceptionType {
    // body
}

The throw Keyword

throw एक statement है जो आप इसे जिस बिल्कुल point पर लिखें वहां एक specific exception instance असल में raise करता है, तुरंत normal execution रोकते हुए और एक handler search करते हुए।

उदाहरण: The throw Keyword

java
public class Main {
	public static void main(String[] args) {
		try {
			throw new IllegalArgumentException("Invalid input"); // raises it right here
		} catch (IllegalArgumentException e) {
			System.out.println(e.getMessage());
		}
	}
}

The throws Keyword

throws एक method-signature declaration है, एक statement नहीं — यह callers को warn करता है कि यह method एक दिए checked exception को propagate कर सकता है, इसलिए compiler enforce कर सकता है कि वे इसे handle या re-declare करें।

उदाहरण: The throws Keyword

java
import java.io.IOException;
public class Main {
	static void readFile() throws IOException { // warns callers, doesn't throw yet
		throw new IOException("File error");
	}
	public static void main(String[] args) {
		try {
			readFile();
		} catch (IOException e) {
			System.out.println(e.getMessage());
		}
	}
}

throw vs throws

throw एक method body के अंदर अभी एक exception trigger करता है; throws बस method के signature में document करता है कि इससे कौन से exceptions आ सकते हैं। दोनों को confuse करना एक आम beginner mistake है क्योंकि वे इतने similar दिखते हैं।

उदाहरण: throw vs throws

java
public class Main {
	static void risky() throws Exception { // throws: documents in the signature
		throw new Exception("Something failed"); // throw: raises it now
	}
	public static void main(String[] args) throws Exception {
		risky();
	}
}

Exception Propagation

जब एक method ऐसा exception catch नहीं करता जो वह throw कर सकता है, वह exception call stack में जिसने इसे call किया उस तक propagate करता है, और फिर उनके caller तक, जब तक कुछ इसे catch न करे या program एक printed stack trace के साथ terminate न हो।

उदाहरण: Exception Propagation

java
public class Main {
	static void level1() {
		level2();
	}
	static void level2() {
		throw new RuntimeException("Error at level2");
	}
	public static void main(String[] args) {
		try {
			level1(); // propagates up from level2 through level1
		} catch (RuntimeException e) {
			System.out.println("Caught: " + e.getMessage());
		}
	}
}

Best Practices with throw and throws

सिर्फ उन exceptions के लिए throws declare करें जिनके बारे में एक caller को असल में जानने की ज़रूरत है और meaningfully react कर सकता है; plain Exception जैसे overly generic exceptions throw करना हर caller को broad, कम उपयोगी catch blocks में मजबूर करता है।

उदाहरण: Best Practices with throw and throws

java
class InvalidAgeException extends RuntimeException { // specific, meaningful exception
	InvalidAgeException(String msg) { super(msg); }
}
public class Main {
	static void setAge(int age) {
		if (age < 0) {
			throw new InvalidAgeException("Age cannot be negative");
		}
	}
	public static void main(String[] args) {
		try {
			setAge(-5);
		} catch (InvalidAgeException e) {
			System.out.println(e.getMessage());
		}
	}
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. throw (एक exception throw करता है) को throws (इसे declare करता है) के साथ confuse करना।
  2. एक checked exception के लिए throws भूल जाना, जो एक compile error है।
  3. एक class के बजाय एक object के साथ throw उपयोग करना, जैसे throw Exception, जो throw new Exception() होना चाहिए।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.