Julio
Julio

Reputation: 1855

Disrupting the flow of a program if an error is caught?

Ok I have a method that kicks off an object that deasl with data - a kind of factory.

The factory splits data and sends the data that is split to custom objects that further process/refine the data.

My problem is I log errors with an object that basically just appends the strings together into a log of error. Some errors are ok - meaning the flow of program can continue - however some are serious and therefore the process needs to terminate and I need to throw the error log back to the original method. And stop processing the data at that point or it could mess things up.

The original method returns a string you see. I don't have to worry about how the method returns the error just need to get it to it.

alt text

Thanks

Upvotes: 0

Views: 370

Answers (6)

Julio
Julio

Reputation: 1855

Ok this is what I wanted..finally figured it out.

4 classes including a custom exception.

package ExceptionTest;

public class Main {

    public static void main(String[] args) {
        exceptionTester();
    }

    private static void exceptionTester(){
        try{         
            new FirstLevelObj().begin();        
        }
        catch(MyException e){
            System.out.println("Its worked!");
            e.printStackTrace();
        }
        finally{
            System.out.println("Oh young man..This Class doth created the other classes! \nAnd has now thrown the exception!");
        }

    }

}

package ExceptionTest;

public class FirstLevelObj {

    private SecondLevelObj second;

    public FirstLevelObj() throws MyException{
    }

    protected void begin()throws MyException{
        try{
            second = new SecondLevelObj();
            second.start();
        }
        catch(MyException e){
            throw new MyException("This Is The One!");
        }
        finally{
            System.out.println("And finally..");
        }
    }

}

package ExceptionTest;

public class SecondLevelObj {

    public SecondLevelObj(){

    }

    protected void start() throws MyException{
        for(int i = 0; i<10; i ++){
            if(i == 6){
                System.out.println("Exception should be thrown:");
                throw new MyException("An Error Hath Occurred Young Man!");
            }
            else{
                System.out.println(i);
            }
        }
    }
}

package ExceptionTest;

public class MyException extends Exception{
    public MyException(String s){
        super(s);
    }
}

Upvotes: 0

Robb
Robb

Reputation: 3851

It sounds like you should be throwing an exception when you hit a serious error and have the call to the factory within a try catch statement to handle the errors it can generate.

Have a look here http://download.oracle.com/javase/tutorial/essential/exceptions/throwing.html for more information.

The basic code would be

Exception e = new Exception();
throw e;

However you could look at creating your own exception class to contain more information about the specific error caused.

Edit: You mention having an error log to return, a custom exception could have this included within it.

Double Edit: Somthing like

public class BadFactoryException extends Exception{
    private String log;
    public BadFactoryException(String log){
        this.log = log
    }
}

With the factory method that can throw it being something like

public returntype factory throws BadFactoryException(input){
    try{
    //code goes here
    }catch(Exception e){
         throw new BadFactoryExeption(log);
    }       

}

Upvotes: 1

Vladimir Ivanov
Vladimir Ivanov

Reputation: 43098

Basic tutorial on exceptions: http://download.oracle.com/javase/tutorial/essential/exceptions/

And another good article: http://www.javaworld.com/javaworld/jw-07-1998/jw-07-exceptions.html

If you want to devide critical situations of non critical just do this:

try {
// get here all your work
} catch (TerribleException e) {
// log and exit the application
} 

and in your work just don't throw any exceptions. Simple log the situation and continue the work. So all you need is to define what situations should stop the execution, then throw some Exception in that place and catch it in the class which launches the work.

Upvotes: 0

Blender
Blender

Reputation: 298156

I'm not a Java person, but I think that you need to use a try {...} catch (error) {...} block. When the desired error is caught, run System.exit(). Here's an example:

try {
  /* Do your stuff here */
} catch (ExceptionType name) {
  /* Oh noes, a fatal error! */
  print('Oh noes!');
  System.exit()
}

Is this what you were looking for?

Upvotes: 0

Gareth
Gareth

Reputation: 138042

You don't mention Exceptions in your question - do you use them already?

If not, this is exactly what they are made for.

If you do, then you need to rethink how you are catching the exceptions for your logging.

Upvotes: 0

Adam Wright
Adam Wright

Reputation: 49376

You've just described exceptions and exception handling, a feature of Java since day one.

Upvotes: 0

Related Questions