This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

passing control between classes

 
le taylom
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 986
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes.
When you "throw" an exception it will keep going back until it is "caught".
Does that make sense?
 
le taylom
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic