← Back to Core Java Course | Chapter 14: Advanced Topics | Lesson 6 of 6

Java में Debugging

Debugging एक program में mistakes ढूंढना और fix करना है, clues follow करते एक detective जैसा। एक stack trace, printing, और एक debugger आपको दिखाते हैं कि चीज़ें कहां गलत हुईं।

Reading a Stack Trace

जब एक uncaught exception एक program crash करता है, Java एक stack trace print करता है: exception type, इसका message, और इसकी ओर ले जाने वाली method calls की chain, innermost-first listed।

हर 'at ClassName.method(File.java:N)' entry में line numbers बिल्कुल उस line तक point करते हैं जो fail हुई, और cause आमतौर पर एक JDK internal class के बजाय आपके अपने code से belong करने वाले top-most frame में है।

उदाहरण: Reading a Stack Trace

java
public class Main {
	static void level2() {
		throw new RuntimeException("Something broke");
	}
	static void level1() {
		level2();
	}
	public static void main(String[] args) {
		level1(); // uncaught: prints a stack trace, innermost call first
	}
}

Debugging with println

किसी method में key points पर System.out.println() calls छिड़कना एक debugger setup किए बिना variable values inspect करने का एक quick तरीका है, खासकर एक one-off check के लिए।

यह blunt है और source edit करने और फिर चलाने की मांग करता है, इसलिए यह subtle bugs track down करने के लिए अच्छी तरह scale नहीं करता, लेकिन यह कई developers के लिए सबसे तेज़ पहला step रहता है।

उदाहरण: Debugging with println

java
public class Main {
	public static void main(String[] args) {
		int x = 5;
		System.out.println("x = " + x); // quick inspection, no debugger needed
		x = x * 2;
		System.out.println("x after doubling = " + x);
	}
}

Using a Debugger and Breakpoints

एक असली debugger आपको execution को एक breakpoint पर pause करने और code modify किए बिना हर variable की live value inspect करने देता है, जो println debugging नहीं कर सकता।

Step Over current line चलाता है और उसी method में अगली पर move करता है, Step Into एक called method के अंदर execution follow करता है, और Step Out current method जल्दी खत्म करता है और आपको इसके caller पर वापस लाता है।

उदाहरण: Using a Debugger and Breakpoints

java
public class Main {
	public static void main(String[] args) {
		int total = 0;
		for (int i = 1; i <= 3; i++) {
			total += i; // a breakpoint here lets you inspect total live
		}
		System.out.println(total);
	}
}

Common IDE Debugger Features

Modern IDEs conditional breakpoints जोड़ते हैं (सिर्फ तब pause करें जब एक expression true हो, हज़ारों बार चलने वाले एक loop के अंदर उपयोगी), watch expressions जो पूरी session में एक variable की value track करते हैं, और pause के बीच source edit किए बिना एक expression test करने के लिए एक interactive evaluation console।

ये features debugging को guesswork से एक controlled inspection process में बदल देते हैं।

उदाहरण: Common IDE Debugger Features

java
public class Main {
	public static void main(String[] args) {
		for (int i = 0; i < 1000; i++) {
			if (i == 500) { // conditional breakpoint would pause only here
				System.out.println("Reached 500");
			}
		}
	}
}

Compile Errors vs Runtime Exceptions vs Logic Bugs

एक compile-time error का मतलब है code कभी चला भी नहीं (एक missing semicolon, गलत type, unresolved symbol) और compiler कुछ भी execute होने से पहले इसे report करता है। एक runtime exception का मतलब है code सही compile हुआ लेकिन चलते समय कुछ गलत हो गया (zero से divide करना, एक null reference) और Java इसे एक stack trace के ज़रिए report करता है।

एक logic bug तीनों में सबसे मुश्किल है: program बिना किसी error के compile और चलता है, यह बस चुपचाप गलत answer produce करता है, यही कारण है कि deliberate debugging technique यहां सबसे ज़्यादा मायने रखती है।

उदाहरण: Compile Errors vs Runtime Exceptions vs Logic Bugs

java
public class Main {
	public static void main(String[] args) {
		int[] arr = {1, 2, 3};
		try {
			System.out.println(arr[5]); // runtime exception, not a compile error
		} catch (ArrayIndexOutOfBoundsException e) {
			System.out.println("Runtime exception caught");
		}
		System.out.println(2 + 2); // if this printed 5, that'd be a logic bug
	}
}
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. Stack trace नज़रअंदाज़ करना, जब top lines exception और यह कहां हुआ दिखाती हैं।
  2. Finished code में System.out.println debug lines छोड़ना।
  3. Compile-time errors को runtime exceptions के साथ confuse करना, जब वे अलग-अलग समय पर मिलती हैं।
चैप्टर सारांश
  • Wrapper classes और type casting primitive types और objects या दूसरे types के बीच move करते हैं।
  • Math operations, advanced bitwise operations, और packages और imports code organize और extend करते हैं।
  • Debugging आपको problems ढूंढने और fix करने में मदद करता है।
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

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.