aspose file tools*
The moose likes Beginning Java and the fly likes passing control between classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "passing control between classes" Watch "passing control between classes" New topic
Author

passing control between classes

le taylom
Ranch Hand

Joined: Dec 01, 2000
Posts: 44
Hi guys,
I have 3 classes, a client, a component that takes something from the client and an exception class.
The client instantiates the component and passes it a string. The component checks to see if the string is null, if it is it calls the exception class with the error.
I want the exception class to pass control back to the client with an error message and for execution to end here (i.e. the component class does not get back control from the exception).
Therefore in the example below I do not want the doOther() mehtod to be called if the string is null.
How do I do this?
My simple test classes are listed below:
client class
***********************************************
package mailtest;
public class MailTest {
private String string = null;
public static void main(String[] args) {
MailTest myTest = new MailTest();
myTest.doTest();
}
private void doTest() {
Tester2 myTester = new Tester2(string);
}
}
*****************************************************
component class
*****************************************************
package mailtest;
public class Tester2 {
private String string = null;
public Tester2(String string) {
this.string = string;
testTheString(string);
doOther();
}
private void testTheString(String string) {
if (string == null) {
MyException myE = new MyException();
}
}
private void doOther() {
System.out.println("in do other!!!");
}
}
******************************************************
exception class
*******************************************************
package mailtest;
public class MyException {
public MyException() {
System.out.println("in exception");
}
}
****************************************************
All help will be greatly appreciated.
T
le taylom
Ranch Hand

Joined: Dec 01, 2000
Posts: 44
is an answer for me to write my own exception class, to throw and catch it? will doing this pass control back to the calling method?
William Barnes
Ranch Hand

Joined: Mar 16, 2001
Posts: 986

Yes.
When you "throw" an exception it will keep going back until it is "caught".
Does that make sense?


Please ignore post, I have no idea what I am talking about.
le taylom
Ranch Hand

Joined: Dec 01, 2000
Posts: 44
Yes, yes, yes.....
Now I get it.
I place the try/catch block around the method where my component class is constructed:
try {
Tester2 myTester = new Tester2(string);
}
catch (MyException me) {
System.out.println(me);]
Then my constructor in the component class must throw the correct exception type:
public Tester2(String string) throws MyException {
this.string = string;
testTheString(string);
doOther();
}
As must the method IT is calling:
private void testTheString(String string) throws MyException{
if (string == null) throw new MyException("failure");
}
Then when the exception is thrown it propogates back up to the calling class, i.e. the client class, meaning my other method call (doOther() never gets called - which is what I want.
Phew, all sorted and I at last understand exceptions!!
Thanks
T
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: passing control between classes