943,917 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 964
  • Java RSS
You are currently viewing page 1 of this multi-page discussion thread
Nov 3rd, 2008
0

Creating and Using Exceptions

Expand Post »
How can I use the Exceptions I have created below in the Cinema class?

//IllegalAgeException class
Java Syntax (Toggle Plain Text)
  1. public class IllegalAgeException extends Exception
  2. {
  3. /**
  4.   * Creates a new instance of <code>IllegalAgeException</code> without detail message.
  5.   */
  6. String exception;
  7. public IllegalAgeException()
  8. {
  9. exception="Unknown";
  10. }
  11. /**
  12.   * Constructs an instance of <code>IllegalAgeException</code> with the specified detail message.
  13.   * @param msg the detail message.
  14.   */
  15. public IllegalAgeException(String msg)
  16. {
  17. super(msg);
  18. this.exception=msg;
  19. }
  20. public String getException()
  21. {
  22. return this.exception;
  23. }
  24. }

//MoviePropertiesNotSetException class
Java Syntax (Toggle Plain Text)
  1. public class MoviePropertiesNotSetException extends Exception
  2. {
  3.  
  4. /**
  5.   * Creates a new instance of <code>MoviePropertiesNotSetException</code> without detail message.
  6.   */
  7. String exception;
  8. public MoviePropertiesNotSetException()
  9. {
  10. exception="Unknown";
  11. }
  12. /**
  13.   * Constructs an instance of <code>MoviePropertiesNotSetException</code> with the specified detail message.
  14.   * @param msg the detail message.
  15.   */
  16. public MoviePropertiesNotSetException(String msg)
  17. {
  18. super(msg);
  19. this.exception=msg;
  20. }
  21. public String getException()
  22. {
  23. return this.exception;
  24. }
  25. }

//Cinema class
Java Syntax (Toggle Plain Text)
  1. import java.util.*;
  2. public class Cinema
  3. {
  4. float cost;
  5. String Title;
  6.  
  7. String mrPG="PG";
  8. String mrA="A";
  9. String mrG="G";
  10.  
  11. Movie movieNew = new Movie();
  12.  
  13. //MovieRating class
  14. MovieRating mrating = new MovieRating();
  15. private ArrayList<Human> hm = new ArrayList();
  16.  
  17. public Cinema()
  18. {
  19.  
  20. }
  21. public Cinema (String mtitle, float pounds)
  22. {
  23. this.Title=mtitle;
  24. this.cost = pounds;
  25. }
  26.  
  27. public float getcost (int people)
  28. {
  29. return cost * people;
  30. }
  31.  
  32. /**
  33.   * Method check that the movie title and rating have been set,
  34.   * if not throw an exception.
  35.   */
  36. public void setMovie(Movie movieNew) throws MyException
  37. {
  38. if(movieNew.getTitle()!=null && movieNew.getRating()!=null)
  39. {
  40. System.out.println("The movie is called " +movieNew.getTitle()+
  41. " and is rated " +movieNew.getRating()+ ".");
  42. }
  43. else
  44. {
  45. throw new MyException("The Movie properties not set");
  46. }
  47. }
  48. /*
  49.   * this method first checks if the movie properties are set.
  50.   */
  51. public void showMovie() throws MyException
  52. {
  53. // movieNew.setName("Comedy");
  54. // movieNew.setRating("PG");
  55. // movieNew.setTitle("Mr Bin");
  56. if(movieNew.getTitle()!=null || movieNew.getRating()!=null)
  57. {
  58. System.out.println("Now Showing... "+movieNew.getTitle());
  59. }
  60. else
  61. {
  62. throw new MyException ("The movie properties are not set.");
  63. }
  64. }
  65. /*Checks if Movie properties are set, if not throws MyException
  66.   * takes a collection of Human - Adult and Child
  67.   */
  68. public void addMovieGoers(Human h) throws MyException
  69. {
  70. Human a = new Adult("Joe",43);
  71. Human b = new Adult("Sue",39);
  72. Human c = new Adult("Tracy",20);
  73. Human d = new Child("Sammy",17);
  74. Human e = new Child("Julie",12);
  75. Human f = new Child("Mona",6);
  76. hm.add(a);hm.add(b);hm.add(c);hm.add(d);
  77. hm.add(e);hm.add(f);
  78.  
  79. hm.trimToSize();
  80. String toPrint = "";
  81.  
  82. if(movieNew.getRating()!=null && movieNew.getTitle()!=null)
  83. {
  84. if(mrPG.equals(movieNew.getRating())) //movies rated PG
  85. {
  86. System.out.println("The following can only watch under parental guidance:");
  87. for(int i = 0; i < hm.size(); i++)
  88. {
  89. Human test = (Human)hm.get(i);
  90. if ( test instanceof Child)
  91. {
  92. if(toPrint.equals(""))
  93. {
  94. System.out.println(test);
  95. }
  96. else
  97. {
  98. System.out.println("Children: " + toPrint);
  99. }
  100. }
  101. }
  102. }
  103. else if(mrA.equals(movieNew.getRating())) //movies rated Adult
  104. {
  105. System.out.println("The following can watch...");
  106. for ( int i = 0; i < hm.size(); i++)
  107. {
  108. Human test = (Human)hm.get(i);
  109. if ( test instanceof Adult)
  110. {
  111. if(toPrint.equals(""))
  112. {
  113. System.out.println(test);
  114. }
  115. else
  116. {
  117. System.out.println("Adults: " + toPrint);
  118. }
  119. }
  120.  
  121. }
  122. }
  123. else //Movies rated G
  124. {
  125. System.out.println("This is movie rated G. So the following can watch"+
  126. ": "+hm+"");
  127. }
  128. }
  129. else
  130. {
  131. throw new MyException("The Movie properties are not set.");
  132. }
  133. }
  134.  
  135. public static void main(String args[]) throws MyException
  136. {
  137. Cinema cn = new Cinema();
  138. Human h = new Human();
  139. //instantiating and setting properties to the movie
  140. Movie movieNew = new Movie();
  141. movieNew.setName("Comedy");
  142. movieNew.setRating("PG");
  143. movieNew.setTitle("Mr Bin");
  144. try
  145. {
  146. if(movieNew.getRating()!=null && movieNew.getTitle()!=null)
  147. {
  148. cn.showMovie();
  149.  
  150. }
  151. }
  152. catch(MoviePropertiesNotSetException msg)
  153. {
  154. System.out.println("Nothing");
  155. }
  156.  
  157. // System.out.println("Setting Movie properties:");
  158. // cn.setMovie(movieNew);
  159. //
  160. // System.out.println("Current Show:");
  161. // cn.showMovie();
  162. //
  163. // System.out.println("Movie Goers are: ");
  164. // cn.addMovieGoers(h);
  165.  
  166. }
  167. }

NOTE
Creating your own exceptions and passing them to the controlling module (here method with scenarios). After an exception occurs your code should stop execution of current scenario and go to another one.
I need to see several scenarios resulting with different exceptions being thrown before finally executing a final scenario without exceptions being thrown.
Similar Threads
Reputation Points: 10
Solved Threads: 1
Newbie Poster
janamrob is offline Offline
22 posts
since Sep 2008
Nov 3rd, 2008
0

Re: Creating and Using Exceptions

First of all the way you create your own exceptions is wrong. You don't need to create a String variables: exception.
The Exception class has a method: getMessage() which returns the description of the exception that occurred with everything you need to know. So you don't need the getException() method.
When you call super(msg); you set the message to be the String that you passed as argument, so when you call the getMessage() of your exception that argument will be returned:

Java Syntax (Toggle Plain Text)
  1. public class IllegalAgeException extends Exception
  2. {
  3. /**
  4.   * Creates a new instance of <code>IllegalAgeException</code> without detail message.
  5.   */
  6. //String exception;
  7. public IllegalAgeException()
  8. {
  9. super();
  10. //exception="Unknown";
  11. }
  12. /**
  13.   * Constructs an instance of <code>IllegalAgeException</code> with the specified detail message.
  14.   * @param msg the detail message.
  15.   */
  16. public IllegalAgeException(String msg)
  17. {
  18. super(msg);
  19. //this.exception=msg;
  20. }
  21. /*
  22.   public String getException()
  23.   {
  24.   return this.exception;
  25.   }
  26. */
  27. }

How to use it:

Whenever something bad happens that it is wrong and you don't want it to happen call:
throw new IllegalAgeException (); in a method. Then at the declararion of that method add: throws IllegalAgeException When you call that method, put it inside a try-catch and treat it as usual:

Java Syntax (Toggle Plain Text)
  1. public void setAge(int age) throws IllegalAgeException {
  2. if (age<=0) throw new IllegalAgeException("Age: "+age+" must be positive");
  3. this.age = age;
  4. }
AND
Java Syntax (Toggle Plain Text)
  1. YourClass obj = new YourClass();
  2. try {
  3. obj.setAge(10);
  4. } catch (IllegalAgeException iae) {
  5. System.out.println(iae.getMessage());
  6. }
Sponsor
Featured Poster
Reputation Points: 1014
Solved Threads: 446
Nearly a Senior Poster
javaAddict is offline Offline
3,259 posts
since Dec 2007
Nov 3rd, 2008
0

Re: Creating and Using Exceptions

Thanks for that. But like my addMovieGoers(Human h) method
Java Syntax (Toggle Plain Text)
  1. public void addMovieGoers(Human h) throws IllegalAgeException
  2. {
  3. }

I have an ArrayList with more than one age. How do I throw an IllegalAgeException for example for a 5 years old kid?

And: I have three methods setMovie(Movie movieNew) ; showMovie(); and addMovieGoers(Human h). How do I call them in that order in my main method?
Reputation Points: 10
Solved Threads: 1
Newbie Poster
janamrob is offline Offline
22 posts
since Sep 2008
Nov 3rd, 2008
0

Re: Creating and Using Exceptions

Before you add the argument h to the ArrayList do this:
If the age of the human is lower than 5 then throw a new IllegalAgeException


Call them in that order in the main method
Sponsor
Featured Poster
Reputation Points: 1014
Solved Threads: 446
Nearly a Senior Poster
javaAddict is offline Offline
3,259 posts
since Dec 2007
Nov 3rd, 2008
0

Re: Creating and Using Exceptions

and in the showMovie() method, is there a way I can check the movie properties in the movieNew object created in the main method without having to set the movie properties in the method?
Java Syntax (Toggle Plain Text)
  1. /**
  2.   * Method check that the movie title and rating have been set,
  3.   * if not throw MoviePropertiesNotSetException.
  4.   */
  5. public void showMovie() throws MoviePropertiesNotSetException
  6. {
  7. movieNew.setName("Comedy");
  8. movieNew.setRating("PG");
  9. movieNew.setTitle("Mr Bin");
  10. if(movieNew.getTitle()!=null || movieNew.getRating()!=null)
  11. {
  12. System.out.println("Now Showing... "+movieNew.getTitle());
  13. }
  14. else
  15. {
  16. throw new MoviePropertiesNotSetException ("The movie properties are not set.");
  17. }
  18. }
Reputation Points: 10
Solved Threads: 1
Newbie Poster
janamrob is offline Offline
22 posts
since Sep 2008
Nov 3rd, 2008
0

Re: Creating and Using Exceptions

The throw new MoviePropertiesNotSetException ("The movie properties are not set."); will be in the set methods. So when you create the movieNew object in main you will get an exception if the attributes are not set
Sponsor
Featured Poster
Reputation Points: 1014
Solved Threads: 446
Nearly a Senior Poster
javaAddict is offline Offline
3,259 posts
since Dec 2007
Nov 3rd, 2008
0

Re: Creating and Using Exceptions

okay, I've modified my Cinema class to look like this
Java Syntax (Toggle Plain Text)
  1. import java.util.*;
  2. public class Cinema
  3. {
  4. float cost;
  5. String Title;
  6.  
  7. String mrPG="PG";
  8. String mrA="A";
  9. String mrG="G";
  10.  
  11. Movie movieNew = new Movie();
  12.  
  13. //MovieRating class
  14. MovieRating mrating = new MovieRating();
  15. private ArrayList<Human> hm = new ArrayList();
  16.  
  17. public Cinema()
  18. {
  19.  
  20. }
  21. public Cinema (String mtitle, float pounds)
  22. {
  23. this.Title=mtitle;
  24. this.cost = pounds;
  25. }
  26.  
  27. public float getcost (int people)
  28. {
  29. return cost * people;
  30. }
  31.  
  32. /**
  33.   * Method check that the movie title and rating have been set,
  34.   * if not throw MoviePropertiesNotSetException.
  35.   */
  36. public void setMovie(Movie movieNew) throws MoviePropertiesNotSetException
  37. {
  38. if(movieNew.getTitle()!=null && movieNew.getRating()!=null)
  39. {
  40. System.out.println("The movie is called " +movieNew.getTitle()+
  41. " and is rated " +movieNew.getRating()+ ".");
  42. }
  43. else
  44. {
  45. throw new MoviePropertiesNotSetException("The Movie properties not set");
  46. }
  47. }
  48. /**
  49.   * Method check that the movie title and rating have been set,
  50.   * if not throw MoviePropertiesNotSetException.
  51.   */
  52. public void showMovie() throws MoviePropertiesNotSetException
  53. {
  54. movieNew.setName("Comedy");
  55. movieNew.setRating("G");
  56. movieNew.setTitle("Mr Bin");
  57. if(movieNew.getTitle()!=null || movieNew.getRating()!=null)
  58. {
  59. System.out.println("Now Showing... "+movieNew.getTitle());
  60. }
  61. else
  62. {
  63. throw new MoviePropertiesNotSetException ("The movie properties are not set.");
  64. }
  65. }
  66. /**Checks if Movie properties are set, if not throws MyException
  67.   * takes a collection of Human - Adult and Child
  68.   */
  69. public void addMovieGoers(Human h) throws IllegalAgeException
  70. {
  71. Human a = new Adult("Joe",43);
  72. Human b = new Adult("Sue",39);
  73. Human c = new Adult("Tracy",20);
  74. Human d = new Child("Sammy",17);
  75. Human e = new Child("Julie",12);
  76. Human f = new Child("Mona",6);
  77. hm.add(a);hm.add(b);hm.add(c);hm.add(d);
  78. hm.add(e);hm.add(f);
  79.  
  80. hm.trimToSize();
  81. String toPrint = "";
  82.  
  83. if(movieNew.getRating()!=null && movieNew.getTitle()!=null)
  84. {
  85. if(mrPG.equals(movieNew.getRating())) //movies rated PG
  86. {
  87. System.out.println("The following can only watch under parental guidance:");
  88. for(int i = 0; i < hm.size(); i++)
  89. {
  90. Human test = (Human)hm.get(i);
  91. if ( test instanceof Child)
  92. {
  93. if(toPrint.equals(""))
  94. {
  95. System.out.println(test);
  96. }
  97. else
  98. {
  99. System.out.println("Children: " + toPrint);
  100. }
  101. }
  102. }
  103. }
  104. else if(mrA.equals(movieNew.getRating())) //movies rated Adult
  105. {
  106. System.out.println("The following can watch...");
  107. for ( int i = 0; i < hm.size(); i++)
  108. {
  109. Human test = (Human)hm.get(i);
  110. if ( test instanceof Adult)
  111. {
  112. if(toPrint.equals(""))
  113. {
  114. System.out.println(test);
  115. }
  116. else
  117. {
  118. System.out.println("Adults: " + toPrint);
  119. }
  120. }
  121.  
  122. }
  123. }
  124. else if(mrG.equals(movieNew.getRating()))//Movies rated G
  125. {
  126. System.out.println("This is movie rated G. So the following can watch"+
  127. ": "+hm+"");
  128. }
  129. else
  130. {
  131. throw new IllegalAgeException("You may not be old enough to watch a movie.");
  132. }
  133. }
  134.  
  135.  
  136. }
  137.  
  138. public static void main(String args[]) throws MoviePropertiesNotSetException, IllegalAgeException
  139. {
  140. Cinema cn = new Cinema();
  141. Human h = new Human();
  142. try
  143. {
  144. //instantiating and setting properties to the movie
  145. Movie movieNew = new Movie();
  146. movieNew.setName("Comedy");
  147. movieNew.setRating("G");
  148. movieNew.setTitle("Mr Bin");
  149.  
  150. cn.setMovie(movieNew);
  151. cn.showMovie();
  152. cn.addMovieGoers(h);
  153. }
  154. catch(MoviePropertiesNotSetException exp)
  155. {
  156. System.out.println(exp.getMessage());
  157. }
  158. catch(IllegalAgeException msg)
  159. {
  160. System.out.println(msg.getMessage());
  161. }
  162. finally
  163. {
  164. System.out.println("Movie is over! Have a good day.");
  165. }
  166.  
  167. }
  168. }

But then i dont know if this condition is met:
Add several scenarios resulting with different exceptions being thrown before finally executing a final scenario without exceptions being thrown.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Achupa is offline Offline
24 posts
since Oct 2008
Nov 3rd, 2008
0

Re: Creating and Using Exceptions

This is wrong:
Java Syntax (Toggle Plain Text)
  1. public void showMovie() throws MoviePropertiesNotSetException
  2. {
  3. movieNew.setName("Comedy");
  4. movieNew.setRating("G");
  5. movieNew.setTitle("Mr Bin");
  6. if(movieNew.getTitle()!=null || movieNew.getRating()!=null)
  7. {
  8. System.out.println("Now Showing... "+movieNew.getTitle());
  9. }
  10. else
  11. {
  12. throw new MoviePropertiesNotSetException ("The movie properties are not set.");
  13. }
  14. }

The movie is already set by the setMovie method. And here you are going and changing what it was entered with fixed values: (Comedy,G,MR Bin). All you need to do is check if the movie attribute was set:

Java Syntax (Toggle Plain Text)
  1. public void showMovie() throws MoviePropertiesNotSetException
  2. {
  3. if ((movieNew!=null) && (movieNew.getTitle()!=null) && (movieNew.getRating()!=null))
  4. {
  5. System.out.println("Now Showing... "+movieNew.getTitle());
  6. }
  7. else
  8. {
  9. throw new MoviePropertiesNotSetException ("The movie properties are not set.");
  10. }
  11. }

You are making the same mistake with addMovieGoers. You are supposed to take the argument and add it to the ArrayList. The user calling the method will set which "humans" will "watch" the movie. And again you are using fixed values. It is as if the method has no meaning of existence since it makes no difference what the argument is since you don't use it
Sponsor
Featured Poster
Reputation Points: 1014
Solved Threads: 446
Nearly a Senior Poster
javaAddict is offline Offline
3,259 posts
since Dec 2007
Nov 4th, 2008
0

Re: Creating and Using Exceptions

Click to Expand / Collapse  Quote originally posted by javaAddict ...
You are making the same mistake with addMovieGoers. You are supposed to take the argument and add it to the ArrayList. The user calling the method will set which "humans" will "watch" the movie. And again you are using fixed values. It is as if the method has no meaning of existence since it makes no difference what the argument is since you don't use it
How do I do that, I'm a little green in java and your help so far is appreciated. Can you just show me how to do it?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Achupa is offline Offline
24 posts
since Oct 2008
Nov 4th, 2008
0

Re: Creating and Using Exceptions

Are you asking how to add something to an ArrayList? If so, and your ArrayList is called myList, use myList.add(item); where item is what you want to add to the ArrayList.
Reputation Points: 874
Solved Threads: 352
Posting Maven
BestJewSinceJC is offline Offline
2,758 posts
since Sep 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: joining 2 database tables in a single query instead of arraying it and making a mess
Next Thread in Java Forum Timeline: Remove node not working in code





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC