以下是从txt文件中读取项目并确定交易是否经过验证的代码。 我有一个问题,当它抛出异常格式的异常时,它跳出while循环,然后结束程序。我如何让它抛出异常,但之后继续循环。我确实看过,有人表示每个手柄有一个试块,我只是想知道是否有更好/更清洁更有效的方式。
/*
* @Author: Louis Krueger
* @Desc: Homework assignment #2 exception handling
* @Version: 1.1
*/
package paymentApplication;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.Scanner;
public class MainHandle {
public static ArrayList<String> saPaymentList = new ArrayList<String>();
public static void main(String[] args) {
boolean bSkipTrans = false;
String sFileName = "";
String sPaymentInfo = "";
String[] saPaymentInfo = null;
Scanner keyBoard = new Scanner(System.in);
ArrayList<Payment> alPayments = new ArrayList<Payment>();
try{
System.out.println("Enter the file name to be read: ");
sFileName = keyBoard.nextLine();
Scanner inputStream = new Scanner(new FileInputStream("details.txt"));
System.out.println("Test1");
//loop to Create arraylist of PaymentInfo objects
while(inputStream.hasNextLine()) {
bSkipTrans = false;
Payment Payment = new Payment();
sPaymentInfo = inputStream.nextLine();
saPaymentInfo = sPaymentInfo.split(";");
if(saPaymentInfo.length == 1){
bSkipTrans = true;
throw new InvalidTransactionException();
}else{
if(saPaymentInfo.length > 1){
if (isInteger(saPaymentInfo[1]) == true){
Payment.setiTransValue(Integer.parseInt(saPaymentInfo[1]));
}else{
//throw exception
bSkipTrans = true;
throw new InvalidTransactionException("Error#1");
}
}
if(saPaymentInfo.length > 4){
Payment.setsCreditNumber(saPaymentInfo[4]);
}
if(saPaymentInfo.length > 3){
Payment.setsDate(saPaymentInfo[3]);
}
if(saPaymentInfo.length > 2){
Payment.setsName(saPaymentInfo[2]);
}
if(saPaymentInfo.length > 0){
if(saPaymentInfo[0] != "Cash"|saPaymentInfo[0] != "Credit"){
//throws exception
Payment.setsTransType(saPaymentInfo[0]);
}else{
bSkipTrans = true;
throw new InvalidTransactionException("Error#2");
}
}
}
if(bSkipTrans == true){
//throw exception
bSkipTrans = true;
throw new InvalidTransactionException("Empty line");
}else{
System.out.println(Payment.toString());
System.out.println("Payment Added");
System.out.println("-------------");
alPayments.add(Payment);
}
}
inputStream.close();
}catch(InputMismatchException e){
System.out.println("Error 1 has occured: " + e.getMessage());
}catch (FileNotFoundException e) {
e.printStackTrace();
} catch (InvalidTransactionException e) {
System.out.println("InvalidTransactionException thrown");
}finally{
System.out.println("Finally statement reached");
keyBoard.close();
System.exit(0);
}
}
public static boolean isInteger(String s) {
try {
Integer.parseInt(s);
} catch(NumberFormatException e) {
return false;
}
// only got here if we didn't return false
return true;
}
}
答案 0 :(得分:0)
在while循环中放置一个try catch:
/*
* @Author: Louis Krueger
* @Desc: Homework assignment #2 exception handling
* @Version: 1.1
*/
package paymentApplication;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.Scanner;
public class MainHandle {
public static ArrayList<String> saPaymentList = new ArrayList<String>();
public static void main(String[] args) {
boolean bSkipTrans = false;
String sFileName = "";
String sPaymentInfo = "";
String[] saPaymentInfo = null;
Scanner keyBoard = new Scanner(System.in);
ArrayList<Payment> alPayments = new ArrayList<Payment>();
try{
System.out.println("Enter the file name to be read: ");
sFileName = keyBoard.nextLine();
Scanner inputStream = new Scanner(new FileInputStream("details.txt"));
System.out.println("Test1");
//loop to Create arraylist of PaymentInfo objects
while(inputStream.hasNextLine()) {
try{
bSkipTrans = false;
Payment Payment = new Payment();
sPaymentInfo = inputStream.nextLine();
saPaymentInfo = sPaymentInfo.split(";");
if(saPaymentInfo.length == 1){
bSkipTrans = true;
throw new InvalidTransactionException();
}else{
if(saPaymentInfo.length > 1){
if (isInteger(saPaymentInfo[1]) == true){
Payment.setiTransValue(Integer.parseInt(saPaymentInfo[1]));
}else{
//throw exception
bSkipTrans = true;
throw new InvalidTransactionException("Error#1");
}
}
if(saPaymentInfo.length > 4){
Payment.setsCreditNumber(saPaymentInfo[4]);
}
if(saPaymentInfo.length > 3){
Payment.setsDate(saPaymentInfo[3]);
}
if(saPaymentInfo.length > 2){
Payment.setsName(saPaymentInfo[2]);
}
if(saPaymentInfo.length > 0){
if(saPaymentInfo[0] != "Cash"|saPaymentInfo[0] != "Credit"){
//throws exception
Payment.setsTransType(saPaymentInfo[0]);
}else{
bSkipTrans = true;
throw new InvalidTransactionException("Error#2");
}
}
}
if(bSkipTrans == true){
//throw exception
bSkipTrans = true;
throw new InvalidTransactionException("Empty line");
}else{
System.out.println(Payment.toString());
System.out.println("Payment Added");
System.out.println("-------------");
alPayments.add(Payment);
}
} catch (InvalidTransactionException e) {
System.out.println("InvalidTransactionException thrown");
}
}
inputStream.close();
}catch(InputMismatchException e){
System.out.println("Error 1 has occured: " + e.getMessage());
}catch (FileNotFoundException e) {
e.printStackTrace();
finally{
System.out.println("Finally statement reached");
keyBoard.close();
System.exit(0);
}
}
public static boolean isInteger(String s) {
try {
Integer.parseInt(s);
} catch(NumberFormatException e) {
return false;
}
// only got here if we didn't return false
return true;
}
}