标签: throw

是否在调用终止时调用自动对象的析构函数?

当我们throw来自析构函数时会发生什么?我知道它会导致terminate()被调用,并且内存确实被释放并且析构函数被调用,但是,这是在调用之前还是之后 ?也许这里的问题是在堆栈展开时使用的问题.throwfoothrow

c++ stack throw brainbench

2
推荐指数
1
解决办法
2149
查看次数

在flash AS3中如何设置单个try catch代码,以便捕获整个类中的任何错误?

在Flash AS3中,我想编写单个try catch块以捕获整个类中的任何错误.
例如,我在myClass.as中有很多函数.我不想在每个函数中编写try catch块以便捕获此函数中的错误.
有没有办法做到这一点?

谢谢!

flash try-catch actionscript-3 throw

2
推荐指数
1
解决办法
3万
查看次数

如何抛出ArrayIndexOutOfBoundsException?

我有一个检查2D数组中的斑点的方法,它还检查它们是否为空.我想扔掉ArrayIndexOutOfBoundsException因为我已经检查了null.

throws ArrayIndexOutOfBoundsException在声明方法后尝试添加,但它不起作用.我该怎么做呢?

java arrays throw indexoutofboundsexception

2
推荐指数
1
解决办法
1万
查看次数

Java抛出的抽象类

如果我有一个具有以下功能的抽象类 -

abstract class A{
    void foo(String s) throws Exception{
        throw new Exception("exception!");
    }
}
Run Code Online (Sandbox Code Playgroud)

然后是另一个扩展抽象类并实现其自己的foo版本的类 -

class B extends A{
    void foo(String s){
        //do stuff that does *not* throw an exception
    }
}
Run Code Online (Sandbox Code Playgroud)

这会产生问题吗?具体在以下测试案例中 -

Collection<A> col = new Collection<A>();
B b = new B();
col.add(b);
for(A a : col){
    a.foo();
}
Run Code Online (Sandbox Code Playgroud)

我做了一些测试,似乎没有什么破坏,但我不明白为什么B的foo被调用而不是A的

java exception abstract throw

2
推荐指数
1
解决办法
1万
查看次数

即使不符合条件,也要保持抛出异常

这可能是一个非常棒的问题,但我想我会抓住机会.

所以基本上我必须做一个任务,它如下:

我必须创建一个构造函数,但变量"naam"不能为null或为空(""),变量"geboortedatum"不能在将来也不能与今天和最后一个变量相同" boeken"与变量"naam"具有相同的要求(因为它不能为null也不能为"").

所以这就是我的构造函数的样子,我只能编辑这部分,因为另一部分是由我们的老师给出的,不能编辑.

        if (this.naam == null || this.naam.equals("")) {
        throw new IllegalArgumentException("Fill in name");
    } else {
        this.naam = naam;
    }
    Date vandaag = new Date();
    if (this.geboorteDatum >= vandaag.getTime()) {
        throw new IllegalArgumentException("Date must be in the past");
    } else {
        this.geboorteDatum = geboortedatum;
    }
    if (this.boeken == null || Arrays.toString(boeken).equals("")) {
        throw new IllegalArgumentException("Can't be empty");
    } else {
        this.boeken = boeken;
    }  
Run Code Online (Sandbox Code Playgroud)

它不断抛出我的第一个例外,我无法弄清楚为什么.这可能是一个非常愚蠢的问题,但我似乎无法找出原因.

任何帮助将非常感谢,提前感谢

java exception throw

2
推荐指数
1
解决办法
47
查看次数

Java:终于如何抛出特朗普?

我刚刚在我们的一个Java库中遇到了一个隐藏的gem:

for(Widget w : widgets) {
    if(shouldDoStuff()) {
        try{
            // Do stuff to w.
        } catch(Exception e){
            throw new RuntimeException("Couldn't do stuff.");
        } finally{
            // Compiler warning: finally block does not complete normally
            continue;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我知道这finally胜过一切,但我想知道两件事:

  1. 当发生什么catch条款执行呢?异常是否被抛出?首先发生什么:抛出的异常或continue声明?
  2. 我怎样才能重写这个以消除警告?

我发现了这个非常相似的问题,但是接受的答案只是声明异常将被突然抛出,我不确定这意味着什么.另外,它并没有真正帮助我理解我上面提到的关于将要发生的事件顺序的第一个问题.

java exception throw compiler-warnings try-catch-finally

2
推荐指数
1
解决办法
162
查看次数

如何在CakePHP上抛出自定义try-catch异常?

我想抛出一个自定义异常,这是我的CakePHP应用程序的控制器中的数据验证异常.如何在Cakephp中创建自己的自定义异常处理程序,以便我可以抛出异常并捕获异常?

我的代码示例:

function getUserDetails($userid){

    try{
         if(!$validUser){
              throw new Exception('Invalid User');
         }

         return $userDetailsData;  //returned from db
    }catch(Exception $e){
         echo 'Error:'.$e->getMessage();
        return;
    }

}
Run Code Online (Sandbox Code Playgroud)

是否有可能在cakephp中使用自定义Exception类,以便只能抛出那些我做的异常.希望它澄清问题.谢谢.

cakephp exception try-catch throw

2
推荐指数
1
解决办法
6332
查看次数

PowerMockito如何在构造函数调用上抛出异常

我正在使用反射测试以下私有方法(getPrintWriter):

public abstract class CsvAuditor extends Auditor {

    public void run() {
        PrintWriter writer = getPrintWriter();
        if (writer == null)
            return;
        writeReport(writer);
        writer.close();
    }

    private PrintWriter getPrintWriter() {
        PrintWriter writer;
        try {
            DateFormat dateFormat = new SimpleDateFormat("yyyy_MM_dd__HH_mm_ss");
            Date date = new Date();
            writer = new PrintWriter("report__" + dateFormat.format(date) + ".csv");
        }
        catch (FileNotFoundException ex) {
            System.err.println(ex.getMessage());
            return null;
        }
        return writer;
    }

}
Run Code Online (Sandbox Code Playgroud)

我想模拟PrintWriter的构造函数抛出FileNotFoundException.

@RunWith(PowerMockRunner.class)
@PrepareForTest({CsvAuditor.class, PrintWriter.class})
public class CsvAuditorTest {

    private final ByteArrayOutputStream errContent = new ByteArrayOutputStream();

    @Before
    public …
Run Code Online (Sandbox Code Playgroud)

java throw powermockito

2
推荐指数
1
解决办法
4658
查看次数

如何抛出异常

throw;和之间选择的最佳做法是throw ex;什么?有没有?关于 - 例如 - 这个简单的片段:

try{
    // some code
} catch (Exception ex) {
    // some catcher code
    // throw; ?
    // or
    // throw ex; ?
    // how to decide which one?
}
Run Code Online (Sandbox Code Playgroud)

更新: 我知道上面的两个区别.问题是如何决定使用其中之一?有没有最好的做法来做出更好的选择?

c# exception try-catch throw c#-4.0

2
推荐指数
1
解决办法
102
查看次数

为什么使用Environment.Exit()而不是引发异常时需要返回?

我试图更好地了解C#的编译器。它坚持要求所有代码路径都必须返回一个值,我认为这很公平。

它还认识到,如果在需要返回值的路径中引发了异常,则在该处返回值没有意义。这也是有道理的。

我的问题是:为什么这还不适合以更优雅的方式退出程序?例如Environment.Exit()

-例子-

这将编译:

private string TestMethod(int x, int y)
{
    if (x == y)
    {
        return "this is a string";
    }
    throw new Exception(); 
    // No point in a return after this, it could never be reached.
}
Run Code Online (Sandbox Code Playgroud)

这不会编译:

private string TestMethod(int x, int y)
{
    if (x == y)
    {
        return "this is a string";
    }
    Environment.Exit(1);
    // This will not compile.
    // "Not all code paths return a value"
    // But, the code would …
Run Code Online (Sandbox Code Playgroud)

c# compiler-errors return exception throw

2
推荐指数
1
解决办法
55
查看次数