繁体   English   中英

我们如何定义一个扩展java.lang类的Exception类的类

[英]How can we define a class which extends the Exception class of java.lang class

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.*;

public class sentence {
    public static void main() throws IOException {
        InputStreamReader br = new InputStreamReader(System.in);
        BufferedReader in = new BufferedReader(br);
        System.out.println("Enter a sentence.");
        String a = in.readLine();
        String ar[] = new String[10000];
        int k = 0;
        try {
            if (a.length() == 0) {
                throw new NullError();
            }
            if ('a' < a.charAt(0) && a.charAt(0) < 'z') {
                throw new CaseError();
            }
            if (a.charAt(a.length() - 1) != '.') {
                throw new FullStopError();
            }
            int countuppercase = 0;
            String s1 = "";
            for (int i = 0; i < a.length(); i++) {
                char c = a.charAt(i);
                if (1 < countuppercase) {
                    throw new CaseError();
                }
                if (c != ' ' && c != '.') {
                    s1 = s1 + c;
                } else {
                    ar[k] = s1;
                    k++;
                    s1 = "";
                }
            }
        } catch (NullError e) {
            System.out.println("You can't enter an empty string.");
        } catch (CaseError e) {
            System.out
                    .println("You have used the upper case and lower case properly.");
        } catch (FullStopError e) {
            System.out.println("You have forgot to use the full stop.");
        }
        for (int i = 0; i < k - 1; i++) {
            String tmp = "";
            for (int j = i; j < k - 1; j++) {
                if (ar[j].length() > ar[j + 1].length()) {
                    tmp = ar[j];
                    ar[j] = ar[j + 1];
                    ar[j + 1] = tmp;
                }
            }
        }
        for (int i = 0; i < k; i++) {
            System.out.print(ar[i]);
            if (i < (k - 1)) {
                System.out.print(" ");
            } else
                System.out.print(".");
        }
    }
}

class NullError extends Exception// If someone asks me what is name or purpose
                                    // of defining this class what should I
                                    // tell?
{
    public NullError() {
    }
}

class CaseError extends Exception {
    public CaseError() {
    }
}

class FullStopError extends Exception {
    public FullStopError() {
    }
}

例如,如果我编写此代码。

扩展Exception类的类的名称是什么?它被称为抽象类,它的用途是什么?

这段代码按照字母数量的升序排列句子中的单词,我实现了一些自定义异常,这些异常将被强制抛出,例如没有句号。 要引发新的异常,我们需要定义一个单独的具有相同名称的类。 我不明白此类的名称是什么,它是一个抽象类,它的用途是什么,因为我仅为该类定义了一个空的构造函数。

不,这不称为抽象类

扩展Exception的类可以称为用户定义的已检查异常。 您可以在http://docs.oracle.com/javase/tutorial/essential/exceptions/creating.html中找到有关此主题的更多信息。

众所周知, 抽象类是完全不同的东西,与异常无关。 有关的更多信息: http : //docs.oracle.com/javase/tutorial/java/IandI/abstract.html

希望有帮助! :)

您编写的是一个自定义异常,它是用户定义的异常。 这些是与您的业务逻辑有关的例外。 这些异常类可以从您的业务逻辑中抛出。 当存在具有引发这些异常的方法的公共类时,这些方法很有用。 使用这些公共方法的其他类可以选择捕获异常并根据异常(如记录异常)执行操作,也可以再次引发相同的异常。

使用用户定义的异常的这种方式有助于模块化类的开发人员不必理会如何处理异常。 异常的处理取决于调用已签名的方法的类。

您看不到用户定义的异常的重要性,因为您只有一个类,并且抛出甚至捕获了异常。

要创建自定义/自我例外:

public class customException extends Exception { 
  String message = "Your Custom Exception"
 public customException() {
    super(message);
}

}

它是扩展父类的常规类(Exception)。 要强制抛出并捕获您的自定义异常:

try
{
   throw new customException();
}     

catch (customException e) 
{
   System.out.println(e);
}

然后,您的代码将打印您的异常。

抽象类:不,它不是抽象类。 该文档定义了抽象类: http : //docs.oracle.com/javase/tutorial/java/IandI/abstract.html

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM