Slicing Java Programs thatThrow and Catch Exceptions
Matthew Allen
Susan Horwitz
University of Wisconsin-Madison
PEPM 2003
San Diego, CA
June 7, 2003
Slicing Java Programs that Throw and Catch Exceptions – Allen / HorwitzSlide 2
MotivationExceptions:
Important error-handling technique
BUT: Current program-slicing algorithms don’t handle
exceptions
Goal: Extend System Dependence Graph (SDG) based
slicing to support exceptions
Slicing Java Programs that Throw and Catch Exceptions – Allen / HorwitzSlide 3
OutlineMotivation
Program Slicing
Slicing with the SDG
Extending SDG-Based Slicing to Handle Exceptions
Related Work
Conclusions
Slicing Java Programs that Throw and Catch Exceptions – Allen / HorwitzSlide 4
Program Slicing
A slice from a component S is the set of
components that might affect:
Whether or how often S executes
The value of a variable used at S
Slicing Java Programs that Throw and Catch Exceptions – Allen / HorwitzSlide 5
Slicing Exampleint x, y;
void foo() { fact(); print(x); print(y);}
void fact() { x = 1; while (y > 0) { x = x * y; y--; }}
Slicing Java Programs that Throw and Catch Exceptions – Allen / HorwitzSlide 6
Slicing Exampleint x, y;
void foo() { fact(); print(x); print(y); slice from here}
void fact() { x = 1; while (y > 0) { x = x * y; y--; }}
Slicing Java Programs that Throw and Catch Exceptions – Allen / HorwitzSlide 7
Slicing Exampleint x, y;
void foo() { fact(); print(x); print(y); slice from here}
void fact() { x = 1; while (y > 0) { x = x * y; y--; }}
Slicing Java Programs that Throw and Catch Exceptions – Allen / HorwitzSlide 8
Slicing Programs with Exceptions
Exceptions can affect:
Whether or how often a statement executes
Value of a variable used at a statement
Slicing Java Programs that Throw and Catch Exceptions – Allen / HorwitzSlide 9
Example: when/how often stmt executes
void foo() {
try {
fact();
print(“no error”);
}
catch (NegEx e) {
print(“error”);
}
}
void fact() throws NegEx {
x = 1;
if (y < 0)
throw new NegEx();
while (y > 0) {
x = x * y;
y--;
}
}
Slicing Java Programs that Throw and Catch Exceptions – Allen / HorwitzSlide 10
void foo() {
try {
fact();
print(“no error”);
}
catch (NegEx e) {
print(“error”); Slice from here
}
}
void fact() throws NegEx {
x = 1;
if (y < 0)
throw new NegEx();
while (y > 0) {
x = x * y;
y--;
}
}
Example: when/how often stmt executes
Slicing Java Programs that Throw and Catch Exceptions – Allen / HorwitzSlide 11
void foo() {
try {
fact();
print(“no error”);
}
catch (NegEx e) {
print(“error”); Slice from here
}
}
void fact() throws NegEx {
x = 1;
if (y < 0)
throw new NegEx();
while (y > 0) {
x = x * y;
y--;
}
}
Example: when/how often stmt executes
Slicing Java Programs that Throw and Catch Exceptions – Allen / HorwitzSlide 12
Exception affects value of variable
void foo() {
try {
fact();
print(x);
}
catch (NegEx e) {
print(x);
}
}
void fact() throws NegEx {
x = 1;
if (y < 0)
throw new NegEx();
while (y > 0) {
x = x * y;
y--;
}
}
Slicing Java Programs that Throw and Catch Exceptions – Allen / HorwitzSlide 13
Exception affects value of variable
void foo() {
try {
fact();
print(x);
}
catch (NegEx e) {
print(x); Slice from here
}
}
void fact() throws NegEx {
x = 1;
if (y < 0)
throw new NegEx();
while (y > 0) {
x = x * y;
y--;
}
}
Slicing Java Programs that Throw and Catch Exceptions – Allen / HorwitzSlide 14
Exception affects value of variable
void foo() {
try {
fact();
print(x);
}
catch (NegEx e) {
print(x); Slice from here
}
}
void fact() throws NegEx {
x = 1;
if (y < 0)
throw new NegEx();
while (y > 0) {
x = x * y;
y--;
}
}
Slicing Java Programs that Throw and Catch Exceptions – Allen / HorwitzSlide 15
Exception affects value of variable
void foo() {
try {
fact();
print(x);
}
catch (NegEx e) {
print(x); Slice from here
}
}
void fact() throws NegEx {
x = 1;
if (y < 0)
throw new NegEx();
while (y > 0) {
x = x * y;
y--;
}
}
Slicing Java Programs that Throw and Catch Exceptions – Allen / HorwitzSlide 16
Exception affects value of variable
void foo() {
try {
fact();
print(x); Slice from here
}
catch (NegEx e) {
print(x);
}
}
void fact() throws NegEx {
x = 1;
if (y < 0)
throw new NegEx();
while (y > 0) {
x = x * y;
y--;
}
}
Slicing Java Programs that Throw and Catch Exceptions – Allen / HorwitzSlide 17
Exception affects value of variable
void foo() {
try {
fact();
print(x); Slice from here
}
catch (NegEx e) {
print(x);
}
}
void fact() throws NegEx {
x = 1;
if (y < 0)
throw new NegEx();
while (y > 0) {
x = x * y;
y--;
}
}
Slicing Java Programs that Throw and Catch Exceptions – Allen / HorwitzSlide 18
OutlineMotivation
Program Slicing
Slicing with the SDG (no exceptions)
Extending SDG-Based Slicing to Handle Exceptions
Related Work
Conclusions
CFGPDGSDG
static void foo() {
fact();
print(x);
print(y);
}
static void fact()
throws NegEx
{
x = 1;
while (y > 0) {
x = x * y;
y--;
}
}
enter foo
print(x)
print(y)
enter fact
x = 1
x = x * y
y--
call fact
exit
T
T
F
F
T
while (y > 0)
exit foo
F
CFGPDGSDG
static void foo() {
fact();
print(x);
print(y);
}
static void fact()
throws NegEx
{
x = 1;
while (y > 0) {
x = x * y;
y--;
}
}
enter fooy = y_in
print(x)
print(y)
enter facty = y_in
x = 1
x = x * y
y--
y_in = ycall factx = x_outy = y_out
x_out = xy_out = y
exit
T
T
F
F
T
while (y > 0)
exit foo
F
Slicing Java Programs that Throw and Catch Exceptions – Allen / HorwitzSlide 21
CFGPDGSDG
static void foo() {
fact();
print(x);
print(y);
}
static void fact()
throws NegEx
{
x = 1;
while (y > 0) {
x = x * y;
y--;
}
}
enter foo
print(x) print(y)
enter fact
x = 1 while (y > 0)
x = x * y y--
call fact
y_in = y x = x_out y = y_out
y=y_in
y = y_in
x_out=x y_out=y
T T TT
T T T
TT T T T
T T
Slicing Java Programs that Throw and Catch Exceptions – Allen / HorwitzSlide 22
CFGPDGSDG
static void foo() {
fact();
print(x);
print(y);
}
static void fact()
throws NegEx
{
x = 1;
while (y > 0) {
x = x * y;
y--;
}
}
enter foo
print(x) print(y)
enter fact
x = 1 while (y > 0)
x = x * y y--
call fact
y_in = y x = x_out y = y_out
y=y_in
y = y_in
x_out=x y_out=y
Slicing Java Programs that Throw and Catch Exceptions – Allen / HorwitzSlide 23
print(y)
CFGPDGSDG
static void foo() {
fact();
print(x);
print(y);
}
static void fact()
throws NegEx
{
x = 1;
while (y > 0) {
x = x * y;
y--;
}
}
enter foo
print(x)
enter fact
x = 1 while (y > 0)
x = x * y y--
call fact
y_in = y x = x_out y = y_out
y=y_in
y = y_in
x_out=x y_out=y
Slicing Java Programs that Throw and Catch Exceptions – Allen / HorwitzSlide 24
print(y)
CFGPDGSDG
static void foo() {
fact();
print(x);
print(y);
}
static void fact()
throws NegEx
{
x = 1;
while (y > 0) {
x = x * y;
y--;
}
}
enter foo
print(x)
enter fact
x = 1 while (y > 0)
x = x * y y--
call fact
y_in = y x = x_out y = y_out
y=y_in
y = y_in
x_out=x y_out=y
enter foo
call fact
y_in = y y = y_out
y = y_in
Slicing Java Programs that Throw and Catch Exceptions – Allen / HorwitzSlide 25
print(y)
CFGPDGSDG
static void foo() {
fact();
print(x);
print(y);
}
static void fact()
throws NegEx
{
x = 1;
while (y > 0) {
x = x * y;
y--;
}
}
enter foo
print(x)
enter fact
x = 1 while (y > 0)
x = x * y y--
call fact
y_in = y x = x_out y = y_out
y=y_in
y = y_in
x_out=x y_out=y
enter foo
call fact
y_in = y y = y_out
y = y_in
enter fact
while (y > 0)
y--
y=y_in y_out=y
Slicing Java Programs that Throw and Catch Exceptions – Allen / HorwitzSlide 26
OutlineMotivation
Program Slicing
Slicing with the SDG
Extending SDG-Based Slicing to Handle Exceptions
Related Work
Conclusions
Slicing Java Programs that Throw and Catch Exceptions – Allen / HorwitzSlide 27
Example Revisited
static void foo() {
try {
fact();
print(“no error”);
}
catch (NegEx e) {
print(“error”); Slice from here
}
}
static void fact() throws NegEx {
x = 1;
if (y < 0)
throw new NegEx ();
while (y > 0) {
x = x * y;
y--;
}
}
Slicing Java Programs that Throw and Catch Exceptions – Allen / HorwitzSlide 28
enter foo
y=y_in try
call fact
y_in=y x=x_out y=y_out normalreturn
catch(NegEx e)
enter factprint(“no error”)
print(“error”)
y=y_in x=1 if(y<0) x_out=x y_out=y
throw newNegEx()
while(y>0)
normalexit
NegExexit
x=x*y y--
Slicing Java Programs that Throw and Catch Exceptions – Allen / HorwitzSlide 29
enter foo
y=y_in try
call fact
y_in=y x=x_out y=y_out normalreturn
catch(NegEx e)
enter factprint(“no error”)
print(“error”)
y=y_in x=1 if(y<0) x_out=x y_out=y
throw newNegEx()
while(y>0)
normalexit
NegExexit
x=x*y y--
catch(NegEx e)
NegExexit
throw newNegEx()
if(y<0)
Slicing Java Programs that Throw and Catch Exceptions – Allen / HorwitzSlide 30
static void fact()
throws NegEx
{
x = 1;
if (y < 0)
throw new NegEx ();
while (y > 0) {
x = x * y;
y--;
}
}
Slicing Java Programs that Throw and Catch Exceptions – Allen / HorwitzSlide 31
static void fact()
throws NegEx
{
x = 1;
if (y < 0)
throw new NegEx ();
while (y > 0) {
x = x * y;
y--;
}
}
enter facty = y_in
x=1
if(y<0)
throw new NegEx()
while (y>0)
x_out = xy_out = y
exit
x=x*y
y--
T
T
T
F
F
F
Slicing Java Programs that Throw and Catch Exceptions – Allen / HorwitzSlide 32
static void fact() {
x = 1;
if (y < 0)
throw new NegEx ();
while (y > 0) {
x = x * y;
y--;
}
}
enter facty = y_in
x=1
if(y<0)
throw new NegEx()
while (y>0)
x_out = xy_out = y
exit
x=x*y
y--
T
T
T
F
F
NegEx exit
F
Slicing Java Programs that Throw and Catch Exceptions – Allen / HorwitzSlide 33
static void fact() {
x = 1;
if (y < 0)
throw new NegEx ();
while (y > 0) {
x = x * y;
y--;
}
}
enter facty = y_in
x=1
if(y<0)
throw new NegEx()
while (y>0)
x_out = xy_out = y
exit
x=x*y
y--
T
T
T
F
F
NegEx exit
normal exit
F
Slicing Java Programs that Throw and Catch Exceptions – Allen / HorwitzSlide 34
static void fact() {
x = 1;
if (y < 0)
throw new NegEx ();
while (y > 0) {
x = x * y;
y--;
}
}
enter facty = y_in
x=1
if(y<0)
throw new NegEx()
while (y>0)
x_out = xy_out = y
exit
x=x*y
y--
T
T
T
F
F
NegEx exit
F
normal exit
TF
Slicing Java Programs that Throw and Catch Exceptions – Allen / HorwitzSlide 35
static void fact() {
x = 1;
if (y < 0)
throw new NegEx ();
while (y > 0) {
x = x * y;
y--;
}
}
enter facty = y_in
x=1
if(y<0)
throw new NegEx()
while (y>0)
x_out = xy_out = y
exit
x=x*y
y--
T
T
T
F
F
NegEx exit
F
normal exit
TF
Slicing Java Programs that Throw and Catch Exceptions – Allen / HorwitzSlide 36
static void foo() {
try {
fact();
print(“no error”);
}
catch (NegEx e) {
print(“error”);
}
}
enter foo
try
y = y_incall fact
catch(NegEx e)
print(“no error”)
print(“error”)
x_out = xy_out = y
exit
Slicing Java Programs that Throw and Catch Exceptions – Allen / HorwitzSlide 37
static void foo() {
try {
fact();
print(“no error”);
}
catch (NegEx e) {
print(“error”);
}
}
enter foo
try
y = y_incall fact
normalreturn
catch(NegEx e)
print(“no error”)
print(“error”)
x_out = xy_out = y
exit
normal NegEx
Slicing Java Programs that Throw and Catch Exceptions – Allen / HorwitzSlide 38
static void foo() {
try {
fact();
print(“no error”);
}
catch (NegEx e) {
print(“error”);
}
}
enter foo
try
y = y_incall fact
normalreturn
catch(NegEx e)
print(“no error”)
print(“error”)
x_out = xy_out = y
exit
normal NegEx
T TF F
Slicing Java Programs that Throw and Catch Exceptions – Allen / HorwitzSlide 39
static void foo() {
try {
fact();
print(“no error”);
}
catch (NegEx e) {
print(“error”);
}
}
enter foo
try
y = y_incall fact
normalreturn
catch(NegEx e)
print(“no error”)
print(“error”)
x_out = xy_out = y
exit
normal NegEx
T TF F
TF
Slicing Java Programs that Throw and Catch Exceptions – Allen / HorwitzSlide 40
static void foo() {
try {
fact();
print(“no error”);
}
catch (NegEx e) {
print(“error”);
}
}
enter foo
try
y = y_incall fact
normalreturn
catch(NegEx e)
print(“no error”)
print(“error”)
x_out = xy_out = y
exit
normal NegEx
T TF F
TF
Slicing Java Programs that Throw and Catch Exceptions – Allen / HorwitzSlide 41
Example revisited
static void foo() {
try {
fact();
print(x);
}
catch (NegEx e) {
print(x); Slice from here
}
}
static void fact() {
x = 1;
if (y < 0)
throw new NegEx ();
while (y > 0) {
x = x * y;
y--;
}
}
Slicing Java Programs that Throw and Catch Exceptions – Allen / HorwitzSlide 42
print(x)
enter foo
y=y_in try
call fact
y_in=y x=x_out y=y_out normalreturn
catch(NegEx e)
enter factprint(x)
y=y_in x=1 if(y<0) x_out=x y_out=y
throw newNegEx()
while(y>0)
normalexit
NegExexit
x=x*y y--
x=x_out
x_out=xx=1
x=x*y
enter foo
y=y_in try
call fact
y_in=ynormalreturn
catch(NegEx e)
enter factprint(x)
y=y_in x=1 if(y<0)
throw newNegEx()
while(y>0)
normalexit
NegExexit x=x*y y--
x_out=xy_out=y
x_out=xy_out=y
x=x_out y=y_out print(x)x=x_out y=y_out
enter foo
y=y_in try
call fact
y_in=ynormalreturn
catch(NegEx e)
enter factprint(x)
y=y_in x=1 if(y<0)
throw newNegEx()
while(y>0)
normalexit
NegExexit x=x*y y--
x_out=xy_out=y
x_out=xy_out=y
x=x_out y=y_out print(x)x=x_out y=y_outx=x_out
x_out=x
x=1
enter foo
y=y_in try
call fact
y_in=ynormalreturn
catch(NegEx e)
enter factprint(x)
y=y_in x=1 if(y<0)
throw newNegEx()
while(y>0)
normalexit
NegExexit x=x*y y--
x_out=xy_out=y
x_out=xy_out=y
x=x_out y=y_out print(x)x=x_out y=y_out
Slicing Java Programs that Throw and Catch Exceptions – Allen / HorwitzSlide 46
Other Issues finally clauses Unchecked exceptions
See paper!
Slicing Java Programs that Throw and Catch Exceptions – Allen / HorwitzSlide 47
OutlineMotivation
Program Slicing
Slicing with the SDG
Extending SDG-Based Slicing to Handle Exceptions
Related Work
Conclusions
Slicing Java Programs that Throw and Catch Exceptions – Allen / HorwitzSlide 48
Related Work[Sinha / Harrold / Rothermel 1999]
Addresses slicing programs with exceptions. Some similar aspects . Problems:
Does not correctly represent interprocedural control dependences when length of call chain from try to throw is greater than 1.
Does not address data dependences.
[Sinha / Harrold 1998, 2000]
Addresses handling finally clauses. See paper.
Slicing Java Programs that Throw and Catch Exceptions – Allen / HorwitzSlide 49
ConclusionsSlicing is an important operation.
Slicing Java programs is an area of current interest.
Contribution:
Extend SDG-based Slicing: To correctly handle exceptions.
Changes only to CFG. Same slicing algorithm!
Slicing Java Programs that Throw and Catch Exceptions – Allen / HorwitzSlide 50
void foo() {
try {
fact();
print(“no error”); Slice from here
}
catch (NegEx e) {
print(“error”);
}
}
void fact() throws NegEx {
x = 1;
if (y < 0)
throw new NegEx();
while (y > 0) {
x = x * y;
y--;
}
}
Example: when/how often stmt executes
Slicing Java Programs that Throw and Catch Exceptions – Allen / HorwitzSlide 51
void foo() {
try {
fact();
print(“no error”); Slice from here
}
catch (NegEx e) {
print(“error”);
}
}
void fact() throws NegEx {
x = 1;
if (y < 0)
throw new NegEx();
while (y > 0) {
x = x * y;
y--;
}
}
Example: when/how often stmt executes
CFGPDGSDG
static void foo() {
fact();
print(x);
print(y);
}
static void fact() {
x = 1;
while (y > 0) {
x = x * y;
y--;
}
}
enter fooy = y_in
print(x)
print(y)
enter facty = y_in
x = 1
x = x * y
y--
y_in = ycall factx = x_outy = y_out
x_out = xy_out = y
exit
T
T
F
F
T
while (y > 0)
exit foo
F
datadependence
controldependence
TTT
T
TT
T
CFGPDGSDG
static void foo() {
fact();
print(x);
print(y);
}
static void fact() {
x = 1;
while (y > 0) {
x = x * y;
y--;
}
}
enter fooy = y_in
print(x)
print(y)
enter facty = y_in
x = 1
x = x * y
y--
y_in = ycall factx = x_outy = y_out
x_out = xy_out = y
exit
while (y > 0)
exit foo datadependence
controldependence
TTT
T
TT
T
Slicing Java Programs that Throw and Catch Exceptions – Allen / HorwitzSlide 54
print(y)
CFGPDGSDG
static void foo() {
fact();
print(x);
print(y);
}
static void fact() {
x = 1;
while (y > 0) {
x = x * y;
y--;
}
}
enter foo
print(x)
enter fact
x = 1 while (y > 0)
x = x * y y--
call fact
y_in = y x = x_out y = y_out
y=y_in
y = y_in
x_out=x y_out=y
Slicing Java Programs that Throw and Catch Exceptions – Allen / HorwitzSlide 55
print(y)
CFGPDGSDG
static void foo() {
fact();
print(x);
print(y);
}
static void fact() {
x = 1;
while (y > 0) {
x = x * y;
y--;
}
}
enter foo
print(x)
enter fact
x = 1 while (y > 0)
x = x * y y--
call fact
y_in = y x = x_out y = y_out
y=y_in
y = y_in
x_out=x y_out=y
Slicing Java Programs that Throw and Catch Exceptions – Allen / HorwitzSlide 56
enter foo
y=y_in try
call fact
y_in=y x=x_out y=y_out normalreturn
catch(NegEx e)
enter factprint(“no error”)
print(“error”)
y=y_in x=1 if(y<0) x_out=x y_out=y
throw newNegEx()
while(y>0)
normalexit
NegExexit
x=x*y y--
Slicing Java Programs that Throw and Catch Exceptions – Allen / HorwitzSlide 57
enter foo
y=y_in try
call fact
y_in=y x=x_out y=y_out normalreturn
catch(NegEx e)
enter factprint(“no error”)
print(“error”)
y=y_in x=1 if(y<0) x_out=x y_out=y
throw newNegEx()
while(y>0)
normalexit
NegExexit
x=x*y y--
Slicing Java Programs that Throw and Catch Exceptions – Allen / HorwitzSlide 58
enter foo
y=y_in try
call fact
y_in=y x=x_out y=y_out normalreturn
catch(NegEx e)
enter factprint(“no error”)
print(“error”)
y=y_in x=1 if(y<0) x_out=x y_out=y
throw newNegEx()
while(y>0)
normalexit
NegExexit
x=x*y y--
Slicing Java Programs that Throw and Catch Exceptions – Allen / HorwitzSlide 59
print(x)
enter foo
y=y_in try
call fact
y_in=y x=x_out y=y_out normalreturn
catch(NegEx e)
enter factprint(x)
y=y_in x=1 if(y<0) x_out=x y_out=y
throw newNegEx()
while(y>0)
normalexit
NegExexit
x=x*y y--
Slicing Java Programs that Throw and Catch Exceptions – Allen / HorwitzSlide 60
print(x)
enter foo
y=y_in try
call fact
y_in=y x=x_out y=y_out normalreturn
catch(NegEx e)
enter factprint(x)
y=y_in x=1 if(y<0) x_out=x y_out=y
throw newNegEx()
while(y>0)
normalexit
NegExexit
x=x*y y--
enter foo
y=y_in try
call fact
y_in=ynormalreturn
catch(NegEx e)
enter factprint(x)
y=y_in x=1 if(y<0)
throw newNegEx()
while(y>0)
normalexit
NegExexit x=x*y y--
x_out=xy_out=y
x_out=xy_out=y
x=x_out y=y_out print(x)x=x_out y=y_out
enter foo
y=y_in try
call fact
y_in=ynormalreturn
catch(NegEx e)
enter factprint(x)
y=y_in x=1 if(y<0)
throw newNegEx()
while(y>0)
normalexit
NegExexit x=x*y y--
x_out=xy_out=y
x_out=xy_out=y
x=x_out y=y_out print(x)x=x_out y=y_out