标签: throw

带百分号 (%) 的 THROW 会产生奇怪的错误消息

在 SSMS 1中执行以下 T-SQL 语句会给出一条仅包含一个空格2 的错误消息:

THROW 50000, 'abc%de', 0;

Msg 50000, Level 16, State 0, Line 1
Run Code Online (Sandbox Code Playgroud)

但是,如果我通过加倍 % 来转义它,我会收到预期的错误消息:

THROW 50000, 'abc%%de', 0;

Msg 50000, Level 16, State 0, Line 1
abc%de
Run Code Online (Sandbox Code Playgroud)

我还注意到,当 % 后面跟空格时,它会被跳过:

THROW 50000, 'abc% de', 0;

Msg 50000, Level 16, State 0, Line 1
abc de
Run Code Online (Sandbox Code Playgroud)

由于某种原因,THROW 以特殊方式解释 %。

  1. 有人知道为什么吗?
  2. 还有其他“特殊”角色吗?
  3. 这有记录在任何地方吗?

我在 MS SQL Server 2012 和 2014 下观察到了这种行为。我没有尝试过其他版本。


1我也尝试过 ADO.NET,具有相同的结果。

2这里看不清楚,但我仔细检查过:错误消息确实不是空字符串,而是恰好有一个空格。

t-sql sql-server throw

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

如何在 LESS 编译器中抛出错误

问题

有什么方法可以(以编程方式)在 LESS 编译器中抛出错误吗?

为什么?

我今天一直在摆弄 mixin 防护,因为我想根据元素大小和元素计数生成 CSS 边距。我认为当元素不适合包装器时,直接在编译时抛出错误会很酷。

信息:我正在使用lessc编译器将 LESS 代码编译为 CSS。我没有使用任何 Javascript 库在执行时编译它。

更少的来源

// Variables
@wrapper-small:  830px;
@wrapper-big:   1200px;

.col-fixed(@size, @count, @wrapper)  when ((@size*@count) <= @wrapper)
{
    width: unit(@size, px);
    margin-right: unit( (@wrapper - @count * @size) / (@count - 1), px);    
}

.test_col_fixed {
    // will fail the mixin guard and output no generated CSS        
    .col-fixed(340, 3, @wrapper-small);

    // would work if not in comment
    // .col-fixed(340, 3, @wrapper-big);
}
Run Code Online (Sandbox Code Playgroud)

生成的 CSS(小包装) …

css compiler-errors throw less less-mixins

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

使用断言 C++ 测试函数

我想使用断言测试 gcd 函数,但我不知道如何捕获异常(并防止程序崩溃)。

int gcd(int a, int b) {
if(a<0 || b<0) {
    throw "Illegal argument";
}
if(a==0 || b==0)
    return a+b;
while(a!=b) {
    if(a>b) {
        a = a - b;
    }
    else {
        b = b - a;
    }
}
return a;
Run Code Online (Sandbox Code Playgroud)

}

void test_gcd() {
assert(gcd(16,24) == 8);
assert(gcd(0, 19) == 19);
try {
    gcd(5, -15);
    assert(false);
} catch (char* s) {
    assert(true);
    cout << "Illegal";
}
Run Code Online (Sandbox Code Playgroud)

}

c++ assert try-catch throw greatest-common-divisor

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

将重新抛出函数保存为非抛出闭包

据我了解,rethrows基本上从单个声明/定义创建两个函数,如下所示:

func f(_ c: () throws -> Void) rethrows { try c()}

// has the same effect as declaring two seperate functions, with the same name:

func g(_ c: () throws -> Void) throws { try c() }
func g(_ c: () -> Void) { c() }
Run Code Online (Sandbox Code Playgroud)

如果我有一个重新抛出函数,比如f,有没有办法将它保存为“非抛出”形式的闭包?假设,像这样:

let x: (() -> Void) -> Void = f
// invalid conversion from throwing function of type '(() throws -> Void) throws -> ()' to non-throwing function type …
Run Code Online (Sandbox Code Playgroud)

closures throw rethrow swift

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

TSQL RAISERROR 作为警告

我正在查看 Microsoft 70-761 考试的问题,发现了一个麻烦的问题。该问题要求“如果出现警告”。

显然我应该使用RAISERROR(or THROW) 语句。我的问题是 - 我应该使用什么严重程度来满足此要求?

我知道 16 是默认值THROW,但我犹豫是否将其称为警告。也许RAISERROR将严重性设置为 10 会更合适?

t-sql sql-server raiserror throw

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

Angular/RxJS 6 - 如何对 next() 触发的指令引发异常进行单元测试

在迁移到 RxJs6 之前,我的单元测试之一是:

it('should do what I expect, () => {
  expect(() => {
    myComponent.mySubject.next({message: 'invalid'});
  }).toThrow('invalid is not an accepted message');
})
Run Code Online (Sandbox Code Playgroud)

在我的组件中,我订阅了主题并调用了一个可以抛出异常的私有方法。看起来像这样的东西:

export class MyComponent {
  //...
  mySubject = new Subject();
  //...
  ngOnInit(){
    this.mySubject.subscribe(obj => this._doSomething(obj))
  }
  //...
  private _doSomething(obj) {
    if ('invalid' === obj.message) {
      throw new Error('invalid is not an accepted message');
    }
    //...
  }
}
Run Code Online (Sandbox Code Playgroud)

自从我迁移到 RxJs6 以来,这个 UT 不再工作(以前工作过),我不知道如何使它工作。

我阅读了迁移指南,尤其是本节:替换同步错误处理,但它是关于subscribe(),而不是next()......

提前致谢

unit-testing throw jasmine rxjs angular

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

javascript forEach 管理异常

我有一个 forEach 循环,用于在将数据推送到数组之前检查是否存在条件:

allow(id, perm, arr) {
  const data = { "userId": id, "permissionId": perm };

  arr.forEach(key => {
    if (id !== key.userId) {
      throw new Error('id does not exists');
    } else {
      if (perm === key.permissionId) {
        throw new Error('perm exists in this id');
      } else {
        arr.push(data);
      }
    }
  });
}
Run Code Online (Sandbox Code Playgroud)

原来的数组arr是这样的:

[{"userId": 1,"permissionId": 1},
 {"userId": 2,"permissionId": 1},
 {"userId": 3,"permissionId": 0},
 {"userId": 4,"permissionId": 0}]
Run Code Online (Sandbox Code Playgroud)

如果我使用console.log()所有作品,但是当我抛出错误时,执行会停止,但是如何在不停止循环的情况下管理异常?

javascript testing error-handling foreach throw

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

使用代码和消息抛出新异常

我正在从包含statusCodestatusMessage... 的服务器解析JSON 我如何在我的异常中抛出这些,以便我不必if在我的catch中使用-statements?所以,我可以有一个通用的过程,处理所有exc.Codeexc.Message,而不必寻找它.

这是我的投掷

else if (statusCode.Equals(26) && statusMessage.StartsWith("response sent", StringComparison.OrdinalIgnoreCase))
    throw new Exception("Response sent - 26");
else if (statusCode.Equals(0))
    throw new Exception("Fatal exception - 0");
else if (statusCode.Equals(3))
    throw new Exception("Invalid parameters - 3");
else if (statusCode.Equals(24))
    throw new Exception("Incorrect response Id - 24");
Run Code Online (Sandbox Code Playgroud)

这是我的捕获

try
{
    dataResponse = GetStatus.RequestStatus(httpRequest);
}
catch (Exception exc)
{
    if (exc.Message.ToString() == "Response sent - 26")
    {
        string errorCode = "26";
        string …
Run Code Online (Sandbox Code Playgroud)

c# exception-handling try-catch throw

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

您如何抛出Lua错误?

是否可以从函数抛出Lua错误,以由调用该函数的脚本来处理?

例如,以下内容将在指定的注释处引发错误

local function aSimpleFunction(...)
    string.format(...) -- Error is indicated to be here
end

aSimpleFunction("An example function: %i",nil)
Run Code Online (Sandbox Code Playgroud)

但是我想做的是捕获错误并通过函数调用者抛出自定义错误

local function aSimpleFunction(...)
    if pcall(function(...)
        string.format(...)
    end) == false then
       -- I want to throw a custom error to whatever is making the call to this function
    end

end

aSimpleFunction("An example function: %i",nil) -- Want the error to start unwinding here 
Run Code Online (Sandbox Code Playgroud)

目的是在我的实际用例中,我的功能会更复杂,并且我想提供更有意义的错误消息

error-handling lua throw

4
推荐指数
2
解决办法
7498
查看次数

"throw(e)"和"throw e"之间的区别?

我遇到了这个重新抛出的异常,我很惊讶它甚至可以编译.

} catch(SomeException e) {
    ...
    throw(e);
}
Run Code Online (Sandbox Code Playgroud)

这个throw()和通常使用的是什么区别?...

} catch(SomeException e) {
    ...
    throw e;
}
Run Code Online (Sandbox Code Playgroud)

是否记录了这些内容的任何链接或选择其中一个的指导?

java exception throw

4
推荐指数
2
解决办法
576
查看次数