-
자바에서 버그의 종류와 디버깅 방법에 대해 정리하였습니다.
< Introduction >
What makes a programmer successful isn’t avoiding errors; it’s knowing how to find the solution.
In Java, there are many different ways of classifying errors, but they can be boiled down to three categories:
- Syntax errors: Errors found by the compiler.
- Run-time errors: Errors that occur when the program is running.
- Logic errors: Errors found by the programmer looking for the causes of erroneous results.
< Syntax Errors >
The compiler is our first line of defense against errors. It can catch syntax errors.
Syntax errors represent grammar errors in the use of the programming language.Some common syntax errors are:
- Misspelled variable and method names.
- Omitting semicolons ;
- Omitting closing parenthesis ), square bracket ], or curly brace }
Example : missing semicolon ;
Debug.java:5: error: ';' expected int year = 2019 ^ 1 error
< Run-time Errors >
Errors which happen during program execution (run-time) after successful compilation are called run-time errors.
Run-time errors occur when a program with no compile-time errors asks the computer to do something that the computer is unable to reliably do.Some common run-time errors:
- Division by zero
- Trying to open a file that doesn’t exist
Example : Division by zero
Exception in thread "main" java.lang.ArithmeticException: / by zero at Debug.main(Debug.java:8)
< Exceptions >
Exceptions are the conditions that occur at runtime and may cause the termination of the program.
When an exception occurs, Java displays a message that includes the name of the exception, the line of the program where the exception occurred, and a stack trace.Some common exceptions that you will see in the wild:
- ArithmeticException: Something went wrong during an arithmetic operation; for example, division by zero.
- NullPointerException: You tried to access an instance variable or invoke a method on an object that is currently null.
- ArrayIndexOutOfBoundsException: The index you are using is either negative or greater than the last index of the array (i.e., array.length-1).
- FileNotFoundException: Java didn’t find the file it was looking for.
< Exception Handling >
Exception handling is an essential feature of Java programming that allows us to use run-time error exceptions to make our debugging process a little easier.
One way to handle exceptions is using the try / catch :
- The try statement allows you to define a block of code to be tested for errors while it is being executed.
- The catch statement allows you to define a block of code to be executed if an error occurs in the try block.
=> The try and catch keywords come in pairs, though you can also catch several types of exceptions in a single block
Example)
try { // Block of code to try } catch (NullPointerException e) { // Code to handle a NullPointerException System.err.println("NullPointerException: " + e.getMessage()); } catch (ArithmeticException e) { // Code to handle an ArithmeticException }
: we used System.err.println() here instead of System.out.println(). System.err.println() will print to the standard error and the text will be in red.
< Logic Errors >
Once we have removed the syntax errors and run-time errors, the program runs successfully. But sometimes, the program still doesn’t do what we want it to do or no output is produced.
These types of errors, which provide incorrect output, but appear to be error-free, are called logic errors.
Logic errors occur when there is a design flaw in your program.Some common logic errors:
- Program logic is flawed
- Some “silly” mistake in an if statement or a for/while loop
< Debugging Techniques >
If you have examined the code thoroughly, and you are sure the compiler is compiling the right source file, it is time for desperate measures
1. Divide and conquer: Comment out or temporarily delete half the code to isolate an issue.
- If the program compiles now, you know the error is in the code you deleted. Bring back about half of what you removed and repeat.
- If the program still doesn’t compile, the error must be in the code that remains. Delete about half of the remaining code and repeat.
2. Print statements for the rescue: Use System.out.println( ) to check variable/return values at various points throughout the program.
A lot of the time with logic errors, there was a flawed piece of logic, a miscalculation, a missing step, etc. By printing out the values at different stages of the execution flow, you can then hopefully pinpoint where you made a mistake.
'Java' 카테고리의 다른 글
Inheritance and Polymorphism (0) 2022.02.09 Static Variables and Methods (0) 2022.02.09 Math Methods (0) 2022.02.08 Access, Encapsulation, and Scope (0) 2022.02.08 String Methods (0) 2022.02.07