为什么我不能处理更改表?
Begin Try
alter table nyork add [Qtr] varchar(20)
End Try
Begin Catch
Print 'Column already exist'
End Catch'
Run Code Online (Sandbox Code Playgroud) 这是我的代码的示例。我想在块if/else内使用现有的语句try-catch,并在验证失败时推送。我试图try-catch在之间的if/else条件下使用它,但它给了我一个错误。
var errLogs = [];
try {
var a = "ABCD";
if(typeof a === "string"){
console.log("equel");
}catch(e) {
}else{
console.error("not equel");
}
console.log(e);
errLogs.push(e);
}
Run Code Online (Sandbox Code Playgroud) 我试图返回 R 函数中的任何实际错误消息,并向其发送电子邮件以通知用户,但它仅打印 R 参数中的自定义消息。有什么办法可以在电子邮件中发送实际的错误消息吗?
以下是我迄今为止编写的虚拟脚本:
mailme <- function(message){
#function to send email
}
b<-function(){
r <- NULL
attempt <- 1
while( is.null(r) && attempt <= 3 ) {
attempt <- attempt + 1
try({
x<-2+3
prin(x)})
}
stop("The error message")
}
a <- tryCatch({
b()
}, error = function(e){
mailme(e$message)
})
Run Code Online (Sandbox Code Playgroud)
实际返回的错误信息是
Error in prin(x) : could not find function "prin"
Run Code Online (Sandbox Code Playgroud)
但是我在电子邮件中收到的错误消息是
The error message #from the stop used in function b
Run Code Online (Sandbox Code Playgroud)
如何在停靠点内调用实际的错误消息?
我正在学习例外,我发现你可以有压抑的例外。我在 stackoverflow 上读了很多例子,但它们仍然不能像在“try/finally”情况下那样工作:
public class MultipleExceptionsExample {
static class IOManip implements Closeable{
@Override
public void close() {
throw new RuntimeException("from IOManip.close");
}
}
public static void main(String[] args) {
try(IOManip ioManip = new IOManip()){
throw new RuntimeException("from try!");
}catch(Exception e){
throw new RuntimeException("from catch!");
}finally{
throw new RuntimeException("from finally!");
}
}
}
Run Code Online (Sandbox Code Playgroud)
正如许多人所解释的,我应该得到所有行:“java.lang.RuntimeException:来自finally!” (是的,我愿意)
删除finally块我应该得到:“java.lang.RuntimeException:来自catch!” (是的,我愿意)
删除 catch 块我应该得到:
Exception in thread "main" java.lang.RuntimeException: from try!
Suppressed: java.lang.RuntimeException: from IOManip.close
Run Code Online (Sandbox Code Playgroud)
我从来不这样做!为什么?我缺少什么?
通过删除 catch 块,我应该看到 try 消息,但我得到的是:
Exception in thread "main" …Run Code Online (Sandbox Code Playgroud) 这里,base可能会溢出超出int的限制,导致运行时错误,此时我打算捕获引发的运行时错误并处理它,所以我尝试了try-catch块,但它没有被捕获。
int base=1;
try
{
base *= 10;
//some code
}
catch(...)
{
//some code
}
Run Code Online (Sandbox Code Playgroud) 我想获取一个 HTML 页面并使用 BufferedReader 读取。所以我使用 try-with-resources 打开它以这种方式处理IOException :
try(BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()))) {
} catch(IOException e) {
throw e;
}
Run Code Online (Sandbox Code Playgroud)
这是一个很好的捕捉并立即抛出的模式吗?如果我完全省略 try 并声明该函数抛出 IOException 会怎样?那么是否有任何潜在的内存泄漏?非常感谢任何建议!
我的理解是,在现代 C++ 中,最好使用throw而不是返回错误代码。也就是说,如果我有一些功能:
void MyFunction(int foo, double bar){
// Do some stuff....
if (exception_criteria_met){
throw "Call to MyFunction with inputs" << foo << " and " << bar << " failed because ...";
}
};
Run Code Online (Sandbox Code Playgroud)
这是否意味着,为了正确处理这个问题,那么在所有调用 的函数中MyFunction,我必须实现如下所示的内容:
void MyOuterFunction(int foo){
// Do some stuff...
try {
MyFunction(foo, bar);
} catch (const char * exception_message) {
throw "Call to MyOuterFunction with inputs " << foo << " failed because call to MyFunction with ....";
};
Run Code Online (Sandbox Code Playgroud)
那么这是否意味着我必须对调用该函数调用堆栈的所有函数一直执行相同类型的错误处理? …