Java 需要为 EmptyStacks 创建一个新的 RunTimeException
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9814283/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Need to create a new RunTimeException for EmptyStacks
提问by Mjall2
So my task may sound simple, but it has me boggled. I have looked through code on the internet, but I cannot get to grips with. Neither can I get to grips with the slides my teacher posted. This is what is required from me.
所以我的任务听起来很简单,但它让我感到困惑。我已经浏览了互联网上的代码,但我无法掌握。我也无法掌握老师发布的幻灯片。这是对我的要求。
Create a new runtime exception type called EmptyStackException.
创建一个名为 EmptyStackException 的新运行时异常类型。
However I have no clue how to make the method, class, variable or whatever needs to be made in order to fulfill the requirement. I have a few classes that are implementations of a DynamicArrayStack and a LinkedStack. An interface for Stack.
但是,我不知道如何制作方法、类、变量或为满足要求而需要制作的任何东西。我有几个类是 DynamicArrayStack 和 LinkedStack 的实现。Stack 的接口。
Any pointers would be mightily helpful.
任何指针都会非常有帮助。
Thanks
谢谢
Mjall2
Mjall2
采纳答案by Jim
Create a new runtime exception type called EmptyStackException.
创建一个名为 EmptyStackException 的新运行时异常类型。
create type is done by
创建类型由
public class EmptyStackException extends RuntimeException { ... }
Now if only we knew what to put in this new type (a.k.a. class). Typically we look at the methods in the superclass and override those needing different handling. Below I have overridden some of them but delegated back to the existing class. No need to do this if you don't need to make any changes in behavior.
现在,如果我们知道在这个新类型(又名类)中放什么。通常我们会查看超类中的方法并覆盖那些需要不同处理的方法。下面我覆盖了其中的一些,但委托回现有的类。如果您不需要对行为进行任何更改,则无需执行此操作。
public class EmptyStackException extends RuntimeException {
public EmptyStackException() {
super();
}
public EmptyStackException(String s) {
super(s);
}
public EmptyStackException(String s, Throwable throwable) {
super(s, throwable);
}
public EmptyStackException(Throwable throwable) {
super(throwable);
}
}
回答by paislee
Sounds like you may want to read up on Java. Check out The Java Tutorials, especially this one about Exceptions.
听起来您可能想阅读 Java。查看The Java Tutorials,尤其是有关Exceptions 的教程。
To put it simply, exceptions are a special kind of object representing an event outside of the normal operation of your code, causing control flowto be subverted. For example, an ArrayIndexOutOfBoundsException
means your code tried to index to a position in an array that did not exist, such as -1.
简单地说,异常是一种特殊的对象,表示代码正常操作之外的事件,导致控制流被颠覆。例如,anArrayIndexOutOfBoundsException
表示您的代码试图索引到数组中不存在的位置,例如 -1。
Because of their association with bugs, exceptions often have a bad connotation to newer programmers. But because Java is Object Oriented, you can extend RuntimeException
to create your own, custom exception types, which is quite useful for debugging and code clarity. To throw a custom exception while your code is executing, you'll have to
由于它们与错误的关联,异常对于新程序员来说通常具有不好的含义。但是因为 Java 是面向对象的,您可以扩展RuntimeException
以创建自己的自定义异常类型,这对于调试和代码清晰度非常有用。要在代码执行时抛出自定义异常,您必须
- have defined the custom exception,
- detect the exceptional condition, and
throw
the exception.
- 定义了自定义异常,
- 检测异常情况,以及
throw
例外。
The easy way to define your custom RuntimeException
is to define a class like:
定义自定义的简单方法RuntimeException
是定义一个类,如:
public EmptyStackException extends RuntimeException {
// customize error messages if necessay
}
Then you'd detect and throw the Exception
like:
然后你会检测并抛出Exception
类似的东西:
if (/** stack is empty */) {
throw new EmptyStackException();
}
These are just the basics. You can also define custom exceptions on the fly. Hope this helps!
这些只是基础知识。您还可以定义自定义异常的飞行。希望这可以帮助!
回答by Matthew
In order to do so, you have to extend the class RuntimeException.
为此,您必须扩展类 RuntimeException。
There are two types of Exceptions in Java: unchecked and checked exceptions. RuntimeExceptions are of the second type. This means they do not need to be explicitly handled and declared.
Java 中有两种类型的异常:未检查异常和已检查异常。运行时异常属于第二种类型。这意味着它们不需要显式处理和声明。
Normally, one uses checked exceptions when writing custom exceptions. This is done by extending the class Exception
. I do not see any use-case for creating a custom RuntimeException
.
通常,在编写自定义异常时使用检查异常。这是通过扩展类来完成的Exception
。我没有看到任何用于创建自定义RuntimeException
.
Anyway, the following code shows how to write your own RuntimeException:
无论如何,以下代码显示了如何编写自己的 RuntimeException:
public class EmptyStackException extends RuntimeException{
public EmptyStackException(String message){
super(message);
}
}
From within your source code you could use this by the following statement:
在您的源代码中,您可以通过以下语句使用它:
throw new EmptyStackException("Stack was Empty, can't pop");
For more information regarding exceptions i recommend you the following Tutorial
有关异常的更多信息,我建议您阅读以下教程