נבנה על ידיBuilt by Ariel Yohimovich

חזרה למפגשBack
JavaJava

דיבוג ב-IntelliJDebugging in IntelliJ

מפגש 11: נקודות עצירה, Step Over ו-Step Into, צפייה במשתנים, קריאת stack trace ושיטה למציאת באגים.Session 11: Breakpoints, Step Over and Step Into, watching variables, reading a stack trace and a method for finding bugs.

מה נעשה היוםToday's plan

  • מונחיםTerms
  • קוראים stack trace מלמעלהRead a stack trace from the top
  • הסימולציה היא דיבאגרThe simulation is a debugger
  • הדפסות דיבוגDebug prints
  • טעויות נפוצותCommon mistakes
  • פרויקט מדףShelf project
נושאTopic 1

מונחיםTerms

המונחים של המפגשThis session's terms

  • דיבאגר: כלי שמריץ את התוכנית לאט, עוצר איפה שביקשתם ומראה את ערכי המשתנים.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.
  • Step Over: מריץ את השורה הנוכחית כולה (גם אם יש בה קריאה למתודה) ועוצר בשורה הבאה.Step Over: Runs the current line completely (even if it calls a method) and stops at the next line.
  • Step Into: נכנס לתוך המתודה שנקראת בשורה הנוכחית, כדי לראות מה קורה בתוכה.Step Into: Goes into the method called on the current line, to see what happens inside it.
נושאTopic 2

קוראים stack trace מלמעלהRead a stack trace from the top

קוראים stack trace מלמעלהRead a stack trace from the top

  • השורה הראשונה אומרת מה קרה (ArithmeticException: / by zero).The first line says what happened (ArithmeticException: / by zero).
  • השורות at אומרות איפה: העליונה היא המקום שבו זה נשבר, והבאות הן מי שקרא לו.The at lines say where: the top one is where it broke, and the next ones are who called it.
  • מחפשים את השורה הראשונה מהקוד שלכם (Main.java:7) ומתחילים משם.Find the first line from your code (Main.java:7) and start there.
נושאTopic 3

הסימולציה היא דיבאגרThe simulation is a debugger

הסימולציה היא דיבאגרThe simulation is a debugger

  • "צפו בזה רץ" עושה בדיוק מה ש-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.
  • עקבו אחרי i: הוא אף פעם לא מגיע ל-5.Follow i: it never reaches 5.
שאלה לכיתהQuestion for the class

מה ידפיס הקוד?What will this print?

main
int sum = 0;
for (int i = 1; i < 5; i++) {
    sum += i;
}
System.out.println("1+2+3+4+5 = " + sum);

    הסימולציה היא דיבאגרThe simulation is a debugger

    Main.java · main
    int sum = 0;
    for (int i = 1; i < 5; i++) {
        sum += i;
    }
    System.out.println("1+2+3+4+5 = " + sum);
    פלטOutput
    1+2+3+4+5 = 10
    נושאTopic 4

    הדפסות דיבוגDebug prints

    הדפסות דיבוגDebug prints

    • כשאין דיבאגר בהישג יד, מדפיסים את המשתנים בתוך הלולאה.When there's no debugger at hand, print the variables inside the loop.
    • מסמנים את השורות ב-DEBUG כדי למצוא ולמחוק אותן אחר כך.Mark the lines with DEBUG so you can find and delete them later.
    נושאTopic 5

    טעויות נפוצותCommon mistakes

    טעויות נפוצות, ומה Java עונהCommon mistakes, and what Java says

    • `StringIndexOutOfBoundsException:`StringIndexOutOfBoundsException:
    • באג 1:Bug 1:
    • `NumberFormatException:`NumberFormatException:
    נושאTopic 6

    פרויקט מדףShelf project

    הצעד של היום בפרויקט מדףToday's step in the Shelf project

    • מדף, צעד 11: שלושה באגיםShelf, step 11: three bugs

    דיבאגר: כלי שמריץ את התוכנית לאט, עוצר איפה שביקשתם ומראה את ערכי המשתנים.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.

    Step Over: מריץ את השורה הנוכחית כולה (גם אם יש בה קריאה למתודה) ועוצר בשורה הבאה.Step Over: Runs the current line completely (even if it calls a method) and stops at the next line.

    Step Into: נכנס לתוך המתודה שנקראת בשורה הנוכחית, כדי לראות מה קורה בתוכה.Step Into: Goes into the method called on the current line, to see what happens inside it.

    השורה הראשונה אומרת מה קרה (ArithmeticException: / by zero).The first line says what happened (ArithmeticException: / by zero).

    השורות at אומרות איפה: העליונה היא המקום שבו זה נשבר, והבאות הן מי שקרא לו.The at lines say where: the top one is where it broke, and the next ones are who called it.

    מחפשים את השורה הראשונה מהקוד שלכם (Main.java:7) ומתחילים משם.Find the first line from your code (Main.java:7) and start there.

    "צפו בזה רץ" עושה בדיוק מה ש-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.

    עקבו אחרי i: הוא אף פעם לא מגיע ל-5.Follow i: it never reaches 5.

    כשאין דיבאגר בהישג יד, מדפיסים את המשתנים בתוך הלולאה.When there's no debugger at hand, print the variables inside the loop.

    מסמנים את השורות ב-DEBUG כדי למצוא ולמחוק אותן אחר כך.Mark the lines with DEBUG so you can find and delete them later.

    `StringIndexOutOfBoundsException:`StringIndexOutOfBoundsException:

    באג 1:Bug 1:

    `NumberFormatException:`NumberFormatException:

    מדף, צעד 11: שלושה באגיםShelf, step 11: three bugs

    ב-stack trace, איזו שורת at מראה איפה התוכנית נשברה בפועל?In a stack trace, which at line shows where the program actually broke?

    • העליונהThe top one
    • התחתונהThe bottom one
    • האמצעיתThe middle one
    • אין קשר לסדרOrder doesn't matter

    העליונהThe top one

    עצרתם על שורה x = compute(5);. מה עושה Step Into (F7)?You stopped at x = compute(5);. What does Step Into (F7) do?

    • נכנס לתוך compute ועוצר בשורה הראשונה שלהGoes into compute and stops at its first line
    • מדלג על השורה בלי להריץSkips the line without running it
    • מריץ עד הסוףRuns to the end
    • עוצר את התוכניתStops the program

    נכנס לתוך compute ועוצר בשורה הראשונה שלהGoes into compute and stops at its first line

    double avg = (2 + 3) / 2; מה הערך?double avg = (2 + 3) / 2; What's the value?

    • 2.52.5
    • 2.02.0
    • 22
    • שגיאהAn error

    2.02.0

    ממוצע שיוצא עגול מדיAn average that's too round

    • הממוצע של 2 ו-3 יוצא 2.0.The average of 2 and 3 comes out 2.0.

    האות האחרונהThe last letter

    • last("Java") אמורה להחזיר a, והתוכנית קורסת.last("Java") should return a, and the program crashes.

    תנועה חסרהA missing vowel

    • ב-banana יש 3 תנועות, והתוכנית מדפיסה 2.banana has 3 vowels, and the program prints 2.

    מקסימום של שלילייםMax of negatives

    • max({-5, -2, -9}) מחזירה 0, שבכלל לא במערך.max({-5, -2, -9}) returns 0, which isn't even in the array.

    the שלא נמצאThe the that isn't found

    • התוכנית סופרת כמה פעמים the מופיע, ומדפיסה 0 במקום 2.The program counts how many times the appears, and prints 0 instead of 2.

    מדף, צעד 11: שלושה באגיםShelf, step 11: three bugs

    • בקוד של המדף יש שלושה באגים.The shelf code has three bugs.

    מה למדנוWhat we learned

    • דיבאגר: כלי שמריץ את התוכנית לאט, עוצר איפה שביקשתם ומראה את ערכי המשתנים.debugger: A tool that runs the program slowly, stops where you asked and shows the variables' values.
    • השורה הראשונה אומרת מה קרה (ArithmeticException: / by zero).The first line says what happened (ArithmeticException: / by zero).
    • "צפו בזה רץ" עושה בדיוק מה ש-Step Over עושה ב-IntelliJ: שורה אחרי שורה, עם הערכים של המשתנים."Watch it run" does exactly what Step Over does in IntelliJ: line after line, with the variables' values.
    • כשאין דיבאגר בהישג יד, מדפיסים את המשתנים בתוך הלולאה.When there's no debugger at hand, print the variables inside the loop.
    • `StringIndexOutOfBoundsException:`StringIndexOutOfBoundsException:
    • מדף, צעד 11: שלושה באגיםShelf, step 11: three bugs

    עכשיו לתרגולNow, practice

    6 תרגילים ו-2 אתגרים, עם בדיקה אוטומטית6 exercises and 2 challenges, auto-checked
    הערות מרצהSpeaker notes

    נבנה על ידיBuilt by Ariel Yohimovich