问题
I am having trouble with my custom book exception to interact with my program that creates a book object and for that too finally interact with my driver class Bookstore.java. My driver class doesn't catch the inconsistencies from happening. Like:
- title should not be blank or contain blanks only
- isbn should be a number between 1000 and 10000 (inclusive)
- quantity should not be negative (zero is ok, it means out of stock)
When I run my driver class BookStore.java, it does not catch the above errors that I have written in book.java.
----------------------------------Creates book Object
public class Book{
//instance variables
private String title = "";
private int isbn;
private int quantity;
public Book (String title, int isbn, int quantity)throws BookException{
//constructors
this.title = title;
this.isbn = isbn;
this.quantity = quantity;
}
public String toString( ){ //toString Method
String s = "";
s = s + "Title: " + this.title + "\nISBN: " + this.isbn
+ "\nQuantity: " + this.quantity + "\n";
return s;
}
public String gettitle( ){
return this.title;
}
public int getisbn( ){
return this.isbn;
}
public int getquantity( ){
return this.quantity;
}
//mutator methods
public void settitle(String newtitle )throws Exception{
if(newtitle.length()<1){
BookException be = new BookException( );
be.setMessage("Title cannot be blank");
throw be;
}
else{
this.title=newtitle;
}
}
public void setisbn(int newisbn)throws Exception{
if(newisbn>=1000 && newisbn>=10000){
this.isbn = newisbn;
}
else{
BookException be = new BookException( );
be.setMessage("ISBN should be between 1000 and 10000.");
throw be;
}
}
public void setquantity(int newquantity)throws Exception{
if(newquantity>=0){
this.quantity = newquantity;
}
else{
BookException be = new BookException( );
be.setMessage("Quantity can't be a negative number.");
throw be;
}
}
}
-----------------------------------------------Custom Book Exception
public class BookException extends Exception{
//instance variable
private String message = "";
public BookException( ){
//empty constructor
}
public void setMessage(String newMessage){
this.message = newMessage;
}
public String getMessage( ){
return this.message;
}
}
------------------------------------------------------ Driver Class
import java.io.*;
import java.util.*;
public class Bookstore{
//this program will read the information for one book
//it will validate it and print it if correct
public static void main(String arg[ ]) throws Exception{
Scanner sc = new Scanner(System.in);
int size = 3;
int isbn=0;
int quantity = 0;
String title = "";
int count=0;
boolean exit = false;
Book oneBook;
try{
System.out.print("Enter title: ");
title = sc.nextLine( );
sc = new Scanner(System.in);
System.out.println();
System.out.print("Enter isbn: ");
isbn = sc.nextInt( );
sc = new Scanner(System.in);
System.out.println();
System.out.print("Enter quantity: ");
quantity = sc.nextInt( );
sc = new Scanner(System.in);
System.out.println();
oneBook = new Book(title, isbn, quantity); //attempting to create the book
//if any information about the book is inconsistent the BookExcpetion will be
//thrown by the Book constructor/set methods and it should be caught
System.out.println("The book entered was:");
System.out.println(oneBook.toString( ));
}
catch(InputMismatchException ime){
System.out.println("you did not enter a number");
}
catch (BookException be){
System.out.println(be.getMessage( )); //calling the getMessage from BookException.java
}
} //main method
} //class
I would really appreciate the help and tips!
回答1:
Your constructor always passes. Change from
this.title = title;
this.isbn = isbn;
this.quantity = quantity;
to
setTitle(title);
setIsbn(isbn);
setQuantity(quantity);
回答2:
public Book (String title, int isbn, int quantity)throws BookException{
//constructors
this.title = title;
this.isbn = isbn;
this.quantity = quantity;
}
just adding a throws clause there, won't make it so. You'll need to actually throw them too. For instance, if title may not be null or empty:
public Book (String title, int isbn, int quantity)throws BookException{
//constructors
this.title = title;
if ( title == null || title.isEmpty())
throw new BookException("Please provide a title");
this.isbn = isbn;
this.quantity = quantity;
}
回答3:
Murat.K already provided an answer that I am not going to repeat.
I am however going to give some some extra advice. First of all, Java already has an exception that deals with illegal / invalid arguments, and that's called IllegalArgumentException. You should really use it and not create your own exception that's supposed to be doing the same thing. Also, note that IllegalArgumentException
is an unchecked exception, meaning you don't need a throws IllegalArgumentException
in the method signature or in a catch
block (unless you want to catch it).
If you still want to use your own exception, there's no need for the message
field and its setter and getter, because the parent Exception
class already has them (well, except for the setter, but it's not needed, as you set the message in the constructor anyway). So your class should look like this
public BookException extends Exception {
public BookException(String message) {
super(message);
}
}
And you can throw it on one line like
throw new BookException("some error"); //no need for setter
来源:https://stackoverflow.com/questions/29938646/java-custom-exception-handling-with-object-and-driver-class