Java 使用 Object 和 Driver 自定义异常处理 class

Java Custom Exception handling with Object and Driver class

我在与创建书籍的程序进行交互时遇到了自定义书籍异常的问题 object 为此,我最终也与我的 driver class [=38= 进行了交互].我的 driver class 没有发现不一致的发生。喜欢:

当我运行我的driverclassBookStore.java时,它没有捕捉到我在book.java.[=17中写的上述错误=]

--------------------------------创建书籍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;
}
}

}

-------------------------------------------- --自定义书籍例外

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

非常感谢您的帮助和提示!

public Book (String title, int isbn, int quantity)throws BookException{     
//constructors

this.title = title;
this.isbn = isbn;
this.quantity = quantity;

}

只是在那里添加一个 throws 子句,不会成功。你也需要真正扔掉它们。例如,如果标题不能为 null 或为空:

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;

}

你的构造函数总是通过。变化自

this.title = title;
this.isbn = isbn;
this.quantity = quantity;

setTitle(title);
setIsbn(isbn);
setQuantity(quantity);

Murat.K 已经提供了一个 我不再重复。

不过,我将提供一些额外的建议。首先,Java 已经有一个处理非法/无效参数的异常,称为 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,这意味着您不需要在方法签名或 catch 阻止(除非你想抓住它)。

如果您仍想使用自己的异常,则不需要 message 字段及其 setter 和 getter,因为父 Exception class 已经有了它们(好吧,除了 setter,但它不是必需的,因为您无论如何都在构造函数中设置消息)。所以你的 class 应该是这样的

public BookException extends Exception {
    public BookException(String message) {
        super(message);
    }
}

你可以像

那样把它放在一行中
throw new BookException("some error"); //no need for setter