Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

passing control between classes

 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 1067
2
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
After some pecan pie, you might want to cleanse your palatte with this tiny ad:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic