JavaJava · Chapterبەش 3 · Lectureوانە 3.4
Errors and Debugging: Syntax, Runtime and Logic Errorsهەڵەکان و کرمەکوژی: هەڵەی داڕستن، هەڵەی کاتی جێبەجێکردن و هەڵەی لۆجیکی
The three kinds of Java error and who finds them: javac (syntax errors), the JVM (runtime errors and their stack traces) and you (logic errors). Reading javac messages and stack traces, why 1 / 0 crashes but 1.0 / 0 gives Infinity, testing with known answers, copy-paste traps, and debugging with print lines and a debugger.
سێ جۆری هەڵە لە Javaـدا و ئەوەی هەریەکەیان دەدۆزێتەوە: javac ()، (، لەگەڵ ) و خۆت (). خوێندنەوەی پەیامەکانی javac و ، بۆچی 1 / 0 دەوەستێنێت بەڵام 1.0 / 0 Infinity دەدات، تاقیکردنەوە بە وەڵامی زانراو، داوەکانی کۆپیکردن و بە دێڕی چاپکردن و ئامرازی .
What you will learnچی فێر دەبیت
- Tell syntax, runtime and logic errors apart, and say who finds each one: javac, the JVM or you.
- Read a javac error message and a Java stack trace: file, line, message and the
^. - Explain why
1 / 0stops a program, why1.0 / 0givesInfinityand why9 / 5is 1. - Find bugs with known-answer tests, print lines and a debugger, one change at a time.
- ، و لە یەک جیا بکەیتەوە و بڵێیت کێ هەریەکەیان دەدۆزێتەوە: javac، یان خۆت.
- javac و Java بخوێنیتەوە: ، دێڕ، پەیام و
^. - ڕوونی بکەیتەوە بۆچی
1 / 0ڕادەگرێت، بۆچی1.0 / 0Infinityدەدات و بۆچی9 / 5دەکاتە 1. - بە تاقیکردنەوەی وەڵامزانراو، دێڕی چاپکردن و ئامرازی بدۆزیتەوە، هەر جارە یەک گۆڕانکاری.
Three kinds of errorسێ جۆری هەڵە
Everybody writes code that does not work the first time. A mistake in a program is called a , and finding and fixing it is (the story of these words is in Why mistakes are called bugs). Java errors come in three kinds, and the best clue is who finds the error:
هەموو کەسێک کۆد دەنووسێت کە لە یەکەم هەوڵدا کار ناکات. هەڵەیەک لە پێی دەگوترێت و دۆزینەوە و چاککردنی پێی دەگوترێت (چیرۆکی ئەم دوو وشەیە لە بۆچی هەڵەکان پێیان دەوترێت باگ؟دایە). هەڵەکانی Java سێ جۆرن و باشترین ڕێنیشاندەر ئەوەیە کە کێ هەڵەکە دەدۆزێتەوە:
- Syntax errors (compile errors):
javacfinds them before the program runs. - Runtime errors: the JVM stops the program while it runs.
- Logic errors: the program runs to the end, but the answer is wrong. Only you can notice them.
- ():
javacپێش جێبەجێبوونی دەیاندۆزێتەوە. - : لە کاتی کارکردندا ڕادەگرێت.
- : تا کۆتایی کار دەکات، بەڵام وەڵامەکە هەڵەیە. تەنیا خۆت دەتوانیت هەستیان پێ بکەیت.
Syntax errors: javac finds themهەڵەکانی داڕستن: javac دەیاندۆزێتەوە
Syntax errors, also called compile errors, break the grammar of Java, its . The compiler, javac, finds them while it translates your code, so the program never starts. They come from mistakes in how the code is built: a mistyped keyword, missing punctuation such as ; or a closing ", or an opening brace { without its closing }. They are usually the easiest errors to fix, because javac tells you where the problem is and what it is.
، کە پێیان دەگوترێت ، یاساکانی ڕێزمانی Java دەشکێنن، واتە . ، واتە javac، لە کاتی وەرگێڕانی کۆدەکەتدا دەیاندۆزێتەوە، بۆیە هەرگیز دەست پێ ناکات. لە هەڵەی دروستکردنی کۆدەوە دێن: هەڵە نووسراو، نیشانەیەکی کەم وەک ; یان "ـی داخستن، یان کەوانەیەکی { کە }ـی داخستنی نییە. زۆرجار ئاسانترین هەڵەکانن بۆ چاککردن، چونکە javac پێت دەڵێت کێشەکە لە کوێیە و چییە.
Syntax errors: javac finds themهەڵەکانی داڕستن: javac دەیاندۆزێتەوە
Reading a javac messageخوێندنەوەی پەیامێکی javac
This program has two syntax errors. Can you see them before you read javac's answer?
ئەم دوو تێدایە. دەتوانیت پێش خوێندنەوەی وەڵامی javac بیانبینیت؟
public class ShowSyntaxErrors {
public static main(String[] args) {
System.out.println("Welcome to Java);
}
}$ javac ShowSyntaxErrors.java
ShowSyntaxErrors.java:2: error: invalid method declaration; return type required
public static main(String[] args) {
^
ShowSyntaxErrors.java:3: error: unclosed string literal
System.out.println("Welcome to Java);
^
2 errorsEvery javac message has the same parts: the file name and the line number, then error: and what is wrong, a copy of the line, and a ^ under the place where javac noticed the problem. The last line counts the errors. Line 2 needs void before main, because every method must say what it gives back (main gives back nothing). Line 3 has no closing quote, so the string never ends. With both fixed, the program prints Welcome to Java.
هەموو پەیامێکی javac هەمان بەشەکانی هەیە: ناوی و ژمارەی دێڕەکە، پاشان error: و ئەوەی هەڵەیە، کۆپییەکی دێڕەکە و نیشانەی ^ لە ژێر ئەو شوێنەی javac کێشەکەی تێدا بینی. دوایین دێڕ هەڵەکان دەژمێرێت. دێڕی 2 پێش main پێویستی بە voidـە، چونکە هەموو دەبێت بڵێت چی دەگەڕێنێتەوە (main هیچ ناگەڕێنێتەوە). دێڕی 3 داخستنی نییە، بۆیە هەرگیز کۆتایی نایەت. کە هەردووکیان چاک بکرێن، Welcome to Java چاپ دەکات.
Syntax errors: javac finds themهەڵەکانی داڕستن: javac دەیاندۆزێتەوە
Fix the first error firstسەرەتا یەکەم هەڵە چاک بکە
One mistake can produce several messages, and the later ones are often side effects of the first. So fix the first error, compile again and repeat: fixing an early error often makes later ones disappear. Here only the { after main(…) is missing, but javac reports two errors, and the first even asks for a ;:
یەک هەڵە دەتوانێت چەند پەیامێک دروست بکات و پەیامەکانی دواتر زۆرجار شوێنەواری یەکەمیانن. بۆیە یەکەم هەڵە چاک بکە، دووپاتە کۆمپایل بکەرەوە و دووپاتەی بکەرەوە: چاککردنی هەڵەیەکی سەرەتا زۆرجار هەڵەکانی دواتریش لادەبات. لێرەدا تەنیا {ـی دوای main(…) کەمە، بەڵام javac دوو هەڵە ڕادەگەیەنێت و یەکەمیان تەنانەت داوای ; دەکات:
public class Hello {
public static void main(String[] args)
System.out.println("Silav!");
}
}$ javac Hello.java
Hello.java:2: error: ';' expected
public static void main(String[] args)
^
Hello.java:5: error: class, interface, enum, or record expected
}
^
2 errorsAdding that one { removes both messages. The animation fixes ShowSyntaxErrors the same way, one error at a time:
زیادکردنی ئەو یەک {ـە هەردوو پەیامەکە لادەبات. ئەنیمەیشنەکە ShowSyntaxErrors بە هەمان شێوە چاک دەکات، هەر جارە یەک هەڵە:


Runtime errors: the JVM stops the programهەڵەکانی کاتی جێبەجێکردن: ئامێری گریمانەیی Java بەرنامەکە ڕادەگرێت
A makes a program stop in the middle: it terminates abnormally. The compiler cannot see it, because the grammar is fine. The JVM finds it while the program runs, when it meets an operation that is impossible to carry out. It then throws an , an object that names the problem, and the program stops. The classic example is dividing a whole number by zero:
وا دەکات لە ناوەڕاستدا بوەستێت: بە شێوەیەکی نائاسایی کۆتایی دێت. ناتوانێت بیبینێت، چونکە ڕێزمانەکە دروستە. لە کاتی کارکردنی دەیدۆزێتەوە، کاتێک تووشی کارێک دەبێت کە ئەنجامدانی مەحاڵە. ئەوسا فڕێ دەدات، کە ناوی کێشەکە دەڵێت. پاشان دەوەستێت. نموونە بەناوبانگەکە دابەشکردنی بەسەر سفردایە:
public class ShowRuntimeErrors {
public static void main(String[] args) {
System.out.println(1 / 0);
}
}$ javac ShowRuntimeErrors.java $ java ShowRuntimeErrors Exception in thread "main" java.lang.ArithmeticException: / by zero at ShowRuntimeErrors.main(ShowRuntimeErrors.java:3)
javac compiled it without a word; the JVM stopped it with ArithmeticException: / by zero and line 3. Lines before the bad one have already run, and nothing after it runs:
javac بێ هیچ قسەیەک کۆمپایلی کرد؛ بە ArithmeticException: / by zero و دێڕی 3 ڕایگرت. ئەو دێڕانەی پێش دێڕە هەڵەکە بوون جێبەجێ بوون و هیچ شتێکی دوای ئەو جێبەجێ نابێت:
A program can also catch an exception and carry on instead of stopping: Exceptions shows how.
دەتوانێت بگرێت و لە جیاتی وەستان بەردەوام بێت: هەڵاوێردەکان پیشانت دەدات چۆن.
Runtime errors: the JVM stops the programهەڵەکانی کاتی جێبەجێکردن: ئامێری گریمانەیی Java بەرنامەکە ڕادەگرێت
Reading a stack traceخوێندنەوەی ڕێچکەیەکی هەڵە
The message of a runtime error is a , in Java usually called a stack trace. Its first line says what went wrong: the exception's name and a short message. Each at line names a method that was running, with its file and line, newest first. So the top at line is where the error happened, and the line under it is where that method was called. (Methods come later in this track; here, just follow the line numbers. A method that calls itself without end makes a very long stack trace: Recursion.)
پەیامی ، کە لە Javaـدا زۆرجار پێی دەگوترێت stack trace. یەکەم دێڕەکەی دەڵێت چی هەڵە بوو: ناوی و پەیامێکی کورت. هەر دێڕێکی at ناوی دەهێنێت کە کاری دەکرد، لەگەڵ و دێڕەکەی، نوێترینیان لە سەرەوە. کەواتە یەکەم دێڕی at ئەو شوێنەیە کە هەڵەکەی تێدا ڕوو دا و دێڕەکەی ژێری ئەو شوێنەیە کە ئەو لێوە بانگ کرا. ( دواتر لەم ڕێڕەوەدا دێن؛ لێرەدا تەنیا بەدوای ژمارەی دێڕەکاندا بڕۆ. کە بێ کۆتایی خۆی بانگ دەکات زۆر درێژ دروست دەکات: خۆبانگکردنەوە.)
public class Marks {
static int average(int total, int count) {
return total / count;
}
public static void main(String[] args) {
System.out.println("Average of 3 marks: " + average(240, 3));
System.out.println("Average of 0 marks: " + average(0, 0));
System.out.println("Done");
}
}$ java Marks Average of 3 marks: 80 Exception in thread "main" java.lang.ArithmeticException: / by zero at Marks.average(Marks.java:3) at Marks.main(Marks.java:8)
Runtime errors: the JVM stops the programهەڵەکانی کاتی جێبەجێکردن: ئامێری گریمانەیی Java بەرنامەکە ڕادەگرێت
Dividing a double by zero: Infinity and NaNدابەشکردنی double بەسەر سفردا: Infinity و NaN
Only whole-number division by zero stops a Java program. A double (a number with a decimal point, next chapter) follows the IEEE 754 rules for floating-point numbers: the answer is a special value and the program carries on:
تەنیا دابەشکردنی بەسەر سفردا Java ڕادەگرێت. double (، بەشی داهاتوو) یاساکانی IEEE 754 بۆ پەیڕەو دەکات: وەڵامەکە تایبەتە و بەردەوام دەبێت:
public class Divide {
public static void main(String[] args) {
System.out.println(1.0 / 0); // a double divided by zero
System.out.println(-1.0 / 0);
System.out.println(0.0 / 0);
System.out.println(Math.sqrt(-1));
System.out.println("Still running");
}
}$ java Divide Infinity -Infinity NaN NaN Still running
Infinity and (not a number) do not stop anything, but they are almost always a bug, and they spread into later results. A bill of 1500.0 dinars shared by 0 friends prints Each friend pays Infinity. Sensor programs meet NaN too: an Arduino gets NaN when a temperature sensor gives no reading (NaN on a DHT11).
Infinity و (ژمارە نییە) هیچ شتێک ڕاناگرن، بەڵام نزیکەی هەمیشە و بڵاو دەبنەوە بۆ ناو ئەنجامەکانی دواتر. پسوولەیەکی 1500.0 دینار کە بەسەر 0 هاوڕێدا دابەش بکرێت چاپ دەکات: Each friend pays Infinity. تووشی NaN دەبن: کاتێک پلەی گەرمی هیچ خوێندنەوەیەک نادات NaN وەردەگرێت (NaN لە DHT11ـدا).
Runtime errors: the JVM stops the programهەڵەکانی کاتی جێبەجێکردن: ئامێری گریمانەیی Java بەرنامەکە ڕادەگرێت
Input errorsهەڵەکانی نێوخستە
Input mistakes are a common cause of runtime errors. The program waits for the user to type a value, and the user types something it cannot handle, for example a word where a number is expected. This program asks for a radius (reading the keyboard is taught in Input and output). The user typed abc:
هەڵەی هۆکارێکی باوی . چاوەڕێ دەکات بەکارهێنەر بنووسێت و بەکارهێنەر شتێک دەنووسێت کە ناتوانێت مامەڵەی لەگەڵدا بکات، بۆ نموونە وشەیەک لە شوێنی ژمارەیەکدا. ئەم داوای نیوەتیرەیەک دەکات (خوێندنەوەی لە نێوخستە و دەرخستەدا دەخوێنیت). بەکارهێنەر abcـی نووسی:
import java.util.Scanner;
public class Area {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Radius: ");
double radius = input.nextDouble();
System.out.println("Area: " + radius * radius * 3.14159);
}
}$ java Area Radius: abc Exception in thread "main" java.util.InputMismatchException at java.base/java.util.Scanner.throwFor(Scanner.java:939) at java.base/java.util.Scanner.next(Scanner.java:1594) at java.base/java.util.Scanner.nextDouble(Scanner.java:2564) at Area.main(Area.java:7)
With the input 2.5 the same program prints Area: 19.6349375. A program should not trust its input: Wrong input explains this error, and Exceptions shows how to catch it and ask again.
بە 2.5 هەمان Area: 19.6349375 چاپ دەکات. نابێت متمانە بە بکات: نێوخستەی هەڵە ئەم هەڵەیە ڕوون دەکاتەوە و هەڵاوێردەکان پیشانت دەدات چۆن بیگریت و دووپاتە داوا بکەیتەوە.
Logic errors: a wrong answer and no messageهەڵە لۆجیکییەکان: وەڵامی هەڵە و هیچ پەیامێک
A means that the program does not do what you intended. Logic errors have many causes. The program compiles, runs to the end and prints no message: the answer is simply wrong. This program should convert 35 degrees Celsius to Fahrenheit:
واتە ئەوە ناکات کە مەبەستت بوو. زۆر هۆکاریان هەیە. کۆمپایل دەبێت، تا کۆتایی کار دەکات و هیچ پەیامێک چاپ ناکات: تەنیا وەڵامەکە هەڵەیە. ئەم دەبێت 35 پلەی سیلیزی بگۆڕێت بۆ فەهرەنهایت:
public class ShowLogicErrors {
public static void main(String[] args) {
System.out.println("Celsius 35 is Fahrenheit degree ");
System.out.println((9 / 5) * 35 + 32);
}
}$ java ShowLogicErrors Celsius 35 is Fahrenheit degree 67
The right answer is 95 °F, but the program printed 67.
وەڵامی ڕاست 95 °Fـە، بەڵام 67ی چاپ کرد.
Logic errors: a wrong answer and no messageهەڵە لۆجیکییەکان: وەڵامی هەڵە و هیچ پەیامێک
Finding the bug: 9 / 5 is 1دۆزینەوەی باگەکە: 9 / 5 دەکاتە 1
The bug is 9 / 5. Both numbers are int, so Java does : it keeps the whole part and throws the fraction away. 9 / 5 is 1, not 1.8, and the program computes 1 × 35 + 32 = 67. Make one number a double, 9.0, and the fraction stays (division is taught in full in Operators):
9 / 5ـە. هەردوو ژمارەکە intن، بۆیە Java دەکات: بەشە تەواوەکە دەهێڵێتەوە و فڕێ دەدات. 9 / 5 دەکاتە 1 نەک 1.8 و 1 × 35 + 32 = 67 دەژمێرێت. یەکێک لە ژمارەکان بکە بە double (9.0) و دەمێنێتەوە (دابەشکردن بە تەواوی لە کارپێکەرەکاندا دەخوێنیت):
public class ShowLogicErrors {
public static void main(String[] args) {
System.out.println("Celsius 35 is Fahrenheit degree ");
System.out.println((9.0 / 5) * 35 + 32);
}
}$ java ShowLogicErrors Celsius 35 is Fahrenheit degree 95.0
Logic errors: a wrong answer and no messageهەڵە لۆجیکییەکان: وەڵامی هەڵە و هیچ پەیامێک
Test with answers you knowبە وەڵامی زانراو تاقی بکەرەوە
javac and the JVM cannot see logic errors, so you must test: run the program with inputs whose right answer you already know, and compare. Good tests for a temperature converter are 0 °C (32 °F), 100 °C (212 °F) and −40 °C (−40 °F). One test is not enough:
javac و ناتوانن ببینن، بۆیە دەبێت تاقی بکەیتەوە: لەگەڵ چەند جێبەجێ بکە کە وەڵامە ڕاستەکانیان پێشتر دەزانیت و بەراوردیان بکە. تاقیکردنەوەی باش بۆ گۆڕەرێکی پلەی گەرمی ئەمانەن: 0 °C (32 °F)، 100 °C (212 °F) و −40 °C (−40 °F). یەک تاقیکردنەوە بەس نییە:
| Testتاقیکردنەوە | Right answerوەڵامی ڕاست | (9 / 5) * c + 32 | (9.0 / 5) * c + 32 |
|---|---|---|---|
| 0 °C | 32 °F | 32 | 32.0 |
| 100 °C | 212 °F | 132 | 212.0 |
| −40 °C | −40 °F | -8 | -40.0 |
With only the 0 °C test the bug would hide, because the wrong version also gives 32. Choose easy values, zero, negative numbers and the smallest and largest cases.
ئەگەر تەنیا 0 °C تاقی بکەیتەوە دەشاردرێتەوە، چونکە وەشانە هەڵەکەش 32 دەدات. ئاسان، سفر، ژمارەی نەرێنی و بچووکترین و گەورەترین حاڵەتەکان هەڵبژێرە.
Copy-paste trapsداوەکانی کۆپیکردن
Code copied from a PDF, a , a web page or a screenshot can hold characters that look like Java's but are different characters. javac does not recognise them and names each one by its number:
ئەو کۆدەی لە PDF، ، یان کۆپی دەکرێت دەتوانێت تێدا بێت کە وەک هی Java دەردەکەون بەڵام جیاوازی . javac نایانناسێتەوە و هەریەکەیان بە ژمارەکەی ناو دەبات:
The last row is a trap of IDE screenshots: editors such as IntelliJ IDEA show the names of a method's parameters in grey (a:, b:), but they are not part of the code. Type Math.max(10, -20); it prints 10, the larger number (The Math class has max and more).
دوایین ڕیز داوێکی IDEـیە: دەستکاریکەری وەک IntelliJ IDEA ناوی بە خۆڵەمێشی پیشان دەدات (a:، b:)، بەڵام بەشێک نین لە کۆدەکە. Math.max(10, -20) بنووسە؛ 10 چاپ دەکات، کە ژمارە گەورەترەکەیە (کلاسەکەی Math max و زیاتری تێدایە).
Debugging: from symptom to causeکرمەکوژی: لە نیشانەوە بۆ هۆکار
Debugging is detective work. Do not guess and change things at random; go round a cycle until the program is right:
کاری لێکۆڵەرەوەیە. مەزەندە مەکە و شتەکان بە هەڕەمەکی مەگۆڕە؛ بە خولێکدا بسوڕێوە تا ڕاست دەبێت:
Read the whole message: the error type, the file and the line.
هەموو پەیامەکە بخوێنەوە: جۆری هەڵەکە، و دێڕەکە.
Make the problem small: find the simplest input that still shows the bug.
کێشەکە بچووک بکەرەوە: سادەترین بدۆزەرەوە کە هێشتا پیشان دەدات.
Look at the values: print them, or step through the code with a debugger (next screens).
سەیری بکە: چاپیان بکە، یان بە ئامرازی هەنگاو بە هەنگاو بە کۆدەکەدا بڕۆ (شاشەکانی داهاتوو).
Change one thing at a time, then compile and run again.
هەر جارە یەک شت بگۆڕە، پاشان دووپاتە کۆمپایل و جێبەجێی بکەرەوە.
Explain your code aloud, line by line, to a classmate or even to a cup of tea. Saying each step often reveals the mistake; programmers call it rubber-duck debugging.
کۆدەکەت دێڕ بە دێڕ بە دەنگی بەرز ڕوون بکەرەوە بۆ هاوپۆلێک یان تەنانەت بۆ پیاڵەیەک چا. گوتنی هەر هەنگاوێک زۆرجار هەڵەکە ئاشکرا دەکات؛ پێی دەڵێن rubber-duck debugging.
Debugging: from symptom to causeکرمەکوژی: لە نیشانەوە بۆ هۆکار
Print the valuesنرخەکان چاپ بکە
The quickest tool is a temporary System.out.println. This program should print the average of 70, 80 and 90, which is 80.0, but it prints Average: 50.0. One extra line shows what total really holds:
خێراترین ئامراز System.out.printlnـێکی کاتییە. ئەم دەبێت تێکڕای 70، 80 و 90 چاپ بکات، کە 80.0ـە، بەڵام Average: 50.0 چاپ دەکات. یەک دێڕی زیادە پیشان دەدات total بەڕاستی چی هەڵگرتووە:
public class Average {
public static void main(String[] args) {
int a = 70;
int b = 80;
int c = 90;
int total = a + b;
System.out.println("DEBUG total = " + total); // temporary line
double average = total / 3.0;
System.out.println("Average: " + average);
}
}$ java Average DEBUG total = 150 Average: 50.0
total is 150, not 240: line 6 forgot c. Change it to a + b + c, run again (Average: 80.0) and delete the DEBUG line. A trace table does the same job on paper.
total 150ـە نەک 240: دێڕی 6 cـی لە بیر چووە. بیگۆڕە بۆ a + b + c، دووپاتە جێبەجێی بکەرەوە (Average: 80.0) و دێڕی DEBUG بسڕەوە. خشتەی بەدواداچوون هەمان کار لەسەر کاغەز دەکات.
Debugging: from symptom to causeکرمەکوژی: لە نیشانەوە بۆ هۆکار
A debugger: one line at a timeئامرازی کرمەکوژی: هەر جارە یەک دێڕ
Every Java IDE (VS Code, IntelliJ IDEA, Eclipse, NetBeans) has a debugger. Click in the margin next to a line to set a , a red dot, and start the program with Debug instead of Run. It pauses just before that line, and a Variables panel shows the value of every variable. Step Over runs one line and pauses again; Step Into goes inside a method that the line calls; Resume runs on to the next breakpoint. The keys differ from one editor to another, but the buttons have these names.
هەموو IDEـێکی Java (VS Code، IntelliJ IDEA، Eclipse، NetBeans) ئامرازی تێدایە. لە پەراوێزی تەنیشت دێڕێک کلیک بکە بۆ دانانی (خاڵێکی سوور) و بە Debug دەست پێ بکە لە جیاتی Run. ڕێک پێش ئەو دێڕە ڕادەوەستێت و بەشێکی Variables هەموو پیشان دەدات. Step Over یەک دێڕ جێبەجێ دەکات و دووپاتە ڕادەوەستێت؛ Step Into دەچێتە ناو ئەو دێڕەکە بانگی دەکات؛ Resume بەردەوام دەبێت تا تر. کلیلەکان لە دەستکاریکەرێکەوە بۆ یەکێکی تر جیاوازن، بەڵام دوگمەکان ئەم ناوانەیان هەیە.


Common mistakesهەڵە باوەکان
| Mistakeهەڵە | Betterباشتر |
|---|---|
| Fixing the last javac message firstچاککردنی دوایین پەیامی javac پێش هەموویان | Fix the first error, then compile again.یەکەم هەڵە چاک بکە، پاشان دووپاتە کۆمپایل بکەرەوە. |
| Looking only at the line in the messageتەنیا سەیرکردنی ئەو دێڕەی لە پەیامەکەدایە | If that line looks fine, look just before the ^ and at the end of the line above.ئەگەر ئەو دێڕە دروست دەردەکەوێت، سەیری پێش ^ و کۆتایی دێڕی سەرەوە بکە. |
Hunting for a missing } at the line javac namesگەڕان بەدوای }ـی کەم لەو دێڕەی javac ناوی دەبات | reached end of file while parsing is reported at the end; count the braces from the top.reached end of file while parsing لە کۆتاییدا ڕادەگەیەنرێت؛ کەوانەکان لە سەرەوە بژمێرە. |
| “It compiles, so it is correct”«کۆمپایل دەبێت، کەواتە دروستە» | Compiling checks only the grammar. Test with answers you know.کۆمپایلکردن تەنیا ڕێزمانەکە دەپشکنێت. بە وەڵامی زانراو تاقی بکەرەوە. |
| Reading a Java stack trace from the bottom, as in Pythonخوێندنەوەی Java لە خوارەوە، وەک Python | In Java the top at line is where the error happened.لە Javaـدا یەکەم دێڕی at شوێنی ڕوودانی هەڵەکەیە. |
Ignoring Infinity or NaN in the outputگوێنەدان بە Infinity یان NaN لە | They are bugs too: look for a division by zero.ئەوانیش : بەدوای دابەشکردن بەسەر سفردا بگەڕێ. |
| Changing many lines at onceگۆڕینی چەند دێڕێک پێکەوە | Change one thing, then run again.یەک شت بگۆڕە، پاشان دووپاتە جێبەجێی بکەرەوە. |
| Pasting code from PDFs, slides or screenshotsلکاندنی کۆد لە PDF، سلاید یان | Retype quotes, minus signs and spaces; delete editor hints such as a:.، نیشانەی کەم و بۆشاییەکان دووپاتە بنووسەوە؛ ناوە یارمەتیدەرەکانی وەک a: بسڕەوە. |
Running the old .class file after a fixجێبەجێکردنی فایلی .classـی کۆن دوای چاککردن | Compile again first (Compiling and running).سەرەتا دووپاتە کۆمپایل بکەرەوە (کۆمپایلکردن و جێبەجێکردن). |
| Leaving DEBUG lines in the finished programهێشتنەوەی دێڕەکانی DEBUG لە تەواوکراودا | Delete them once the bug is fixed.کاتێک چاک بوو بیانسڕەوە. |
Practiceڕاهێنان
Each line is the only statement in main. Decide whether it gives a syntax error, a runtime error or a logic error, then check with javac and java. (a) System.out.println("Hi") · (b) System.out.println(10 / 0); · (c) System.out.println("2 + 3 = " + 2 * 3); · (d) System.out.prinln("Hi"); · (e) System.out.println(Integer.parseInt("12a")); · (f) System.out.println(7 / 2 * 2.0); (you expected 7.0)
هەر دێڕێک تاکە ناو mainـە. بڕیار بدە ، یان دەدات، پاشان بە javac و java بیپشکنە. (a) System.out.println("Hi") · (b) System.out.println(10 / 0); · (c) System.out.println("2 + 3 = " + 2 * 3); · (d) System.out.prinln("Hi"); · (e) System.out.println(Integer.parseInt("12a")); · (f) System.out.println(7 / 2 * 2.0); (چاوەڕوانی 7.0 بوویت)
Show answerوەڵامەکە پیشان بدە
- (a) Syntax error: javac says
';' expected. - (b) Runtime error:
ArithmeticException: / by zero. - (c) Logic error: it prints
2 + 3 = 6, because the code multiplies. - (d) Syntax error:
cannot find symbol(the method isprintln). - (e) Runtime error:
NumberFormatException: For input string: "12a". - (f) Logic error:
7 / 2is3(integer division), so it prints6.0.
- (a) : javac دەڵێت
';' expected. - (b) : پەیامی
ArithmeticException: / by zero. - (c) :
2 + 3 = 6چاپ دەکات، چونکە کۆدەکە لێکدەدات. - (d) :
cannot find symbol(ناویprintlnـە). - (e) : پەیامی
NumberFormatException: For input string: "12a". - (f) :
7 / 2دەکاتە3()، بۆیە6.0چاپ دەکات.
A quiz program prints each student's score as a percentage. Answer: (1) Which exception stopped it? (2) In which method and line? (3) Which line called that method? (4) Which call in main started it all? (5) How would you fix it?
تاقیکردنەوە نمرەی هەر قوتابییەک بە سەدی چاپ دەکات. وەڵام بدەرەوە: (1) کام ڕایگرت؟ (2) لە کام و دێڕدا؟ (3) کام دێڕ ئەو بانگ کرد؟ (4) کام بانگکردن لە mainـدا هەموو شتەکەی دەست پێ کرد؟ (5) چۆن چاکی دەکەیتەوە؟
public class Report {
static int percent(int part, int whole) {
return part * 100 / whole;
}
static void printLine(String name, int correct, int questions) {
System.out.println(name + ": " + percent(correct, questions) + "%");
}
public static void main(String[] args) {
printLine("Aram", 18, 20);
printLine("Shilan", 0, 0);
}
}$ java Report Aram: 90% Exception in thread "main" java.lang.ArithmeticException: / by zero at Report.percent(Report.java:3) at Report.printLine(Report.java:7) at Report.main(Report.java:12)
Show answerوەڵامەکە پیشان بدە
- (1)
ArithmeticException: / by zero. - (2) In
percent, line 3:part * 100 / wholewithwhole= 0. - (3) Line 7, inside
printLine. - (4) Line 12:
printLine("Shilan", 0, 0); Aram's line (90%) had already been printed. - (5) A test with 0 questions cannot have a percentage: call it with the real number of questions, or check for 0 before dividing (decisions come in The if statement).
- (1)
ArithmeticException: / by zeroڕایگرت. - (2) لە
percent، دێڕی 3:part * 100 / wholeکاتێکwhole= 0. - (3) دێڕی 7، لە ناو
printLineـدا. - (4) دێڕی 12:
printLine("Shilan", 0, 0)؛ دێڕەکەی ئارام (90%) پێشتر چاپ کرابوو. - (5) تاقیکردنەوەیەک بە 0 پرسیار ناتوانێت بە سەدی هەبێت: بە ژمارەی ڕاستەقینەی پرسیارەکان بانگی بکە، یان پێش دابەشکردن 0 بپشکنە (بڕیاردان لە ڕستەی ifدا دێت).
This program should turn 95 °F back into 35 °C, but it prints 0.0. Find the bug with a known-answer test and fix it.
ئەم دەبێت 95 °F بگەڕێنێتەوە بۆ 35 °C، بەڵام 0.0 چاپ دەکات. بە تاقیکردنەوەیەکی وەڵامزانراو بدۆزەرەوە و چاکی بکەرەوە.
public class FahrenheitToCelsius {
public static void main(String[] args) {
double fahrenheit = 95;
double celsius = (5 / 9) * (fahrenheit - 32);
System.out.println(fahrenheit + " F = " + celsius + " C");
}
}95.0 F = 0.0 C
Show answerوەڵامەکە پیشان بدە
5 / 9 is integer division and gives 0, so celsius is always 0.0. Write 5.0 / 9:
5 / 9 و 0 دەدات، بۆیە celsius هەمیشە 0.0ـە. 5.0 / 9 بنووسە:
public class FahrenheitToCelsius {
public static void main(String[] args) {
double fahrenheit = 95;
double celsius = (5.0 / 9) * (fahrenheit - 32);
System.out.println(fahrenheit + " F = " + celsius + " C");
}
}95.0 F = 35.0 C
Three friends drink 3 glasses of tea at 750 dinars each and share the bill. This program has one syntax error, one runtime error and one logic error. Fix them one at a time, compiling and running after each fix. What does it print at each step?
سێ هاوڕێ 3 پیاڵە چا دەخۆنەوە (هەریەکەی بە 750 دینار) و پسوولەکە بەش دەکەن. ئەم یەک ، یەک و یەک تێدایە. یەک بە یەک چاکیان بکە و دوای هەر چاککردنێک کۆمپایل و جێبەجێی بکە. لە هەر هەنگاوێکدا چی چاپ دەکات؟
public class TeaBill {
public static void main(String[] args) {
int glasses = 3;
int price = 750
int friends = 0;
System.out.println("Total: " + glasses + price);
System.out.println("Each friend pays: " + glasses * price / friends);
}
}$ javac TeaBill.java
TeaBill.java:4: error: ';' expected
int price = 750
^
1 errorShow answerوەڵامەکە پیشان بدە
Syntax: javac stops at line 4 with
';' expected; add the;.: javac لە دێڕی 4دا بە
';' expectedدەوەستێت؛;زیاد بکە.Runtime: it prints
Total: 3750and thenArithmeticException: / by zeroat line 7, becausefriendsis 0. Make it 3.:
Total: 3750چاپ دەکات و پاشان لە دێڕی 7داArithmeticException: / by zero، چونکەfriends0ـە. بیکە بە 3.Logic:
Total: 3750is wrong.+joined the text3and750instead of multiplying. Write"Total: " + (glasses * price).:
Total: 3750هەڵەیە.+دەقی3و750ـی پێکەوە لکاند لە جیاتی لێکدان."Total: " + (glasses * price)بنووسە.
public class TeaBill {
public static void main(String[] args) {
int glasses = 3;
int price = 750;
int friends = 3;
System.out.println("Total: " + (glasses * price));
System.out.println("Each friend pays: " + glasses * price / friends);
}
}Total: 2250 Each friend pays: 750
Summaryپوختە
- Syntax errors break Java's grammar;
javacfinds them before anything runs. Read the file, the line, the message and the^, and fix the first error first. - Runtime errors stop a running program with an exception. A Java stack trace lists the newest call at the top; look for the first line with your own file.
1 / 0throwsArithmeticException;1.0 / 0givesInfinityand0.0 / 0givesNaN, which are bugs that do not stop the program.- Logic errors give wrong answers with no message, such as
9 / 5= 1. Find them by testing with answers you know. - Debug in a cycle: find where, find the cause, change one thing, test again. Print values, or use a debugger with breakpoints and Step Over.
- ڕێزمانی Java دەشکێنن؛
javacپێش جێبەجێبوونی هەر شتێک دەیاندۆزێتەوە. ، دێڕەکە، پەیامەکە و^بخوێنەوە و سەرەتا یەکەم هەڵە چاک بکە. - کارا بە ڕادەگرن. Java نوێترین بانگکردن لە سەرەوە دادەنێت؛ بەدوای یەکەم دێڕدا بگەڕێ کە خۆتی تێدایە.
1 / 0ArithmeticExceptionفڕێ دەدات؛1.0 / 0Infinityدەدات و0.0 / 0NaN، کە و ڕاناگرن.- وەڵامی هەڵە بێ پەیام دەدەن، وەک
9 / 5= 1. بە تاقیکردنەوە بە وەڵامی زانراو بیاندۆزەرەوە. - بە خولێک بکە: شوێنەکەی بدۆزەرەوە، هۆکارەکەی بدۆزەرەوە، یەک شت بگۆڕە، دووپاتە تاقی بکەرەوە. چاپ بکە، یان ئامرازی لەگەڵ و Step Over بەکار بهێنە.
Who finds each kind of error?کێ هەر جۆرێکی هەڵە دەدۆزێتەوە؟
javac finds syntax errors, the JVM finds runtime errors, and only a person who knows the right answer finds logic errors.
javac دەدۆزێتەوە، دەدۆزێتەوە و تەنیا کەسێک کە وەڵامە ڕاستەکە دەزانێت دەدۆزێتەوە.
Why should you fix the first javac error first?بۆچی دەبێت سەرەتا یەکەم هەڵەی javac چاک بکەیت؟
One mistake can cause several messages; fixing the first and compiling again often removes the rest.
یەک هەڵە دەتوانێت چەند پەیامێک دروست بکات؛ چاککردنی یەکەمیان و دووپاتە کۆمپایلکردن زۆرجار ئەوانی تر لادەبات.
What is a breakpoint?خاڵی وەستان چییە؟
A mark on a line in a debugger: the program pauses there, so you can see the variables and run one line at a time.
نیشانەیەکە لەسەر دێڕێک لە ئامرازی : لەوێ ڕادەوەستێت، بۆ ئەوەی ببینیت و هەر جارە یەک دێڕ جێبەجێ بکەیت.
Lesson complete!وانەکە تەواو بوو!
Well done. Review the key terms, then go on to the next lesson.دەستخۆش. سەیرێکی زاراوە سەرەکییەکان بکەرەوە، پاشان بڕۆ بۆ وانەی داهاتوو.
Key termsزاراوە سەرەکییەکان
Variables, Names and Constantsگۆڕاوەکان، ناوەکان و نەگۆڕەکان
Related lecturesوانە پەیوەندیدارەکان
- What Is Programming?بەرنامەسازی چییە؟
- Errors and Debuggingهەڵەکان و کرمەکوژی
- Your First Java Program: Output and Commentsیەکەم بەرنامەی Javaـت: دەرخستە و لێدوانەکان
- Primitive Data Typesجۆرە سەرەتاییەکان
- Arithmetic, Increment and Assignment Operatorsکارپێکەرە ژمێرەییەکان و کارپێکەرەکانی دانانی نرخ،
++و-- - Keyboard Input with Scanner and Formatted Outputنێوخستە لە کیبۆردەوە بە Scanner و دەرخستەی ڕێکخراو
- Exceptions: try, catch and finallyهەڵاوێردەکان: try، catch و finally
References and further readingسەرچاوەکان و خوێندنەوەی زیاتر
- W3Schools: Java Errors
- W3Schools: Java Debugging
- The Java Language Specification, Java SE 17 — §4.2.2 integer operations and §4.2.4 floating-point operations
- Oracle: the jdb command (JDK 17)
- Visual Studio Code: Running and debugging Java
- JetBrains: Debug your first Java application (IntelliJ IDEA)
- dev.java: Learn Java
This lecture is a first edition. The Kurdish text and terminology are waiting for review by Kurdish IT teachers.ئەم وانەیە چاپی یەکەمە. دەقە کوردییەکە و زاراوەکانی چاوەڕێی پێداچوونەوەی مامۆستایانی کوردی بواری IT ن.