חלקPart 2: מתודות ומערכיםMethods & arrays
כ-3 שעות (כולל הפסקה)About 3 hours (with a break)
דיבוג ב-IntelliJDebugging in IntelliJ
נקודות עצירה, Step Over ו-Step Into, צפייה במשתנים, קריאת stack trace ושיטה למציאת באגים.
Breakpoints, Step Over and Step Into, watching variables, reading a stack trace and a method for finding bugs.
הנקודות החשובותKey points
- דיבאגר: כלי שמריץ את התוכנית לאט, עוצר איפה שביקשתם ומראה את ערכי המשתנים.debugger: A tool that runs the program slowly, stops where you asked and shows the variables' values.
- נקודת עצירה: נקודה אדומה שמסמנים בלחיצה ליד מספר השורה.breakpoint: A red dot you set by clicking next to a line number.
- השורה הראשונה אומרת מה קרה (
ArithmeticException: / by zero).The first line says what happened (ArithmeticException: / by zero). - השורות
atאומרות איפה: העליונה היא המקום שבו זה נשבר, והבאות הן מי שקרא לו.Theatlines say where: the top one is where it broke, and the next ones are who called it. - "צפו בזה רץ" עושה בדיוק מה ש-Step Over עושה ב-IntelliJ: שורה אחרי שורה, עם הערכים של המשתנים."Watch it run" does exactly what Step Over does in IntelliJ: line after line, with the variables' values.
- כאן הסכום יוצא 10 במקום 15.Here the sum comes out 10 instead of 15.
- כשאין דיבאגר בהישג יד, מדפיסים את המשתנים בתוך הלולאה.When there's no debugger at hand, print the variables inside the loop.
- מסמנים את השורות ב-
DEBUGכדי למצוא ולמחוק אותן אחר כך.Mark the lines withDEBUGso you can find and delete them later. - `StringIndexOutOfBoundsException:`StringIndexOutOfBoundsException:
מונחיםTerms
- דיבאגרdebugger: כלי שמריץ את התוכנית לאט, עוצר איפה שביקשתם ומראה את ערכי המשתנים. ב-IntelliJ: הכפתור עם החיפושית (Debug) במקום ▶.A tool that runs the program slowly, stops where you asked and shows the variables' values. In IntelliJ: the bug button (Debug) instead of ▶.
- נקודת עצירהbreakpoint: נקודה אדומה שמסמנים בלחיצה ליד מספר השורה. בהרצת Debug התוכנית נעצרת לפני השורה הזאת.A red dot you set by clicking next to a line number. In a Debug run the program stops before that line.
- Step OverStep Over: מריץ את השורה הנוכחית כולה (גם אם יש בה קריאה למתודה) ועוצר בשורה הבאה. קיצור: F8.Runs the current line completely (even if it calls a method) and stops at the next line. Shortcut: F8.
- Step IntoStep Into: נכנס לתוך המתודה שנקראת בשורה הנוכחית, כדי לראות מה קורה בתוכה. קיצור: F7.Goes into the method called on the current line, to see what happens inside it. Shortcut: F7.
- stack tracestack trace: הרשימה שמופיעה מתחת לחריגה, שורות שמתחילות ב-at. היא אומרת באיזו מתודה ובאיזו שורה החריגה קרתה.The list under an exception, lines starting with at. It says which method and which line the exception happened on.
- טעות של אחד (off-by-one)off-by-one: לולאה שרצה פעם אחת יותר מדי או פחות מדי, לרוב בגלל
<במקום<=או התחלה מ-1 במקום מ-0.A loop that runs one time too many or too few, usually from<instead of<=or starting at 1 instead of 0. - דיבאגרdebugger: כלי שמריץ את התוכנית לאט, עוצר איפה שביקשתם ומראה את ערכי המשתנים. ב-IntelliJ: הכפתור עם החיפושית (Debug) במקום ▶.A tool that runs the program slowly, stops where you asked and shows the variables' values. In IntelliJ: the bug button (Debug) instead of ▶.
- נקודת עצירהbreakpoint: נקודה אדומה שמסמנים בלחיצה ליד מספר השורה. בהרצת Debug התוכנית נעצרת לפני השורה הזאת.A red dot you set by clicking next to a line number. In a Debug run the program stops before that line.
- Step OverStep Over: מריץ את השורה הנוכחית כולה (גם אם יש בה קריאה למתודה) ועוצר בשורה הבאה. קיצור: F8.Runs the current line completely (even if it calls a method) and stops at the next line. Shortcut: F8.
- Step IntoStep Into: נכנס לתוך המתודה שנקראת בשורה הנוכחית, כדי לראות מה קורה בתוכה. קיצור: F7.Goes into the method called on the current line, to see what happens inside it. Shortcut: F7.
Cheatsheet
הסימולציה היא דיבאגרThe simulation is a debugger
int sum = 0;
for (int i = 1; i < 5; i++) {
sum += i;
}
System.out.println("1+2+3+4+5 = " + sum);הדפסות דיבוגDebug prints
int[] prices = {30, 45, 25};
int total = 0;
for (int i = 0; i < prices.length; i++) {
total += prices[i];
System.out.println("DEBUG i=" + i + " total=" + total);
}
System.out.println("Total: " + total);שגיאות שפגשנוErrors we met
| שגיאהError | מה זה אומרWhat it means | מה עושיםWhat to do |
|---|---|---|
ArithmeticException | קוראים stack trace מלמעלהRead a stack trace from the top | / by zero ב-average (שורה 7), שנקראה מ-report (שורה 11), שנקראה מ-main (שורה 16). עם {}, grades.length הוא 0./ by zero in average (line 7), called from report (line 11), called from main (line 16). With {}, grades.length is 0. |
StringIndexOutOfBoundsException | האות האחרונהThe last letter | StringIndexOutOfBoundsException: Index 4 out of bounds for length 4 בשורה 3: האות האחרונה היא באינדקס length() - 1.StringIndexOutOfBoundsException: Index 4 out of bounds for length 4 on line 3: the last letter is at index length() - 1. |
ArrayIndexOutOfBoundsException | מדף, צעד 11: שלושה באגיםShelf, step 11: three bugs | באג 1: Index 3 out of bounds for length 3 בשורה 7, כי i <= ratings.length. אחרי שתתקנו, תגלו את 2 (חילוק שלמים נותן 4) ואת 3 (== על מחרוזות, ולכן Emma לא נמצאת).Bug 1: Index 3 out of bounds for length 3 on line 7, because i <= ratings.length. Once fixed you'll find 2 (integer division gives 4) and 3 (== on strings, so Emma isn't found). |
NumberFormatException | הבאג לא איפה שהוא נשברThe bug isn't where it breaks | NumberFormatException: For input string: "2,00": ה-trace עובר דרך parsePrice (שורה 3), total (שורה 9) ו-main (שורה 15). parseDouble מכיר רק נקודה.NumberFormatException: For input string: "2,00": the trace goes through parsePrice (line 3), total (line 9) and main (line 15). parseDouble only knows a dot. |