我很困惑抓住和重新抛出一个例外而不仅仅是把它放在首位.
例如
private void testMethod() throws Exception
{
//some bad code here
}
Run Code Online (Sandbox Code Playgroud)
与:
private void testMethod() throws Exception
{
try
{
//some bad code here
}
catch (Exception e)
{
throw e;
}
} //end testMethod()
Run Code Online (Sandbox Code Playgroud)
这是为了保留错误消息的堆栈跟踪吗?我尝试设置一个示例,但两者之间没有看到任何不同的输出.
谢谢您的帮助.
我有一个convertToArray()转换ArrayList为数组的方法.我想在每次添加元素时调用此方法ArrayList.
public class Table extends ArrayList<Row>
{
public String appArray[]; //Array of single applicant details
public String tableArray[][]; //Array of every applicant
/**
* Constructor for objects of class Table
*/
public Table()
{
}
public void addApplicant(Row app)
{
add(app);
convertToArray();
}
public void convertToArray()
{
int x = size();
appArray=toArray(new String[x]);
}
Run Code Online (Sandbox Code Playgroud)
}
当我调用该addApplication(Row app)方法时,我得到错误:java.lang.ArrayStoreException
所以我改变了我的addApplicant()方法:
public void addApplicant(Row app)
{
add(app);
if (size() != 0)
convertToArray(); …Run Code Online (Sandbox Code Playgroud) 在编写QUnit测试时,我对'throws'的行为感到惊讶.关于以下代码(http://jsfiddle.net/DuYAc/75/),有谁可以回答我的问题:
function subfunc() {
throw "subfunc error";
}
function func() {
try {
subfunc();
} catch (e) {}
}
test("official cookbook example", function () {
throws(function () {
throw "error";
}, "Must throw error to pass.");
});
test("Would expect this to work", function () {
throws(subfunc(), "Must throw error to pass.");
});
test("Why do I need this encapsulation?", function () {
throws(function(){subfunc()}, "Must throw error to pass.");
});
test("Would expect this to fail, because func does not throw …Run Code Online (Sandbox Code Playgroud) 我正在通过阅读 SG Ganesh 和 Tushar Sharma 撰写的书来学习 OCJP 考试。第346页有一段文字说:
\n\n\n如果您尝试更改 throws 子句会怎样?有多种方法可以更改重写方法中的 throws 子句,包括以下几种
\n\n
:不提供任何 throws 子句。
\n b.列出要抛出的更一般的检查异常。
\n c. 除了基本方法中给定的已检查异常之外,还列出更多已检查异常。
\n 如果尝试这三种情况中的任何一种,\xe2\x80\x99 将收到编译器错误。例如,尝试不在实现 IntReader 接口的类的 readIntFromFile() 方法中提供 throws 子句。Run Code Online (Sandbox Code Playgroud)\n\npublic int readIntFromFile() {\n Scanner consoleScanner = new Scanner(new File("integer.txt"));\n return consoleScanner.nextInt();\n}\n你\xe2\x80\x99会得到这个编译器错误:\xe2\x80\x9cun报告异常FileNotFoundException; 必须被捕获或声明为抛出。\xe2\x80\x9d
\n\n
\n 总而言之,基类方法\xe2\x80\x99s throws 子句是它提供给该方法的调用者的契约:
\n 它表示调用者应该处理列出的异常或在其 throws 子句中声明这些异常。当重写基本方法时,派生方法也应遵守该约定。基本方法的调用者准备只处理基本方法中列出的异常,因此重写方法不能抛出更一般的异常或除列出的已检查异常之外的异常。
\n 但是,请注意,派生类方法\xe2\x80\x99s throws 子句应遵循基方法\xe2\x80\x99s throws 子句的约定的讨论仅限于受检查的异常。与基类 method\xe2\x80\x99s throws 子句相比,仍可以在合约中添加或删除未经检查的异常。例如,请考虑以下情况:Run Code Online (Sandbox Code Playgroud)public int readIntFromFile() throws IOException, NoSuchElementException {\n Scanner consoleScanner = new Scanner(new File("integer.txt"));\n …
我正在使用SQLite库,其中查询返回可选值以及可能抛出错误.我想有条件地解包该值,或者如果它返回错误则收到nil.我不完全确定怎么说这个,这段代码会解释,这就是它的样子:
func getSomething() throws -> Value? {
//example function from library, returns optional or throws errors
}
func myFunctionToGetSpecificDate() -> Date? {
if let specificValue = db!.getSomething() {
let returnedValue = specificValue!
// it says I need to force unwrap specificValue,
// shouldn't it be unwrapped already?
let specificDate = Date.init(timeIntervalSinceReferenceDate: TimeInterval(returnedValue))
return time
} else {
return nil
}
}
Run Code Online (Sandbox Code Playgroud)
有没有办法避免不得不强行打开那里?在更新到Swift3之前,我没有被迫在这里强行打开包装.
以下是实际代码.只是想从所有条目中获取最新的时间戳:
func getLastDateWithData() -> Date? {
if let max = try? db!.scalar(eventTable.select(timestamp.max)){
let time = Date.init(timeIntervalSinceReferenceDate: TimeInterval(max!))
// …Run Code Online (Sandbox Code Playgroud) 在查看此问题时,我注意到应用于@Throwsaget或setuse-site 无效。
此外,唯一有效的目标为@Throws是AnnotationTarget.FUNCTION,AnnotationTarget.PROPERTY_GETTER,AnnotationTarget.PROPERTY_SETTER,和AnnotationTarget.CONSTRUCTOR。
其他注释,例如 JPA 注释和Deprecated工作正常,并正确应用于该方法!
这是奇怪的行为。
为了演示,我在 Java 中创建了一个简单的抽象类,具有一个构造函数、一个方法和一个 get 方法。
public abstract class JavaAbstractClass {
@Deprecated
@NotNull public abstract String getString() throws IOException;
public abstract void setString(@NotNull String string) throws IOException;
public abstract void throwsFunction() throws IOException;
public JavaAbstractClass() throws IOException {
}
}
Run Code Online (Sandbox Code Playgroud)
如您所见,每个方法/构造函数都标记为 throwing IOException。
然而,当我尝试在 Kotlin 中编写一个等效的类,并用throwsfor interop标记适当的方法时,生成的getString和 …
“处理或申报。这是法律。” -先行
但是,这是一条好法律吗?我先举个例子:
public static void main(String[] args) throws Exception {
m1();
}
static void m1() throws Exception{
m2();
}
static void m2() throws Exception {
throw new Exception();
}
Run Code Online (Sandbox Code Playgroud)
m2() 抛出异常并m1()调用m2(),这意味着它必须处理或声明它。嗯,让我们宣布吧。然后main()调用m1(),它有相同的 poll: declare或handle。我再次决定声明它并且代码编译得很好。
好的,它有效,但是谁处理了这个异常呢?好像没有人做过。我知道我是初学者,但我不喜欢那样的声音。是的,有些方法可以决定是声明还是处理异常,但为什么main()呢?main 方法不应该是一个只处理的方法吗?那样的话,任何例外都不会“滑倒”。
我错过了什么吗?老实说,我很惊讶 main 方法可以只声明异常,因为知道这是我们在技术上可以捕获某些东西的最后一个地方。
在 中Java,Android Studio 有一个功能可以避免调用throws没有try/catch块的异常方法,并且它的自动完成建议:Add exception to method signature或Surround with try/catch。
但Kotlin我们没有throws关键字,应该使用@Throws()它。问题是,它Android Studio不再迫使我们包围try/catch。另一方面,它并不强迫我们使用 类似try/catch的java methods进步。throws ExceptionIO
测试.java
public class Test {
public void doSomeWork1(int a) throws Exception {
if (a == -1) {
throw new Exception("some error");
}
}
public void main() {
doSomeWork1(-1);
}
}
Run Code Online (Sandbox Code Playgroud)
测试.kt
class Test {
@Throws(Exception::class)
fun doSomeWork1(a: Int) { …Run Code Online (Sandbox Code Playgroud) 我正在进行代码审查,我遇到了这个方法定义:
public void something() throws RuntimeException
Run Code Online (Sandbox Code Playgroud)
有没有合理的理由在Java中编写'throws RuntimeException'?
我想知道只Exception抛出和抛出一个特定的异常有什么区别,比如NullPointer Exception.
据我目前所知,Exception应该能够捕获任何类型的异常,因为使用特定的 Exception 期望只能抛出该异常类型。
例子:
void test(String testString) throws Exception;
Run Code Online (Sandbox Code Playgroud)
对比
void test(String testString) throws NullPointerException;
Run Code Online (Sandbox Code Playgroud)
如果这对我来说是正确的,那么总是抛出异常并且从不命名特定异常是有意义的。我在这里遗漏了什么重要的东西吗?它至少会影响性能吗?很多人在抛出和捕获异常之间寻找,但没有人问这个非常基本的问题。
我没有看到抛出任何异常的好处,除了Exception.