标签: try-catch

在 Django 中尝试/除外,良好实践

这部分代码代表我网页的索引,例如:127.0.0.1:8000/

def IndexView(request):
    try:
        profile = request.user.get_profile() 
    except User.DoesNotExist:
        return render_to_response('index.html',
                              {'request': request,},
                              context_instance=RequestContext(request))

    return render_to_response('index.html',
                  {'request': request, 'profile' : profile},
                  context_instance=RequestContext(request))  
Run Code Online (Sandbox Code Playgroud)

为什么我在调试时仍然收到此错误?

AttributeError at / 'AnonymousUser' 对象没有属性 'get_profile'

提前致谢

django exception-handling try-catch

0
推荐指数
1
解决办法
6051
查看次数

如果发生异常,如何继续循环迭代?

嘿,我已经完成了我的try-catch块,但有疑问.我想计算一个字符串包含多少个单词和数字.为此,我想使用try - catch块.我将String解析为int,如果输出为false,我想计算catch块逻辑中的字数.但是在程序进入catch块后,程序退出.我想继续捕获错误后循环遍历循环.我怎样才能做到这一点?

    Scanner odczyt = new Scanner(System.in);
    String zdanie = odczyt.nextLine();
    String[] podzdania = zdanie.split(" ");
    boolean exception = false;
    int numberword = 0;

    try {

        for (int i = 0; i < podzdania.length; i++) {
            Integer.parseInt(podzdania[i]);
        }

    } catch (NumberFormatException e) {

        numberword++;

    }
Run Code Online (Sandbox Code Playgroud)

java loops try-catch

0
推荐指数
1
解决办法
178
查看次数

真的需要块吗?

如果将要执行catch块之后的语句,那么java中finally块的真正用途是什么?例

 try {
 //Code
}
catch (Exception e)
{
//Code
}
finally {
 System.out.println("anyway it will be        executed");
 }

System.out.println("anyway it will be executed");
Run Code Online (Sandbox Code Playgroud)

java finally try-catch

0
推荐指数
1
解决办法
96
查看次数

catch块中是否捕获已捕获的JavaScript异常?

try {
    await function1()
    await function2()
    await function3().catch(err => { /*...*/ })
} catch (err) { /*...*/ }
Run Code Online (Sandbox Code Playgroud)

我的问题是,即使异常已被" catch捕获" ,块是否捕获了由此发出function3的异常.catch(err => ...)


如果catch块捕获异常function3,我的问题是如何防止这种行为?

我的目标基本上是捕获这些函数的特定异常,并将catch块用于每个未处理的异常.

javascript error-handling try-catch

0
推荐指数
1
解决办法
269
查看次数

Java - Catch Exception e

e以下代码的含义是什么?

try {
    // Do something
} catch (Exception e) {
    // Do something
}
Run Code Online (Sandbox Code Playgroud)

我一直在研究,一无所获.

System.out.println("Thanks!");

java exception try-catch

0
推荐指数
1
解决办法
495
查看次数

try-with-resources fails but try works

I'm trying to set up a service which listens to a RabbitMQ server and I've set up code using the RabbitMQ Sample code from Github, which includes the following try-with-resources block

try (Connection connection = factory.newConnection();
     Channel channel = connection.createChannel()) {
        // code here
}
Run Code Online (Sandbox Code Playgroud)

When I use the same code and build and run this service using java -cp myJar.jar MyService, it just starts and ends immediately (and echo $? returns 0)

However, if I replace the …

java try-catch rabbitmq try-with-resources java-11

0
推荐指数
1
解决办法
57
查看次数

一次尝试捕获多个错误

因此,尝试在PowerShell中对服务器上的文件夹进行简单try/ catch更新共享访问。

try {
    Grant-SmbShareAccess -Name [FolderName] -AccountName [GroupToShare] -AccessRight Read -Force
    Grant-SmbShareAccess -Name [FolderName] -AccountName [GroupToShare] -AccessRight Read -Force
} catch {
    Write-Host "Error Granting one or more permission: $_" -ForegroundColor DarkMagenta
}
Run Code Online (Sandbox Code Playgroud)

是否可以捕获特定的授权访问语句并打印失败的组名。

说我有两个小组:

  • 不适用\管理员
  • NA \ dev

并且这Grant-SmbShareAccess两个组都失败了,我可以捕获并打印出:

权限失败:NA \ admin
权限失败:NA \ dev

只用一个试钩?

powershell file-permissions try-catch

0
推荐指数
1
解决办法
38
查看次数

在哪些情况下,try-catch会捕获异常?

我刚刚开始学习Scala,所以这可能是一个简单的问题。我想使用try-catch块来检查变量是否已声明。

我正在使用try-catch块,并NoSuchElementException在变量不存在时捕获。

try{
  print(testVariable)
}
catch{
  case e: NoSuchElementException => print("testVariable not found")
}
Run Code Online (Sandbox Code Playgroud)

我的代码显示了一个testVariable不存在的错误,而不是引发异常。然后,我也尝试了多个其他异常,但是Scala的try-catch似乎没有捕获任何异常(除以零的异常除外)。

有人可以指导我如何使用Scala的try-catch块吗?

scala exception try-catch

0
推荐指数
1
解决办法
65
查看次数

尝试在main()范围之外的catch块

我能够编译并运行它。如何在主作用域之外编写try-catch块,这似乎违背了我的逻辑?有什么术语可以描述这种行为?

int main() try
{

}

catch(...){}
Run Code Online (Sandbox Code Playgroud)

c++ try-catch

0
推荐指数
1
解决办法
53
查看次数

引发多个异常时catch块执行的顺序是什么?为什么?

在以下代码中,该函数在一条语句中引发两个异常。现在,为什么int catch块处理异常而不是其他块?是否总是存在最后一个异常是要处理的异常的情况?


    try
    {
        quotient = safe_divide(numerator , denominator);
    }
    catch(DivideByZero)
    {
        cout << "Error: Division by zero!\n"
            << "Program aborting.\n";
        system("pause");
    }

    catch (int )
    {
        cout << "got you " << endl;
        cout << "top : " << numerator << endl;

        system("Pause");
        exit(0);
    }

    double safe_divide(int top, int bottom) throw(DivideByZero,int)
    {
        if(bottom == 0)
            throw (DivideByZero(),top);

        return top/static_cast<double>(bottom);
    }


Run Code Online (Sandbox Code Playgroud)

c++ exception try-catch throw

0
推荐指数
1
解决办法
68
查看次数