Creating and Using Exceptions

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Sep 2008
Posts: 22
Reputation: janamrob is an unknown quantity at this point 
Solved Threads: 1
janamrob janamrob is offline Offline
Newbie Poster

Creating and Using Exceptions

 
0
  #1
Nov 3rd, 2008
How can I use the Exceptions I have created below in the Cinema class?

//IllegalAgeException class
  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
  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
  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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,713
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 229
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Posting Virtuoso

Re: Creating and Using Exceptions

 
0
  #2
Nov 3rd, 2008
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:

  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:

  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
  1. YourClass obj = new YourClass();
  2. try {
  3. obj.setAge(10);
  4. } catch (IllegalAgeException iae) {
  5. System.out.println(iae.getMessage());
  6. }
Check out my New Bike at my Public Profile at the "About Me" tab
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 22
Reputation: janamrob is an unknown quantity at this point 
Solved Threads: 1
janamrob janamrob is offline Offline
Newbie Poster

Re: Creating and Using Exceptions

 
0
  #3
Nov 3rd, 2008
Thanks for that. But like my addMovieGoers(Human h) method
  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?
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,713
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 229
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Posting Virtuoso

Re: Creating and Using Exceptions

 
0
  #4
Nov 3rd, 2008
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
Check out my New Bike at my Public Profile at the "About Me" tab
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 22
Reputation: janamrob is an unknown quantity at this point 
Solved Threads: 1
janamrob janamrob is offline Offline
Newbie Poster

Re: Creating and Using Exceptions

 
0
  #5
Nov 3rd, 2008
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?
  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. }
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,713
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 229
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Posting Virtuoso

Re: Creating and Using Exceptions

 
0
  #6
Nov 3rd, 2008
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
Check out my New Bike at my Public Profile at the "About Me" tab
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 24
Reputation: Achupa is an unknown quantity at this point 
Solved Threads: 0
Achupa Achupa is offline Offline
Newbie Poster

Re: Creating and Using Exceptions

 
0
  #7
Nov 3rd, 2008
okay, I've modified my Cinema class to look like this
  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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,713
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 229
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Posting Virtuoso

Re: Creating and Using Exceptions

 
0
  #8
Nov 3rd, 2008
This is wrong:
  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:

  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
Check out my New Bike at my Public Profile at the "About Me" tab
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 24
Reputation: Achupa is an unknown quantity at this point 
Solved Threads: 0
Achupa Achupa is offline Offline
Newbie Poster

Re: Creating and Using Exceptions

 
0
  #9
Nov 4th, 2008
Originally Posted by javaAddict View Post
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?
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 1,637
Reputation: BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold 
Solved Threads: 206
BestJewSinceJC BestJewSinceJC is offline Offline
Posting Virtuoso

Re: Creating and Using Exceptions

 
0
  #10
Nov 4th, 2008
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC