Types of Errors in Java (जावा में एरर के प्रकार)
जावा में एरर वे समस्याएँ होती हैं जो प्रोग्राम को कम्पाइल या रन करते समय उत्पन्न होती हैं। इन त्रुटियों को समझना प्रोग्रामिंग में सुधार और डिबगिंग के लिए आवश्यक है।
🧩 a) Syntax Errors (सिंटेक्स एरर)
Syntax errors occur when the programmer does not follow the correct structure or rules of the Java language. These errors are detected during compilation time, hence also called compile-time errors.
सिंटेक्स एरर तब उत्पन्न होती हैं जब प्रोग्रामर जावा की नियमबद्ध संरचना का सही पालन नहीं करता है। ये त्रुटियाँ कम्पाइल के समय पता चलती हैं, इसलिए इन्हें कम्पाइल-टाइम एरर भी कहते हैं।
✅ Example:
// Missing semicolon
public class HelloWorld {public static void main(String[] args) {System.out.println("Hello, World!") // ← semicolon missing}}
❌ Output:
HelloWorld.java:3: error: ';' expected
System.out.println("Hello, World!")^
🧠 b) Logical Errors (लॉजिकल एरर)
Logical errors occur when the program compiles and runs without crashing, but gives incorrect output due to faulty logic. These are the most difficult to detect because there is no error message.
लॉजिकल एरर तब होती हैं जब प्रोग्राम बिना किसी एरर के रन तो करता है, लेकिन आउटपुट गलत आता है। ये एरर सबसे मुश्किल होती हैं क्योंकि कोई एरर मैसेज नहीं मिलता।
✅ Example:
// Infinite loop due to faulty logic
public class InfiniteLoopExample {public static void main(String[] args) {for(int i = 1; true; i++) {System.out.println("Value of i: " + i);}}}
❌ Problem:
The loop will never terminate, creating an infinite loop.
⚠️ c) Runtime Errors (रन टाइम एरर) or Exceptions (एक्सेप्शन)
These errors occur while the program is running, not during compilation. They are often caused by illegal operations such as dividing by zero, accessing null objects, or file not found issues.
रन टाइम एरर वो त्रुटियाँ हैं जो प्रोग्राम के रन करते समय उत्पन्न होती हैं। ये एक्सेप्शन कहलाती हैं और आमतौर पर अवैध ऑपरेशन के कारण होती हैं जैसे - ज़ीरो से डिवाइड करना या null को एक्सेस करना।
Technically, errors and exceptions are different in Java:
-
Errors like
OutOfMemoryError
are serious issues and can't be handled easily. -
Exceptions like
ArithmeticException
orNullPointerException
can be caught using try-catch blocks.
तकनीकी रूप से Error और Exception अलग-अलग होते हैं:
-
Error (जैसे OutOfMemoryError) गंभीर होती हैं और इन्हें हैंडल करना कठिन होता है।
-
Exception (जैसे ArithmeticException) को try-catch से संभाला जा सकता है।
✅ Example:
// Division by zero
public class RuntimeErrorExample {public static void main(String[] args) {int a = 10;int b = 0;System.out.println(a / b); // Runtime Error: ArithmeticException}}
❌ Output:
Exception in thread "main" java.lang.ArithmeticException: / by zero
🛡 Handling Runtime Exceptions (रन टाइम एक्सेप्शन को हैंडल करना)
You can handle runtime exceptions using try-catch blocks to avoid program crashes.
आप try-catch ब्लॉक का उपयोग करके रन टाइम एक्सेप्शन को संभाल सकते हैं और प्रोग्राम को क्रैश होने से बचा सकते हैं।
✅ Example with try-catch:
public class HandleException {
public static void main(String[] args) {try {int a = 10, b = 0;System.out.println(a / b);} catch (ArithmeticException e) {System.out.println("Can't divide by zero!");}}}
✅ Output:
Can't divide by zero!
🔚 Conclusion (निष्कर्ष)
Understanding the types of errors in Java helps developers write better, bug-free programs. Always check for syntax, plan logic carefully, and handle exceptions gracefully.
जावा में एरर के प्रकारों की समझ से प्रोग्रामिंग में गुणवत्ता आती है। सिंटेक्स की जांच करें, लॉजिक सही बनाएँ और एक्सेप्शन को सावधानीपूर्वक हैंडल करें।
Comments
Post a Comment