1911
Example Language: Java (Bad)
public class Main {
public static void main(String[] args) {
int a = 1;
int b = 0;
int c = 0;
try {
c = a / b;
} catch(ArithmeticException ae) {
}
}
}
Since the exception block is empty, no action is taken.
In the code below the exception has been logged and the bad execution has been handled in the
desired way allowing the program to continue in an expected way.
Example Language: Java (Good)
public class Main {
public static void main(String[] args) {
int a = 1;
int b = 0;
int c = 0;
try {
c = a / b;
} catch(ArithmeticException ae) {
log.error("Divided by zero detected, setting to -1.");
c = -1;
}
}
}
Example 2:
The following code attempts to synchronize on an object, but does not execute anything in
the synchronized block. This does not actually accomplish anything and may be a sign that a
programmer is wrestling with synchronization but has not yet achieved the result they intend.
Example Language: Java (Bad)
synchronized(this) { }
Instead, in a correct usage, the synchronized statement should contain procedures that access or
modify data that is exposed to multiple threads. For example, consider a scenario in which several
threads are accessing student records at the same time. The method which sets the student ID to a
new value will need to make sure that nobody else is accessing this data at the same time and will
require synchronization.
Example Language: (Good)
public void setID(int ID){
synchronized(this){
this.ID = ID;
}
}
MemberOf Relationships
This MemberOf relationships table shows additional CWE Catgeories and Views that reference this
weakness as a member. This information is often useful in understanding where a weakness fits
within the context of external information sources.
CWE Version 4.14
CWE-1072: Data Resource Access without Use of Connection Pooling
CWE-1072: Data Resource Access without Use of Connection Pooling