Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Catch exceptions (where?)

 
Paul Keohan
Ranch Hand
Posts: 411
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have the following piece of code :

import java.util.*;
import java.io.*;
import java.text.*;
import com.ibm.sns.svk.qik.*;
/**
this is the Paul class
*/
public class Paul
{
private void go()
{
try
{
method1();
method2();
method3();
method4();
}
catch (Exception e)
{
e.printStackTrace();
}
}

private void method1()
{
System.out.println("this is method 1");
}

private void method2() throws Exception
{
System.out.println("this is method 2");
int i = 10/0;
}

private void method3()
{
System.out.println("this is method 3");
}

private void method4()
{
System.out.println("this is method 4");
}

public static void main (String args[])
{
Paul p = new Paul();
p.go();
}
}
Because of the try/catch in the go() method I see the details of my ArithmeticException in method2(). I was hoping that because I catch the exception, method3() and method4() would continue to be executed. They're not and I assume this is because, once a catch is invoked, any remaining code in the try, will not be reached. So, it looks like I need to put a try/catch block around each method call if I want to proceed with the next methods after an exception. Is there a better way to do this?
Thanks!
Paul
 
Dave Vick
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul
Put your try/catch in a while loop, then when it is done with the catch it will just run the next iteration of the while loop. After the last method call you can set the while variable so the next test is false and it stops running.
something like this:

Dave
 
Paul Keohan
Ranch Hand
Posts: 411
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I appreciate the help Dave but this still doesn't work. All that happens is that method1() and method2() get called over and over again but method3() and method4() are still never reached because of the exception in mehtod2().
 
kyle amburn
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Paul-
I assume in your actual code that any of the methods can throw an exception, not just method2()?
You can always put the methods after method2() in a finally block (if they throw exceptions also, then you will have to embed a try-catch block within the finally block.
i.e.
try {
method1();
}catch (Exception e){
}finally{
try{
method2();
}catch (Exception e){
}
....
Another way is to set a boolean flag when a method is called.
i.e.
boolean method1Done = false;
...
boolean method4Done = false;
boolean run = true;
while (run){
try{
if (!method1Done){
method1Done = true;
method1();
}
if (!method2Done){
method2Done = true;
method2();
}
if (!method3Done){
method3Done = true;
method3();
}
if (!method4Done){
method4Done = true;
method4();
}
run = false;
}
}
This way each time the loop gets thrown out because of an exception, the if statement will tell the execution path to skip all methods up to and including the one that threw the exception.
Kyle
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The obvious solution to your example would be to put the try-catch-block into method2.

I suppose this doesn't fit your real world problem, so please provide more inside to enhance our understanding of your problem and therefore our ability to help you.
Regards, Ilja
[This message has been edited by Ilja Preuss (edited July 31, 2001).]
 
Ragesh Menon
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is the best Way I can think off, if U want all the methods to be executed what ever the case may be..
Simply
Catch the exception.. Ignore it...

private void go()
{
try
{
method1();
}
catch(Exception ignore1)
{
System.out.println("Exception in Method1"+e)
System.out.println("Continuing with Method2");
}
try
{
method2();
}
catch(Exception ignore2)
{
System.out.println("Exception in Method2"+e)
System.out.println("Continuing with Method3");
}
try
{
method3();
}
catch(Exception ignore3)
{
System.out.println("Exception in Method3"+e)
System.out.println("Continuing with Method4");
}
try
{
method4();
}
catch(Exception ignore4)
{
System.out.println("Exception in Method4"+e)
System.out.println("Continuing ...");
}
}

------------------
 
Paul Keohan
Ranch Hand
Posts: 411
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It looks like I have to do something like this. I was trying to avoid having to many try/catch blocks but it looks like I'll need one for each method call.
Thanks everyone.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, how about the following:

Granted, it's not much shorter, but at least you don't duplicate the logic to handle the exception.
Sigh, this is much easier in Smalltalk...

[This message has been edited by Ilja Preuss (edited July 31, 2001).]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic