标签: throw

你用什么样的异常消息?

在编写抛出我在这里询问的异常的代码时,我走到了我的消息的末尾,并在标点符号处停了下来.我意识到几乎所有我抛出的异常消息都可能有!某处.

throw new InvalidOperationException("I'm not configured correctly!");
throw new ArgumentNullException("You passed a null!");
throw new StupidUserException("You can't divide by 0!  What the hell were you THINKING???  DUMMY!!!!!");
Run Code Online (Sandbox Code Playgroud)

在编写异常消息时你会采取什么样的语气?在浏览日志时,您是否发现任何特定类型的消息实际上比另一种更有帮助?

.net c# logging exception throw

5
推荐指数
3
解决办法
797
查看次数

使用错误对象相关性在Javascript中抛出错误

这几乎与IE有关,因为IE是我用来测试它的环境,但我想知道在抛出错误时是否可以影响错误对象属性的相关性.考虑以下javascript:

function MyClass (Arg1, Arg2)    // Line 5 of my.js
{
    if (typeof Arg1 != "string")
        throw new Error("Invalid argument passed for MyClass");
    // Do some other stuff here
}
Run Code Online (Sandbox Code Playgroud)

进一步下载你的代码

var myvar = new MyClass(100, "Hello");    // Line 3201 of my.js
Run Code Online (Sandbox Code Playgroud)

所以上面会抛出一个错误,但调试信息中报告的错误会显示在my.js第9行而不是第320行引发的错误.这是否可以使用标准方法更改?

javascript error-handling throw

5
推荐指数
1
解决办法
904
查看次数

C++ Exception在虚函数上抛出注释

我看到了以下代码段:

class Foo
{
public:
        void virtual func() throw (int, float) = 0;
};

class Bar : public Foo
{
public:
        void virtual func() throw(short);      // line 1: compile error "
                                                                      // looser throw specifier"
        void virtual func() throw();                // line 2: can compile
        void virtual func() throw(float, int); // line 3: can compile
        void virtual func() throw(float);        // line 4: can compile
        void virtual func() throw(int);           // line 5: can compile

};

int main(void)
{
        return 1;
}
Run Code Online (Sandbox Code Playgroud)

Q1>是什么意思 …

c++ virtual throw

5
推荐指数
2
解决办法
5437
查看次数

在Scala中@throws问题

我正在使用Eclipse在Scala中编程,但是当我使用@throws注释时它会给我一个错误.

import org.newdawn.slick.AppGameContainer
import org.newdawn.slick.BasicGame
import org.newdawn.slick.GameContainer
import org.newdawn.slick.Graphics
import org.newdawn.slick.SlickException
import scala.throws

object Base extends BasicGame("SNAKE!")
{  
  def main(args: Array[String]) 
  {
      println("Starting up")
  }

  def init(container : GameContainer)
  {
    @throws(classOf[SlickException])
  }

}
Run Code Online (Sandbox Code Playgroud)

scala exception throw

5
推荐指数
1
解决办法
5673
查看次数

new然后抛出C++构造函数?

如果我做

Bat::Bat() : m_member_str(new std::string("Am I freed?"))
{
  throw std::runtime_error("oops");
}
Run Code Online (Sandbox Code Playgroud)

new分配的是免费的std::string吗?我在想它可能是因为没有调用析构函数.

我没有使用std :: string,而是使用我自己的类,只是将它显示为一个简单的例子.

c++ constructor throw new-operator

5
推荐指数
1
解决办法
489
查看次数

php,可以异常被抛出2级吗?

我知道这很奇怪,但在我的代码中,我有开发模式错误和生产模式错误.这是我的功能:

private function error($message, $mysql_error = null){
    if( DEVELOPMENT_MODE ){
        $exp = new Exception();
        $trace = $exp -> getTrace();
        array_shift( $trace ); // removes this functions from trace
        $data["Error Mg"] = $message;
        $data["MySQL Er"] = ( is_null ( $mysql_error ) ) ? "" : $mysql_error;
        array_unshift($trace, $data );
        fkill( $trace );  // formats array and then dies
    }
    else{
        throw new Exception ( $data );
    }
}
Run Code Online (Sandbox Code Playgroud)

我在我的数据库类中编写了这个函数,这样如果发生错误,我不必提供检查我们是否处于开发模式!

所以我认为我可以将可重复使用的代码外部化.但是,因为我从这个函数抛出一个异常,我基本上只是使用一个函数,它将返回抛出的错误.在生产模式中相当无用.

每次我想使用它时我都必须这样做:

try{
    $this -> error( "Invalid Link After Connect.", mysql_error () …
Run Code Online (Sandbox Code Playgroud)

php exception try-catch throw

5
推荐指数
2
解决办法
3535
查看次数

在throw参数周围添加括号是否有效?

写作有区别吗:

throw SomeException;
Run Code Online (Sandbox Code Playgroud)

throw(SomeException);
Run Code Online (Sandbox Code Playgroud)

我看到一些消息来源声称后者(括号)不是出于某种原因的好选择但是我不记得我在哪里见过这个.

c++ throw parentheses

5
推荐指数
1
解决办法
576
查看次数

如何在iOS中以编程方式捕获__cxa_throw

AudioToolBox中的某些函数总是抛出__cxa_throw,同时ExtAudioFileDispose等函数运行良好,返回值为零,这是正常的.当我想使用try {} catch(...){}块捕获此异常时,但这不起作用.在Xcode的所有例外情况下,可以捕获此异常.在这里,只想了解__cxa_throw是什么,以及如何以编程方式捕获它?

xcode try-catch throw audiotoolbox ios

5
推荐指数
1
解决办法
974
查看次数

如果传入无效值,则抛出异常

我有一个类,现在我正在更改setter以在传入无效值时抛出异常.它需要:

  • 答:(dueDay必须介于1和31之间,且dueMonth必须介于1和12之间.)异常显示不在setter方法中处理.
  • B.更改主要方法,TodoItem以便它向用户询问任务,截止日期和到期月份,并将此信息存储为新信息TodoItem.
  • C.更改构造函数,以便将其称为新的setter方法.如果抛出异常,则应在main方法中处理.应告知用户他们输入无效的日期或月份,并要求提供正确的日期或月份.

我的班级是:

(我已经更改了setter以抛出异常,但是,它不起作用,我想我应该更改main函数的构造函数,但是我不知道该怎么做.)

public class TodoItem {

    private String task;
    private int dueMonth;
    private int dueDay;
    private boolean isDone;

    // class variables
    private static int numItems;
    private static int numDone;

    // constructor
    public TodoItem(String taks,int day,int month) {
        this.task = task;
        dueDay = day;
        dueMonth = month;
        isDone = false;

        numItems++;
    }

    // second constructor
    public TodoItem(String task) {
        this.task = task;
        isDone = false;

        numItems++;
    }

    public static …
Run Code Online (Sandbox Code Playgroud)

java exception throw

5
推荐指数
1
解决办法
1609
查看次数

C++中有可能什么都不扔?

在特殊情况下,我希望我的程序停止处理,输出错误std::cerr,清理和退出.

但是,调用exit()不会调用已构造的任何对象的所有析构函数.我想要很好地调用析构函数,所以我将所有代码包装在一个try-catch块中,如下所示:

int main(int argc, char** argv){
    try {
        bool something_is_not_right = false;
        /* lots of variables declared here */
        /* some code that might set something_is_not_right to true goes here */
        if(something_is_not_right){
            std::cerr << "something is not right!!!" << std::endl;
            throw '\0';  // dummy unused variable for throwing.
        }
    }
    catch (...) {}
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

通过这种方式,我可以保证破坏所有变量.但我似乎无法找到让C++ throw无所作为的方法. throw;在C++中有特殊意义; 它什么都不扔.

有什么方法可以throw吗?

c++ destructor exception throw

5
推荐指数
2
解决办法
1178
查看次数