Java में throw और throws
In this page:
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
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());
}
}
}
Login to try C/C++/Java/PHP code in the editor
The throws Keyword
throws एक method-signature declaration है, एक statement नहीं — यह callers को warn करता है कि यह method एक दिए checked exception को propagate कर सकता है, इसलिए compiler enforce कर सकता है कि वे इसे handle या re-declare करें।
उदाहरण: The throws Keyword
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());
}
}
}
Login to try C/C++/Java/PHP code in the editor
throw vs throws
throw एक method body के अंदर अभी एक exception trigger करता है; throws बस method के signature में document करता है कि इससे कौन से exceptions आ सकते हैं। दोनों को confuse करना एक आम beginner mistake है क्योंकि वे इतने similar दिखते हैं।
उदाहरण: throw vs throws
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();
}
}
Login to try C/C++/Java/PHP code in the editor
Exception Propagation
जब एक method ऐसा exception catch नहीं करता जो वह throw कर सकता है, वह exception call stack में जिसने इसे call किया उस तक propagate करता है, और फिर उनके caller तक, जब तक कुछ इसे catch न करे या program एक printed stack trace के साथ terminate न हो।
उदाहरण: Exception Propagation
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());
}
}
}
Login to try C/C++/Java/PHP code in the editor
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
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());
}
}
}
Login to try C/C++/Java/PHP code in the editor
throw(एक exception throw करता है) कोthrows(इसे declare करता है) के साथ confuse करना।- एक checked exception के लिए
throwsभूल जाना, जो एक compile error है। - एक class के बजाय एक object के साथ
throwउपयोग करना, जैसेthrow Exception, जोthrow new Exception()होना चाहिए।
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: