my dog learned polymorphism*
The moose likes Java in General and the fly likes Catch exceptions (where?) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Catch exceptions (where?)" Watch "Catch exceptions (where?)" New topic
Author

Catch exceptions (where?)

Paul Keohan
Ranch Hand

Joined: Mar 15, 2000
Posts: 411
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

Joined: May 10, 2001
Posts: 3244
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


Dave
Paul Keohan
Ranch Hand

Joined: Mar 15, 2000
Posts: 411
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

Joined: Jul 29, 2001
Posts: 64
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

Joined: Jul 11, 2001
Posts: 14112
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).]


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Ragesh Menon
Greenhorn

Joined: Jul 02, 2001
Posts: 6
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

Joined: Mar 15, 2000
Posts: 411
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

Joined: Jul 11, 2001
Posts: 14112
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).]
 
Don't get me started about those stupid light bulbs.
 
subject: Catch exceptions (where?)
 
Similar Threads
Downcasting
Method Overriding
How to identify exception source?
Problem -- Virtual Method Invocation
exception declaration