| | |
Creating and Using Exceptions
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Sep 2008
Posts: 22
Reputation:
Solved Threads: 1
How can I use the Exceptions I have created below in the Cinema class?
//IllegalAgeException class
//MoviePropertiesNotSetException class
//Cinema class
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.
//IllegalAgeException class
Java Syntax (Toggle Plain Text)
public class IllegalAgeException extends Exception { /** * Creates a new instance of <code>IllegalAgeException</code> without detail message. */ String exception; public IllegalAgeException() { exception="Unknown"; } /** * Constructs an instance of <code>IllegalAgeException</code> with the specified detail message. * @param msg the detail message. */ public IllegalAgeException(String msg) { super(msg); this.exception=msg; } public String getException() { return this.exception; } }
//MoviePropertiesNotSetException class
Java Syntax (Toggle Plain Text)
public class MoviePropertiesNotSetException extends Exception { /** * Creates a new instance of <code>MoviePropertiesNotSetException</code> without detail message. */ String exception; public MoviePropertiesNotSetException() { exception="Unknown"; } /** * Constructs an instance of <code>MoviePropertiesNotSetException</code> with the specified detail message. * @param msg the detail message. */ public MoviePropertiesNotSetException(String msg) { super(msg); this.exception=msg; } public String getException() { return this.exception; } }
//Cinema class
Java Syntax (Toggle Plain Text)
import java.util.*; public class Cinema { float cost; String Title; String mrPG="PG"; String mrA="A"; String mrG="G"; Movie movieNew = new Movie(); //MovieRating class MovieRating mrating = new MovieRating(); private ArrayList<Human> hm = new ArrayList(); public Cinema() { } public Cinema (String mtitle, float pounds) { this.Title=mtitle; this.cost = pounds; } public float getcost (int people) { return cost * people; } /** * Method check that the movie title and rating have been set, * if not throw an exception. */ public void setMovie(Movie movieNew) throws MyException { if(movieNew.getTitle()!=null && movieNew.getRating()!=null) { System.out.println("The movie is called " +movieNew.getTitle()+ " and is rated " +movieNew.getRating()+ "."); } else { throw new MyException("The Movie properties not set"); } } /* * this method first checks if the movie properties are set. */ public void showMovie() throws MyException { // movieNew.setName("Comedy"); // movieNew.setRating("PG"); // movieNew.setTitle("Mr Bin"); if(movieNew.getTitle()!=null || movieNew.getRating()!=null) { System.out.println("Now Showing... "+movieNew.getTitle()); } else { throw new MyException ("The movie properties are not set."); } } /*Checks if Movie properties are set, if not throws MyException * takes a collection of Human - Adult and Child */ public void addMovieGoers(Human h) throws MyException { Human a = new Adult("Joe",43); Human b = new Adult("Sue",39); Human c = new Adult("Tracy",20); Human d = new Child("Sammy",17); Human e = new Child("Julie",12); Human f = new Child("Mona",6); hm.add(a);hm.add(b);hm.add(c);hm.add(d); hm.add(e);hm.add(f); hm.trimToSize(); String toPrint = ""; if(movieNew.getRating()!=null && movieNew.getTitle()!=null) { if(mrPG.equals(movieNew.getRating())) //movies rated PG { System.out.println("The following can only watch under parental guidance:"); for(int i = 0; i < hm.size(); i++) { Human test = (Human)hm.get(i); if ( test instanceof Child) { if(toPrint.equals("")) { System.out.println(test); } else { System.out.println("Children: " + toPrint); } } } } else if(mrA.equals(movieNew.getRating())) //movies rated Adult { System.out.println("The following can watch..."); for ( int i = 0; i < hm.size(); i++) { Human test = (Human)hm.get(i); if ( test instanceof Adult) { if(toPrint.equals("")) { System.out.println(test); } else { System.out.println("Adults: " + toPrint); } } } } else //Movies rated G { System.out.println("This is movie rated G. So the following can watch"+ ": "+hm+""); } } else { throw new MyException("The Movie properties are not set."); } } public static void main(String args[]) throws MyException { Cinema cn = new Cinema(); Human h = new Human(); //instantiating and setting properties to the movie Movie movieNew = new Movie(); movieNew.setName("Comedy"); movieNew.setRating("PG"); movieNew.setTitle("Mr Bin"); try { if(movieNew.getRating()!=null && movieNew.getTitle()!=null) { cn.showMovie(); } } catch(MoviePropertiesNotSetException msg) { System.out.println("Nothing"); } // System.out.println("Setting Movie properties:"); // cn.setMovie(movieNew); // // System.out.println("Current Show:"); // cn.showMovie(); // // System.out.println("Movie Goers are: "); // cn.addMovieGoers(h); } }
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.
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
How to use it:
Whenever something bad happens that it is wrong and you don't want it to happen call:
AND
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)
public class IllegalAgeException extends Exception { /** * Creates a new instance of <code>IllegalAgeException</code> without detail message. */ //String exception; public IllegalAgeException() { super(); //exception="Unknown"; } /** * Constructs an instance of <code>IllegalAgeException</code> with the specified detail message. * @param msg the detail message. */ public IllegalAgeException(String msg) { super(msg); //this.exception=msg; } /* public String getException() { return this.exception; } */ }
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)
public void setAge(int age) throws IllegalAgeException { if (age<=0) throw new IllegalAgeException("Age: "+age+" must be positive"); this.age = age; }
Java Syntax (Toggle Plain Text)
YourClass obj = new YourClass(); try { obj.setAge(10); } catch (IllegalAgeException iae) { System.out.println(iae.getMessage()); }
Check out my New Bike at my Public Profile at the "About Me" tab
•
•
Join Date: Sep 2008
Posts: 22
Reputation:
Solved Threads: 1
Thanks for that. But like my addMovieGoers(Human h) method
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?
Java Syntax (Toggle Plain Text)
public void addMovieGoers(Human h) throws IllegalAgeException { }
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?
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
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
•
•
Join Date: Sep 2008
Posts: 22
Reputation:
Solved Threads: 1
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)
/** * Method check that the movie title and rating have been set, * if not throw MoviePropertiesNotSetException. */ public void showMovie() throws MoviePropertiesNotSetException { movieNew.setName("Comedy"); movieNew.setRating("PG"); movieNew.setTitle("Mr Bin"); if(movieNew.getTitle()!=null || movieNew.getRating()!=null) { System.out.println("Now Showing... "+movieNew.getTitle()); } else { throw new MoviePropertiesNotSetException ("The movie properties are not set."); } }
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
•
•
Join Date: Oct 2008
Posts: 24
Reputation:
Solved Threads: 0
okay, I've modified my Cinema class to look like this
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.
Java Syntax (Toggle Plain Text)
import java.util.*; public class Cinema { float cost; String Title; String mrPG="PG"; String mrA="A"; String mrG="G"; Movie movieNew = new Movie(); //MovieRating class MovieRating mrating = new MovieRating(); private ArrayList<Human> hm = new ArrayList(); public Cinema() { } public Cinema (String mtitle, float pounds) { this.Title=mtitle; this.cost = pounds; } public float getcost (int people) { return cost * people; } /** * Method check that the movie title and rating have been set, * if not throw MoviePropertiesNotSetException. */ public void setMovie(Movie movieNew) throws MoviePropertiesNotSetException { if(movieNew.getTitle()!=null && movieNew.getRating()!=null) { System.out.println("The movie is called " +movieNew.getTitle()+ " and is rated " +movieNew.getRating()+ "."); } else { throw new MoviePropertiesNotSetException("The Movie properties not set"); } } /** * Method check that the movie title and rating have been set, * if not throw MoviePropertiesNotSetException. */ public void showMovie() throws MoviePropertiesNotSetException { movieNew.setName("Comedy"); movieNew.setRating("G"); movieNew.setTitle("Mr Bin"); if(movieNew.getTitle()!=null || movieNew.getRating()!=null) { System.out.println("Now Showing... "+movieNew.getTitle()); } else { throw new MoviePropertiesNotSetException ("The movie properties are not set."); } } /**Checks if Movie properties are set, if not throws MyException * takes a collection of Human - Adult and Child */ public void addMovieGoers(Human h) throws IllegalAgeException { Human a = new Adult("Joe",43); Human b = new Adult("Sue",39); Human c = new Adult("Tracy",20); Human d = new Child("Sammy",17); Human e = new Child("Julie",12); Human f = new Child("Mona",6); hm.add(a);hm.add(b);hm.add(c);hm.add(d); hm.add(e);hm.add(f); hm.trimToSize(); String toPrint = ""; if(movieNew.getRating()!=null && movieNew.getTitle()!=null) { if(mrPG.equals(movieNew.getRating())) //movies rated PG { System.out.println("The following can only watch under parental guidance:"); for(int i = 0; i < hm.size(); i++) { Human test = (Human)hm.get(i); if ( test instanceof Child) { if(toPrint.equals("")) { System.out.println(test); } else { System.out.println("Children: " + toPrint); } } } } else if(mrA.equals(movieNew.getRating())) //movies rated Adult { System.out.println("The following can watch..."); for ( int i = 0; i < hm.size(); i++) { Human test = (Human)hm.get(i); if ( test instanceof Adult) { if(toPrint.equals("")) { System.out.println(test); } else { System.out.println("Adults: " + toPrint); } } } } else if(mrG.equals(movieNew.getRating()))//Movies rated G { System.out.println("This is movie rated G. So the following can watch"+ ": "+hm+""); } else { throw new IllegalAgeException("You may not be old enough to watch a movie."); } } } public static void main(String args[]) throws MoviePropertiesNotSetException, IllegalAgeException { Cinema cn = new Cinema(); Human h = new Human(); try { //instantiating and setting properties to the movie Movie movieNew = new Movie(); movieNew.setName("Comedy"); movieNew.setRating("G"); movieNew.setTitle("Mr Bin"); cn.setMovie(movieNew); cn.showMovie(); cn.addMovieGoers(h); } catch(MoviePropertiesNotSetException exp) { System.out.println(exp.getMessage()); } catch(IllegalAgeException msg) { System.out.println(msg.getMessage()); } finally { System.out.println("Movie is over! Have a good day."); } } }
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.
This is wrong:
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:
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
Java Syntax (Toggle Plain Text)
public void showMovie() throws MoviePropertiesNotSetException { movieNew.setName("Comedy"); movieNew.setRating("G"); movieNew.setTitle("Mr Bin"); if(movieNew.getTitle()!=null || movieNew.getRating()!=null) { System.out.println("Now Showing... "+movieNew.getTitle()); } else { throw new MoviePropertiesNotSetException ("The movie properties are not set."); } }
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)
public void showMovie() throws MoviePropertiesNotSetException { if ((movieNew!=null) && (movieNew.getTitle()!=null) && (movieNew.getRating()!=null)) { System.out.println("Now Showing... "+movieNew.getTitle()); } else { throw new MoviePropertiesNotSetException ("The movie properties are not set."); } }
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
•
•
Join Date: Oct 2008
Posts: 24
Reputation:
Solved Threads: 0
•
•
•
•
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
![]() |
Similar Threads
- Updated : Simple ASP.Net Login Page (ASP.NET)
- Creating a Library (C++)
- creating an instant messenger program in java (Java)
- help with creating and calling a function (C++)
- OutOfMemory Error (Java)
- weird exceptions:( (Python)
- Creating a .EXE file (Java)
- Playing with RMI in Tiger (Java)
- Cannot get CF set up on server (ColdFusion)
- creating a frame with four clocks (Java)
Other Threads in the Java Forum
- Previous Thread: joining 2 database tables in a single query instead of arraying it and making a mess
- Next Thread: Java lang exception thrown
| Thread Tools | Search this Thread |
Tag cloud for Java
affinetransform android api apple applet application arc arguments array arrays automation binary bluetooth businessintelligence chat class classes client code component database desktop draw ebook eclipse encode equation error event exception file fractal game givemetehcodez graphics gui helpwithhomework html ide image input integer intersect j2me java javaexcel javaprojects jmf jni jpanel julia linked linux list loop mac main map method methods mobile netbeans newbie number online open-source oracle parameter print problem program programming project properties recursion reference replaysolutions rotatetext scanner score screen scrollbar server set size sms socket sort sql string superclass swing template test threads time tree windows working xstream






