标签: throw

如何在 Javascript 中抛出自定义错误类?

在JS中,你可以抛出一个“new Error(message)”,但是如果你想检测异常的类型并对消息做一些不同的事情,那就没那么容易了。

这篇文章: http://www.nczonline.net/blog/2009/03/10/the-art-of- throwing-javascript-errors-part-2/

是说你可以这样做:

function MyError(message){
  this.message=messsage;
  this.name="MyError";
  this.poo="poo";
}
MyError.prototype = new Error();

try{
  alert("hello hal");
  throw new MyError("wibble");
} catch (er) {
  alert (er.poo);   // undefined.
  alert (er instanceof MyError);  // false
      alert (er.name);  // ReferenceError.
}
Run Code Online (Sandbox Code Playgroud)

但它不起作用(得到“未定义”和错误)

这可能吗?

javascript exception throw

4
推荐指数
1
解决办法
2946
查看次数

`void func()throw(type)`有什么意义?

我知道这是一个有效的c ++程序.函数声明中抛出的重点是什么?AFAIK它什么都不做,不用于任何东西.

#include <exception>
void func() throw(std::exception) { }
int main() { return 0; }
Run Code Online (Sandbox Code Playgroud)

c++ standards throw

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

如何:生成UnhandledException?

我使用此代码来捕获WinForm应用程序UnhandledException.

[STAThread]
static void Main(string[] args)
{
    // Add the event handler for handling UI thread exceptions to the event.
    Application.ThreadException += new
      System.Threading.ThreadExceptionEventHandler(Application_ThreadException);

    // Set the unhandled exception mode to force all Windows Forms errors 
    // to go through our handler.
    Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);

    // Add the event handler for handling non-UI thread exceptions to the event. 
    AppDomain.CurrentDomain.UnhandledException += 
        new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);

    try
    {
        Application.Run(new MainForm());
    } catch....
Run Code Online (Sandbox Code Playgroud)

在那里,我将尝试重新启动应用程序.现在我的问题是模拟这样的异常.我尝试过之前尝试过(在主要方面):throw new NullReferenceException("test");VS抓住了它.

在按钮的MainForm代码中也尝试过:

    private void button1_Click(object sender, EventArgs ev)
    {         
        ThreadPool.QueueUserWorkItem(new …
Run Code Online (Sandbox Code Playgroud)

.net testing exception-handling throw winforms

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

这个语法是什么意思?运算符重载和抛出异常

在书中看到一段代码:

T& operator[](int i) throw(RangeError)
{
    if(i >= 0 && i < sz) return ptr[i];
    throw RangeError();
}
Run Code Online (Sandbox Code Playgroud)

throw(RangeError)是什么意思?函数声明的后面,我知道我们可以添加常量,或= 0(纯虚拟的),但我从来没有见过掷(...)

c++ throw

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

声明异常而不是处理它有什么意义

据我所知,如果您只声明一个已检查的异常,它将通过您的所有方法传播到main方法,并仍然中断您的正常程序流程,您的程序仍将停止工作.那么,为什么不总是使用try/catch处理已检查的异常...这样你的程序不会因异常而停止?为什么要在方法的签名中声明异常?对不起,我的英语不好

java exception try-catch throw try-catch-finally

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

抛出ConnectException之前的Java延迟

我正在用Java编写一些代码来从网址下载内容,在我的配置中,一些下载应该由代理处理,而其他人则不需要处理.

所以我编写了这个代码(它可以工作)来下载所有的URL类型,但是我想减少抛出ConnectException之前的延迟时间,这样代码可以更快地执行.

URL global_url = new URL("http://google.com");
Scanner sc = null;
try { 
    sc = new Scanner(global_url.openStream());
}
catch (ConnectException e) {
    try {
        System.setProperty("http.proxyHost", "my.host");
        System.setProperty("http.proxyPort", "my.port");
        sc = new Scanner(global_url.openStream());
        System.setProperty("http.proxyHost", "");
        System.setProperty("http.proxyPort", "");
    }
    catch (ConnectException exc) {
        //Do stuff
    }
}
Run Code Online (Sandbox Code Playgroud)

现在需要约.抛出异常前10秒,我想把这个时间减少到最多2s或3s.

我可以得到一些帮助吗?谢谢 !

java exception delay throw connectexception

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

投掷三元运算符?

阅读三元运算符的文档,我意识到有两个我从未使用过的特殊情况:

  • 您可以将它与返回void的函数一起使用: bool ? void : void
  • 你可以扔进一个三元运算符

以下是有效的,完全定义的,经常使用的(假设这是一个类成员,并且该类拥有一个Type _data[Size])?

Type& at(const unsigned int i) 
{
    return (i < Size) ? (_data[i]) : (throw std::out_of_range("ERROR"));
}
Run Code Online (Sandbox Code Playgroud)

c++ ternary-operator throw c++11

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

格式错误,无需诊断(NDR):C ++ 14中抛出了ConstExpr函数

#include <iostream>
using namespace std;

constexpr int f(bool b){ return b ? throw 0 : 0; } // OK 

constexpr int f() { return f(true); } // Ill-Formed, No Diagnostic Required

int main(){

    try{
        f();
    }catch( int x ){
        cout << "x = " << x << endl;
    }

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

该代码是C ++ 14标准(ISO / IEC 14882:2014)第7.1.5节第5段的示例:

对于非模板,非默认constexpr函数或非模板,非默认,非继承constexpr构造函数,如果不存在参数值,则该函数或构造函数的调用可以是核心常量的评估子表达式表达式(5.19),程序格式错误;无需诊断。

它被描述为“ 格式错误,不需要诊断 ”,因为throw-expression不是核心常量表达式(5.19 / 2)。但是,Clang和GCC都可以成功编译它(Ideone)。

  • 此代码正确(标准中有错误)还是不正确(Clang和GCC中都存在错误)?

我还发现了有关标准措辞的这些有趣的讨论:

一个/该程序是否可能“ 格式错误,不需要诊断 ”,并且允许编译器成功编译该程序?

c++ throw constexpr c++14

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

C#7中"throw"表达式的编译时类型是什么?

在C#7中,可以在表达式中引发异常:

int n = list != null ? list.Count : throw new NullReferenceException("list");
Run Code Online (Sandbox Code Playgroud)

在此位置,throw表达式可以替换任何类型的表达式.

现在我想定义一个在引发异常之前执行某些操作的函数:

??? DoSomethingAndThrowException(Exception e)
{
    MessageBox.Show("Prepare for an exception!");
    throw e;
}
Run Code Online (Sandbox Code Playgroud)

这样的函数的返回类型是什么,因此它可以在原始throw表达式的相同位置使用:

int n = list != null ? list.Count : DoSomethingAndThrowException(new NullReferenceException("list"));
Run Code Online (Sandbox Code Playgroud)

可以选择将其声明为通用方法:

T DoSomethingAndThrowException<T>(Exception e) {...}
Run Code Online (Sandbox Code Playgroud)

但这似乎很麻烦,因为泛型类型不会出现在函数体的任何地方.这是唯一的方法吗,或者是否有一些我不知道的类型,并且可以分配给任何类型(可以这么说是"反对象"类型)?

c# types throw compile-time c#-7.0

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

当为它们分配抛出错误的函数的返回值时,导致"var"和"let"之间的不同行为的原因

请在下图中找到代码.1.将函数的返回值(实际上抛出一个错误)分配给使用关键字"let"声明的变量'withLet'.2.调用'withLet',发生错误:'withLet未定义'.3.尝试使用'let'断言'withLet',错误显示已经声明'withLet'.

但是'var'不存在悖论(请参见下图).

我很好奇是什么导致了这两种情况之间的不同行为.它是非常有线的,"未定义","已经声明"描述了一个相同的变量.

let withLet = (function() {throw 'error!'})()
var withVar = (function() {throw 'error!'})()
//VM2470:1 Uncaught error!
//(anonymous) @ VM2470:1
//(anonymous) @ VM2470:1
withLet
//VM2484:1 Uncaught ReferenceError: withLet is not defined at 
//<anonymous>:1:1
//(anonymous) @ VM2484:1
withVar
//undefined
let withLet = 'sth'
//VM2520:1 Uncaught SyntaxError: Identifier 'withLet' has already been 
//declared
//at <anonymous>:1:1
//(anonymous) @ VM2520:1
withVar = 'sth'
//"sth"
Run Code Online (Sandbox Code Playgroud)

截图:

javascript var let throw

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