了解 try &捕获和错误处理 [英] Understanding try & catch and error handling

查看:18
本文介绍了了解 try &捕获和错误处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

import java.util.Scanner;

public class Lab4_5 {
    public static void main(String[]args) {
        Scanner scan= new Scanner(System.in);

        int rows=0;
        int rowIndex=0, colIndex=0;
        boolean choice1= true;
        String y="y";
        String n="n";
        boolean first = true;

        while (choice1==true) {
            if (first==true) {  
                first=false;
                System.out.println("Do you want to start(Y/N): ");
            } else if (first==false) {
                System.out.println("Do you want to continue(Y/N): ");
            }

            String choice2=scan.next();
            if (choice2.equals(y)) {
                System.out.println("How many rows/columns(5-21)?");
                rows=scan.nextInt();
                while (rows<5 || rows>21) {
                    System.out.println("That is either out of range or not an integer, try again! ");
                    rows=scan.nextInt();
                }

                System.out.println("What character?");
                String choice3=scan.next();
                System.out.println(" ");

                for (rowIndex=1; rowIndex<=rows; rowIndex++) {
                    for (colIndex=1; colIndex<=rows; colIndex++) {
                        if (rowIndex==1 || rowIndex==rows || colIndex==1 || colIndex==rows) {
                            System.out.print(choice3);
                        } else {
                            System.out.print(" ");
                        }
                    }
                    System.out.println();
                }
            } else if(choice2.equals(n)) {
                choice1 = false;
                System.out.println("Thank you. Goodbye.");
            } else {
                System.out.println("Please either enter Y or N.");
            }
        }
    }//end of main 
}

代码打印了我需要它打印的内容,但是当它询问有多少行/列来捕获我是否输入了整数以外的内容时,我还必须在代码中添加一些内容(在下面的部分中).需要一些帮助,我们还没有做任何关于如何捕获异常的事情,我不知道如何开始.

The code prints what I need it to print, but I also have to have something in the code when it asks how many rows/columns to catch whether or not i input something other than an integer(in the part below). need some help, we haven't done anything yet with how to catch exceptions and i don't know how to start.

String choice2=scan.next();
if (choice2.equals(y)) {
    System.out.println("How many rows/columns(5-21)?");
    rows=scan.nextInt();
    while (rows<5 || rows>21) {
        System.out.println("That is either out of range or not an integer, try again! ");
        rows=scan.nextInt();
    }
}

推荐答案

你需要了解 这个请看一下.

You need to understand this please look into it.

基本理解是

try { 
   //Something that can throw an exception.
} catch (Exception e) {
  // To do whatever when the exception is caught.
} 

还有一个 finally 块,即使出现错误也会始终执行.它是这样使用的

There is also an finally block which will always be execute even if there is an error. it is used like this

try { 
   //Something that can throw an exception.
} catch (Exception e) {
  // To do whatever when the exception is caught & the returned.
} finally {
  // This will always execute if there is an exception or no exception.
}

在您的特定情况下,您可能有以下例外情况(链接).

In your particular case you can have the following exceptions (link).

InputMismatchException - 如果下一个标记与 Integer 正则表达式不匹配,或者超出范围NoSuchElementException - 如果输入已用尽IllegalStateException - 如果此扫描器已关闭

InputMismatchException - if the next token does not match the Integer regular expression, or is out of range NoSuchElementException - if input is exhausted IllegalStateException - if this scanner is closed

所以你需要捕捉像

try { 
   rows=scan.nextInt();
} catch (InputMismatchException e) {
  // When the InputMismatchException is caught.
  System.out.println("The next token does not match the Integer regular expression, or is out of range");
} catch (NoSuchElementException e) {
  // When the NoSuchElementException is caught.
  System.out.println("Input is exhausted");
} catch (IllegalStateException e) {
  // When the IllegalStateException is caught.
  System.out.println("Scanner is close");
} 

这篇关于了解 try &amp;捕获和错误处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆