DivideByZeroException代码漏洞 [英] DivideByZeroException code flaw

查看:60
本文介绍了DivideByZeroException代码漏洞的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请用c#帮我处理以下程序。



Please help me with the below program in c#.

static void Main(string[] args)
{
int a, b;
a= 100; b=0; 
try
{
   Console.WriteLine("before");
   Console.Write(a/b);
   Console.WriteLine("after");
}
catch (Exception ex)
{
   Console.WriteLine("Exception");
}
catch (DivideByZeroException ex)
{
   Console.WriteLine("DivideByZeroException");
}
finally
{
   Console.WriteLine("Finally");
}
   Console.WriteLine("End";);
}





a)代码存在结构缺陷。你能解释一下吗?如何纠正它?

b)纠正后,执行后会显示哪个文本? Justify



谢谢:)



其他信息已移动已复制来自下面的评论

我自己已经尝试过了。我无法弄清楚结构上的缺陷..但显示执行的文本我认为它应该在之前,dividebyzeroexception,最后和结束。如果我的理解是正确的,请告诉我。我是c#编程的新手。虽然我想答案我很怀疑是以公开形式发布的。



a)There is a structure flaw on the code. Can you explain it and how to correct it?
b)After correcting, which text would be shown after execution? Justify

Thanks :)

additional information moved copied from comment below
I already tried it myself. I couldn't figure out the structural flaw..but the text shown for execution I think it should be before,dividebyzeroexception,finally and end. Please let me know if my understanding is correct. I am new to c# programming. Though I guess an answer I am doubtful to post it in the public form.

推荐答案

这里有语法错误:
Console.WriteLine("End";);

额外的分号将阻止编译。



结构错误是你的第一个异常捕获是将捕获所有错误,并处理它们,因此编译器不知道该做什么与第二个catch,并将导致此错误:前一个catch子句已经捕获了这个或超类型的所有异常( 'System.Exception')



如果你把更具体的catch子句用于之前的除零错误 - 所有catch子句,这将工作:

The extra semi-colon will prevent compiling.

The structural error is that your first catch of the exception is going to catch all errors, and handle them, so the compiler "doesn't know what to do" with the second catch, and will cause this error: "A previous catch clause already catches all exceptions of this or of a super type ('System.Exception')"

If you put the more specific catch clause for a divide-by-zero error before the catch-everything catch clause, this will work:

catch (DivideByZeroException ex)
{
    Console.WriteLine("DivideByZeroException");
}
catch (Exception ex)
{
    Console.WriteLine("Exception");
}


这不是CP通常的工作方式。这里最重要的目标是学习和帮助学习。

你应该自己尝试,当你遇到困难时来到这里,对你的代码,设计等有一个具体的问题。

请查看你有什么尝试? [ ^ ]至看看我的意思是一个很好的解释。

不要忘记这里的人不会得到报酬。此外,如果我们给你一个随时可用的解决方案,它不会帮助你,因为你不会从中学到任何东西。



In另外,我们不会家庭作业 [ ^ ]



您可以尝试猜测并解释它,然后问问题,你会得到你的答案......你发布的内容意味着:请为我做我的工作
This is not how CP usually works. Most important goal here is to learn and help learning.
You are supposed to try it on your own, and come here when you got stuck with something, with a concrete question about your code, design, etc.
Please have a look to What have you tried?[^] to see a good explanation about what I mean.
Don't forget people here don't get payed. And besides, if we give you a ready-to-go solution, it is not going to help you because you are not going to learn anything from it.

In addition, we don't do Homework[^]

You can try it giving a guess and an explanation of it, then ask something and you will get your answers... What you posted means: "please do my work for me"


这篇关于DivideByZeroException代码漏洞的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆