开发者

Is this a VB.NET Arithmetic exception?

Okay, here is what I am ask开发者_运维技巧ed to do by my teacher. Write a basic program that uses one of the arithmetic exceptions (other than dividing a number by zero) modeling the programs you’ve seen in this lesson. Then, in a second program, write one that would not produce an error.

The one's in the lesson were DivideByZeroException, NotFiniteNumberException, and OverflowException.

EDIT: I cannot use DivideByZeroException.

I am a beginner so I don't know much about programming. I'm taking a high school class, so please be patient.


Bala's answer comes close:

Dim j As Integer = Integer.MaxValue + 1 '<-- doesn't compile

This would indeed overflow, but it doesn't compile because the compiler detects that we're trying to assign a value that doesn't fit inside an integer.

If we pass a variable to it, then the compiler doesn't make any assumption about the value of that variable, so the code compiles. It will fail with OverflowException at runtime.

Dim i As Integer = Integer.MaxValue
Dim j As Integer = i + 1 '<--  OverflowException at runtime


I'd recommend reading the documentation on ArithmeticException here:

http://msdn.microsoft.com/en-us/library/system.arithmeticexception.aspx

There are 3 subclasses of ArithmeticException, which you can read about in the link above.

  • DivideByZeroException
  • NotFiniteNumberException
  • OverflowException

The easiest one to produce is the DivideByZeroException - just divide an int by zero. That might be a good one to try first. For the other ones, read up on what causes them and try to create a scenario that matches the description.


Try

Dim j As Integer = Integer.MaxValue + 1

that will cause OverflowException

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜