CENG325 9 Debugging
CENG325 9 Debugging
1
Content
Definition
Good Programming Practices
Basic Debugging, printout messages
Advanced Debugging: Netbeans
Debugger
Exception Handling Try/Catch
Types of exceptions with examples
2
Debugging Definition
The process of detecting and removing of existing and
potential errors (also called as ‘bugs’) in a software code that
can cause it to behave unexpectedly or crash.
Types of errors:
Easy to catch errors (e.g. syntax) that are taken care of by the
compiler (underlined in red with a hint message aside )
Quickly identified error by looking at the exception message given
in the output
However, there are errors which can be very tricky and take really
long to find and fix!
4
Good programming practices
Commenting:
Start your program with a comment explaining the main
purpose of the program/class.
Next to each line, or method, insert a comment to explain
what the line of code is trying to achieve.
Good commenting not only helps another programmer read
and understand your code, it would also allow you to detect
your own mistakes and remember your code objectives
Naming conventions:
Choose meaningful names for your variables, classes and
methods.
Good names facilitate the process of debugging and make
your code more portable and understandable for later use or
for other programmers.
5
Good programming practices
Modular Programming (use functions):
A procedure that is repeated several times in your
code create a method with a meaningful name
Easier for read your code later and also easier to
debug your program.
Try/Catch blocks:
Java offers an elegant way to catch all the exceptions
that might happen in your code.
By using Try/catch block, errors are caught during
runtime and can be explicitly handled not to interrupt
the course of the program.
There are many types of exceptions (to be seen later).
6
Learn by Example: Summation
program
7
Program output
• i=7 was not printed and we got an error. This is due to the fact that
the for loop has reached the character ‘a’ which is not an integer.
• The execution is interrupted and the final sum was not displayed.
8
Content
Definition
Good Programming Practices
Basic Debugging, printout messages
Advanced Debugging: Netbeans
Debugger
Exception Handling Try/Catch
Types of exceptions with examples
9
Basic debugging: printout messages
The simplest way to debug a program is to printout some
messages here and there in the code to follow up its execution
thread.
E.g. In the previous example: Output the character to the screen
and its index before trying to convert to integer.
Ou
tp
ut
10
Basic debugging: shortage
Littering your code with System.out.println here and
there to detect any buggy performance might seem
an easy idea for small codes!
11
Content
Definition
Good Programming Practices
Basic Debugging, printout messages
Advanced Debugging: Netbeans
Debugger
Exception Handling Try/Catch
Types of exceptions with examples
12
Advanced Debugging: NetBeans
Debugger
IDEs (NetBeans in our case) usually provide user with a
debugger option
The debugger is a powerful tool to find bugs by providing
an insight into the internal operations of a program.
13
Learn by Example: SumSquare prog.
14
1- Set/Remove a BreakPoint
A breakpoint: a mark in the source code that indicates
the debugger to stop when the execution reach it.
When your program stops on a breakpoint, you can
inspect the current values of the variables in your
program or continue the execution of your program,
one line at a time.
16
3. Start Debugging
Several ways to execute a java program in debug
mode:
17
Debugging SumSequare prog.
Debug Main project in the Debug menu.
Program runs and stops at the line where the
breakpoint is set and the color becomes
green.
18
Debugging options
Step Over ( - F8) - Executes one line of code.
If the line is a call to a method, executes the method
without stepping into the method's code.
21
End Debugging
To end the debugger session, click on
Finish Debugger Session in the Debug
menu.
22
Content
Definition
Good Programming Practices
Basic Debugging, printout messages
Advanced Debugging: Netbeans
Debugger
Exception Handling Try/Catch
Exceptions categories with
examples
23
Exception Definition
An exception: is a problem (unwanted event) that
arises during the execution of a program.
24
Reasons for exceptions
Scenarios in which an exception occurs:
26
Exception Categories
Checked exceptions
Unchecked exceptions
Errors
27
Checked exceptions
An exception that is checked (notified) by the
compiler at compilation-time.
28
Checked Exception example
Ou
tp
ut
Note − Since the methods read() and close() of FileReader class throws
IOException the compiler notifies to handle IOException, along with
FileNotFoundException.
29
Unchecked exceptions (Runtime
exceptions)
An exception that occurs at the time of
execution.
Ou
tp
ut
30
Errors
These are not exceptions at all, but problems
that arise beyond the control of the user or
the programmer.
32
Handling Exceptions: Try/Catch method
Java handle exceptions by using a combination of
the try and catch keywords.
A try/catch block is placed around the code that
might generate an exception.
try {
// Protected code
} catch (ExceptionType e) {
// Catch block
}
34
Finally block
The finally block follows a try block or a catch
block.
A finally block of code always executes,
irrespective of occurrence of an Exception.
Using a finally block allows you to run any
cleanup-type statements that you want to
execute,
try { no matter what happens in the
protected code. code
// Protected
} catch (ExceptionType1 e1) {
// Catch block
} catch (ExceptionType2 e2) {
// Catch block
}finally {
// The finally block always executes.
35 }
Learn by Example: Summation program
• We will add another mistake to the first string summation programmer.
• Modify your code so it looks like the following. Note the <= in the for
loop (access beyond the bounds of the string (outOfBound)).
36
Output
By handling exceptions
We can visualize and customize the way we detect errors
We allow the program to continue normally (not to be interrupted).
Catch block can be controlled to end the program as well.
37