标签: throw

C++:抛出异常,使用"新"或不使用?

是否适合使用throw new FoobarException(Baz argument);throw FoobarException(Baz argument);

在捕捉时我总是使用catch(FoobarException& e)"以防万一",但我无法找到一个可靠的答案,我是否必须在C++中使用新的或不使用(肯定是Java),或者它只是程序员的偏好.

c++ exception throw

26
推荐指数
2
解决办法
7263
查看次数

重新抛出python异常.要抓哪个?

我正在学习使用python.我刚刚看到这篇文章:http: //nedbatchelder.com/blog/200711/rethrowing_exceptions_in_python.html 它描述了在python中重新抛出异常,如下所示:

try:
    do_something_dangerous()
except:
    do_something_to_apologize()
    raise
Run Code Online (Sandbox Code Playgroud)

因为你重新抛出异常,所以shouold是一个"外部catch-except"语句.但现在,我在想.如果except中的do_something_to_apologize()抛出错误怎么办?哪一个将被捕获在外部"捕获 - 除外"?你重新抛出的那个还是do_something_to_apologize()抛出的那个?或者首先捕获具有最高暴力的例外情况?

python exception throw rethrow

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

可以手动抛出std :: bad_alloc吗?

我有这个代码..

 CEngineLayer::CEngineLayer(void)
 {
    // Incoming creation of layers. Wrapping all of this in a try/catch block is
    // not helpful if logging of errors will happen.

    logger = new (std::nothrow) CLogger(this);

    if(logger == 0)
    {
     std::bad_alloc exception;
     throw exception;
    }

    videoLayer = new (std::nothrow) CVideoLayer(this);

    if(videoLayer == 0)
    {
     logger->log("Unable to create the video layer!");

     std::bad_alloc exception;
     throw exception;
    }
 }

 IEngineLayer* createEngineLayer(void)
 {
    // Using std::nothrow would be a bad idea here as catching things thrown
    // from the …
Run Code Online (Sandbox Code Playgroud)

c++ throw new-operator bad-alloc

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

SQL只在if语句中抛出

我正在为一些存储过程添加一些验证,并且需要检查某些变量是否为空(它们在存储过程的早期填充).

我一直试图在if语句中添加一个"throw",如下所示:

IF (@val is null)
BEGIN
    THROW 50001, 'Custom text', 1
END
Run Code Online (Sandbox Code Playgroud)

这会导致"throw"出现语法错误,因为它在throw之前查找if语句中的其他代码,但我只需要它在if语句中执行throw.

我需要保持存储过程尽可能轻,以尽可能快地执行.

有没有人有任何想法?

sql sql-server if-statement throw

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

try-catch-rethrow的代码是否等同于没有try-catch的代码?

在哪种情况下,以下两个代码不相同?

{
  // some code, may throw and/or have side effects
}

try {
  // same code as above
} catch(...) {
  throw;
}
Run Code Online (Sandbox Code Playgroud)

编辑只是为了澄清,我对(i)偏离上述模式(例如catch块中的更多代码)以及(ii)旨在邀请关于try- catchblock 的正确使用的光顾评论感兴趣.

我正在寻找一个参考C++标准的合格答案.这个问题是由干杯和他的评论提出的.- Alf对我的这个答案,说明没有进一步说明上述代码不相同.


编辑他们确实不同.堆栈取消将在后者中完成,但不一定在前者中,取决于是否catch在运行时找到异常处理程序(堆栈上方的某些块).

c++ exception try-catch throw language-lawyer

23
推荐指数
2
解决办法
1339
查看次数

`throw` 可以在 C++ 条件(三元)运算符中的逗号子表达式内吗?

众所周知,throw可以作为C++三元运算符的第二个或第三个操作数放置?:。但是它可以在操作数的逗号子表达式中吗?看起来编译器在这方面存在分歧。请考虑一个例子:

#include <iostream>

void foo(bool b) {
    int i = b ? 1 : (throw 0); //ok everywhere
    if ( !b )
        (std::cout << "smth\n", throw 0); //ok everywhere
    i = b ? 2 : (std::cout << "smth\n", throw 0); //ok in MSVC only
};
Run Code Online (Sandbox Code Playgroud)

这个例子被 MSVC 接受,但被 GCC 和 Clang 拒绝,演示:https : //gcc.godbolt.org/z/6q46j5exP

虽然错误信息:

#include <iostream>

void foo(bool b) {
    int i = b ? 1 : (throw 0); //ok everywhere
    if ( !b …
Run Code Online (Sandbox Code Playgroud)

c++ conditional-operator throw language-lawyer

23
推荐指数
2
解决办法
1062
查看次数

在java中抛出异常后继续执行

我正在尝试抛出一个异常(不使用try catch块),我的程序在抛出异常后立即完成.有没有办法在我抛出异常之后再继续执行我的程序?我抛出了我在另一个类中定义的InvalidEmployeeTypeException,但我希望程序在抛出之后继续.

    private void getData() throws InvalidEmployeeTypeException{

    System.out.println("Enter filename: ");
    Scanner prompt = new Scanner(System.in);

    inp = prompt.nextLine();

    File inFile = new File(inp);
    try {
        input = new Scanner(inFile);
    } catch (FileNotFoundException ex) {
        ex.printStackTrace();
        System.exit(1);
    }

    String type, name;
    int year, salary, hours;
    double wage;
    Employee e = null;


    while(input.hasNext()) {
        try{
        type = input.next();
        name = input.next();
        year = input.nextInt();

        if (type.equalsIgnoreCase("manager") || type.equalsIgnoreCase("staff")) {
            salary = input.nextInt();
            if (type.equalsIgnoreCase("manager")) {
                e = new Manager(name, year, salary);
            } …
Run Code Online (Sandbox Code Playgroud)

java exception-handling throw

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

抛出异常后我是否必须休息?

我正在用C#编写一个自定义类,如果人们在某些方法中给出错误的输入,我会抛出几个例外.如果抛出异常,抛出后方法中的任何代码仍会被执行吗?我必须在投掷后休息一下,还是投掷总是退出方法?

.net c# exception break throw

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

使用arg捕获的异常抛出和抛出有什么区别?

想象一下两个相似的代码片段:

try {
  [...]
} catch (myErr &err) {
  err.append("More info added to error...");
  throw err;
}
Run Code Online (Sandbox Code Playgroud)

try {
  [...]
} catch (myErr &err) {
  err.append("More info added to error...");
  throw;
}
Run Code Online (Sandbox Code Playgroud)

这些是否实际相同,或者它们是否以某种微妙的方式不同?例如,第一个是否导致复制构造函数运行,而第二个可能重用相同的对象来重新抛出它?

c++ exception try-catch throw

21
推荐指数
2
解决办法
6250
查看次数

我无法使用THROW SQL Server 2008 R2

它说,SQL Server 2008 R2 Management Studio无法识别下面的例子

Throw附近的语法错误

我试图在这里抛出一个错误,所以当有人插入两次相同的值时,我可以在我的网站上处理它.

Begin Try
 insert into BusinessID (BusinessID) values (@ID)
 insert into BusinessID (BusinessID) values (@ID)

End Try

Begin Catch

Print 'PK already exist'
THROW
End Catch
Run Code Online (Sandbox Code Playgroud)

sql sql-server try-catch throw sql-server-2008-r2

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