标签: try-catch

C++ try/throw/catch =>机器码

在心理上,当C++编译将其转换为汇编程序时,我总是想知道try/throw/catch在幕后是怎么看的.但是因为我从来没有使用它,所以我从来没有去过它(有些人会说懒惰).

是用于跟踪trys 的普通堆栈,还是仅为此目的而保留的单独的每线程堆栈?MSVC和g ++之间的实现是大还是小?请告诉我一些伪asm(IA-32也可以)所以我永远不必自己查看!:)

编辑:现在我了解了基于IA-32处理的MSVC实现的基础知识.任何人都知道在IA-32或其他任何CPU上的g ++?

c++ assembly exception-handling try-catch

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

如何捕获空指针异常?

  try {
        int* p = 0;
        *p = 1;
    } catch (...) {
        cout << "null pointer." << endl;
    }
Run Code Online (Sandbox Code Playgroud)

我试图抓住这样的例外,但它没有用,有什么帮助吗?

c++ exception-handling try-catch

41
推荐指数
5
解决办法
7万
查看次数

除了在python中包装try的一般装饰器?

我正在与许多深度嵌套的json进行交互,我没有写,并且想让我的python脚本对无效输入更"宽容".我发现自己编写了涉及try-except块的内容,而宁愿将这个可疑的函数包装起来.

我理解吞下异常是一个糟糕的政策,但我宁愿他们稍后打印和分析,而不是实际停止执行.在我的用例中继续执行循环而不是获取所有密钥更有价值.

这就是我现在正在做的事情:

try:
    item['a'] = myobject.get('key').METHOD_THAT_DOESNT_EXIST()
except:
    item['a'] = ''
try:
    item['b'] = OBJECT_THAT_DOESNT_EXIST.get('key2')
except:
    item['b'] = ''
try:
    item['c'] = func1(ARGUMENT_THAT_DOESNT_EXIST)
except:
    item['c'] = ''
...
try:
    item['z'] = FUNCTION_THAT_DOESNT_EXIST(myobject.method())
except:
    item['z'] = ''
Run Code Online (Sandbox Code Playgroud)

这是我想要的,(1):

item['a'] = f(myobject.get('key').get('subkey'))
item['b'] = f(myobject.get('key2'))
item['c'] = f(func1(myobject)
...
Run Code Online (Sandbox Code Playgroud)

或(2):

@f
def get_stuff():
   item={}
   item['a'] = myobject.get('key').get('subkey')
   item['b'] = myobject.get('key2')
   item['c'] = func1(myobject)
   ...
   return(item)
Run Code Online (Sandbox Code Playgroud)

...我可以将单个数据项(1)或主函数(2)包装在某个函数中,该函数将执行暂停异常转换为空字段,打印到stdout.前者将是一种逐项跳过 - 其中该键不可用,它记录为空白并继续前进 - 后者是行跳过,如果任何字段不起作用,则整个记录为跳过.

我的理解是某种包装应该能够解决这个问题.这是我尝试过的包装器:

def f(func):
   def silenceit():
      try:
         func(*args,**kwargs)
      except:
         print('Error')
      return(silenceit)
Run Code Online (Sandbox Code Playgroud)

这就是为什么它不起作用.调用一个不存在的函数,它不会尝试捕获它:

>>> …
Run Code Online (Sandbox Code Playgroud)

python try-catch decorator wrapper

41
推荐指数
7
解决办法
3万
查看次数

使用Try/Catch PHP方法捕获条带错误

在网站上测试STRIPE期间,我构建了这样的代码:

   try {
        $charge = Stripe_Charge::create(array(
          "amount" => $clientPriceStripe, // amount in cents
          "currency" => "usd",
          "customer" => $customer->id,
          "description" => $description));
          $success = 1;
          $paymentProcessor="Credit card (www.stripe.com)";
    } 
    catch (Stripe_InvalidRequestError $a) {
        // Since it's a decline, Stripe_CardError will be caught
        $error3 = $a->getMessage();
    }

    catch (Stripe_Error $e) {
        // Since it's a decline, Stripe_CardError will be caught
        $error2 = $e->getMessage();
        $error = 1;
    }
if ($success!=1)
{
    $_SESSION['error3'] = $error3;
    $_SESSION['error2'] = $error2;
    header('Location: checkout.php');
    exit();
}
Run Code Online (Sandbox Code Playgroud)

问题是卡有时会出错(没有被我在那里的"catch"参数捕获)并且"try"失败并且页面立即在屏幕上发布错误而不是进入"if"和重定向回checkout.php. …

php error-handling try-catch stripe-payments

41
推荐指数
5
解决办法
5万
查看次数

当AutoCloseable为null时,尝试使用资源

try-with功能如何适用于AutoCloseable已声明的变量null

我假设这会在尝试调用close变量时导致空指针异常,但它运行没有问题:

try (BufferedReader br = null){
    System.out.println("Test");
}
catch (IOException e){
    e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)

java try-catch autocloseable

41
推荐指数
1
解决办法
8715
查看次数

为什么Try-Catch块会影响封闭范围内的变量?

为什么temp在捕获到第一个异常后外部变成空的?

#include <iostream>
int main()
{
    std::string temp("exception");
    int value;
    while(std::cin>> value && value != 0)
    {
         try{
              if(value > 9) throw temp;
              else std::cout << value << "\n";
            }
         catch(std::string temp)
         {
              std::cout << temp << "\n";
         }
    }

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

输入:

1
2
11
13
Run Code Online (Sandbox Code Playgroud)

输出:

1
2
exception
// Printing Empty string
Run Code Online (Sandbox Code Playgroud)

预期产量:

1
2
exception
exception
Run Code Online (Sandbox Code Playgroud)

我使用g ++ 7.3.0编译代码。

c++ try-catch

41
推荐指数
1
解决办法
2098
查看次数

获取/关闭资源时Java try/catch/finally最佳实践

在研究学校项目时,我写了以下代码:

FileOutputStream fos;
ObjectOutputStream oos;
try {
    fos = new FileOutputStream(file);
    oos = new ObjectOutputStream(fos);

    oos.writeObject(shapes);
} catch (FileNotFoundException ex) {
    // complain to user
} catch (IOException ex) {
    // notify user
} finally {
    if (oos != null) oos.close();
    if (fos != null) fos.close();
}
Run Code Online (Sandbox Code Playgroud)

问题是Netbeans告诉我resource.close()线路抛出IOException,因此必须被捕获或声明.它也抱怨oos并且fos可能尚未初始化(尽管无效检查).

这看起来有点奇怪,看到整个点是如何阻止IOException那里的权利.

我的下意识修复是这样做的:

} finally {
    try {
        if (oos != null) oos.close();
        if (fos != null) fos.close();
    } catch (IOException ex) { …
Run Code Online (Sandbox Code Playgroud)

java resources try-catch

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

在java中的try-catch块之后使用"finally"有什么好处?

当try-catch结束时,总是执行"finally"块,无论是否异常.但是,try-catch之外和之后的每一行代码总是被执行.那么,我为什么要使用finally语句呢?

例:

try {
    //code...
} catch (Exception e) {
    //code...
} finally {
    System.out.println("This line is always printed");
}
System.out.println("Also this line is always printed !! So why to use 'finally'?? ");
Run Code Online (Sandbox Code Playgroud)

java finally try-catch

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

如何在Python中编写一个空的缩进块?

运行时不断告诉我:

期望一个缩进的块

但是我不希望在我的except块中没有写任何内容,我只是希望它能够捕获并吞下异常.

python syntax try-catch indentation

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

如何将警告和错误保存为函数的输出?

我正在使用lapply大量项目上的复杂函数,我想保存每个项目的输出(如果有的话)以及生成的任何警告/错误,以便我可以告诉哪个项目产生了哪个警告/错误.

我找到了一种方法来捕捉警告withCallingHandlers(在此描述).但是,我也需要捕获错误.我可以将它包装在一个tryCatch(如下面的代码中),但是有更好的方法吗?

catchToList <- function(expr) {
  val <- NULL
  myWarnings <- NULL
  wHandler <- function(w) {
    myWarnings <<- c(myWarnings, w$message)
    invokeRestart("muffleWarning")
  }
  myError <- NULL
  eHandler <- function(e) {
    myError <<- e$message
    NULL
  }
  val <- tryCatch(withCallingHandlers(expr, warning = wHandler), error = eHandler)
  list(value = val, warnings = myWarnings, error=myError)
} 
Run Code Online (Sandbox Code Playgroud)

此函数的示例输出是:

> catchToList({warning("warning 1");warning("warning 2");1})
$value
[1] 1

$warnings
[1] "warning 1" "warning 2"

$error
NULL

> catchToList({warning("my warning");stop("my error")})
$value …
Run Code Online (Sandbox Code Playgroud)

error-handling r try-catch

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