Divide By Zero Error In Vb.net
The Decimal data type in VB.NET 3 Option Infer 4 How a VB.NET Structure Is Different From a… 5 Coding the Double Linked List "The Hard Way" About.com About Tech Visual Basic Quick Tips for VB Programmers NaN, Infinity, and Divide by Zero in VB.NET VB.NET Constants and Structured Error Handling. By Dan Mabbutt Visual Basic Expert Share Pin Tweet Submit Stumble Post Share By Dan Mabbutt Beginning programming books usually include this warning: "Don't divide by zero! You'll get a runtime error!"But things have changed in VB.NET! And, although there are more programming options (and the calculation is more accurate) it isn't always easy to see why things happen the way they do. In this article, we learn how to handle division by zero using VB.NET's structured error handling. And along the way, we also cover the new VB.NET constants: NaN, Infinity and Epsilon.If you run a 'divide by zero' scenario in VB.NET, you get this result: Dim a, b, c As Double a = 1 : b = 0 c = a / b Console.WriteLine( _ "Have math rules" _ & vbCrLf & _ "been repealed?" _ & vbCrLf & _ "Division by zero " _ & vbCrLf & _ "must be possible!") So what's going on here? The answer is that VB.NET actually gives you the mathematically correct answer. Mathematically, you can divide by zero, but what you get is "infinity". Dim a, b, c As Double a = 1 : b = 0 c = a / b Console.WriteLine( _ "The answer is: " _ & c) ' Displays: ' The answer is: infinity continue reading below our video What is Virtual Reality? The value "infinity" isn't too useful for most business applications. (Unless the CEO is wondering what the upper limit on his stock bonus is.) But it does keep your applications from crashing on a runtime exception like less powerful languages do.VB.NET gives you even more flexibility by even allowing you to perform calculations. Check this out: Dim a, b, c As Double a = 1 : b = 0 c = a / b c = c + 1 ' Infinity plus 1 is ' still infinity To remain mathematically correct, VB.NET gives you the answer NaN (Not a Number) for some calculations such as 0 / 0. Dim a, b, c As Double a = 0 : b = 0 c = a / b Console.WriteLine( _ "The answer is: " _ & c) ' Displays: ' The answer is: NaN VB.NET can also tell the difference between positive infinity and negative infinity: Dim a1, a2, b, c As Double a1 = 1 : a2 = -1 : b = 0 If (a1 / b) > (a2 / b) Then _ Console.WriteLine( _ "Postive infinity is" _ & vbCrLf & _ "greater than" _ & vbCrLf & _ "negative infinity.") In addition to PositiveInfinity and NegativeInfinity, VB.NET als
resources Windows Server 2012 resources Programs MSDN subscriptions Overview Benefits Administrators Students Microsoft Imagine Microsoft Student Partners ISV Startups TechRewards Events Community Magazine Forums Blogs Channel 9 Documentation APIs and reference Dev centers Retired content Samples We’re sorry. The content you requested has been removed. You’ll be auto redirected in 1 second. MSDN Library MSDN Library MSDN Library MSDN Library Design Tools Development Tools and Languages Mobile and Embedded Development .NET Development Office development Online Services Open Specifications patterns & practices Servers and Enterprise Development Speech Technologies http://visualbasic.about.com/od/quicktips/qt/DivZeroNaN.htm Web Development Windows Desktop App Development TOC Collapse the table of content Expand the table of content This documentation is archived and is not being maintained. This documentation is archived and is not being maintained. Division by zero (Visual Basic Error) Other Versions Visual Studio 2008 Visual Studio 2005 The value of an expression used as a divisor is zero.To https://msdn.microsoft.com/en-us/library/8syw9cx3.aspx correct this errorCheck the spelling of variables in the expression. A misspelled variable can implicitly create a numeric variable initialized to zero.Check previous operations on variable in the expression, especially those passed into the procedure as arguments from other procedures.See AlsoError Types (Visual Basic) Show: Inherited Protected Print Export (0) Print Export (0) Share IN THIS ARTICLE Is this page helpful? Yes No Additional feedback? 1500 characters remaining Submit Skip this Thank you! We appreciate your feedback. Dev centers Windows Office Visual Studio Microsoft Azure More... Learning resources Microsoft Virtual Academy Channel 9 MSDN Magazine Community Forums Blogs Codeplex Support Self support Programs BizSpark (for startups) Microsoft Imagine (for students) United States (English) Newsletter Privacy & cookies Terms of use Trademarks © 2016 Microsoft © 2016 Microsoft
Error If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to http://www.vbforums.com/showthread.php?620880-RESOLVED-Division-By-Zero-Error visit from the selection below. Results 1 to 5 of 5 Thread: [RESOLVED] 'Division By Zero' Error Tweet Thread Tools Show Printable Version Subscribe to this Thread… Display Linear Mode Switch to Hybrid Mode Switch to Threaded Mode Jul 8th, http://www.deitel.com/articles/visualbasic2005_tutorials/20060512/ 2010,04:38 AM #1 Jonney View Profile View Forum Posts Thread Starter Frenzied Member Join Date Jan 2010 Posts 1,039 [RESOLVED] 'Division By Zero' Error VB Report 'Run-Time error 11' ('Division By Zero' Error) in VB6/SP6/XP SP3. What is the reason? Code: divide by Dim yOff As Double, dblVPos As Double, dblZoom As Double dblVPos = 101.196264584032 dblZoom = 0.486455791667741 yOff = -(dblVPos Mod dblZoom) Reply With Quote Jul 8th, 2010,04:46 AM #2 Lightning View Profile View Forum Posts Frenzied Member Join Date Oct 2002 Location Eygelshoven Posts 1,611 Re: 'Division By Zero' Error Mod, in vb6 works with integers not with double. so .48nnn is converted to 0 VB6 & C# (WCF LINQ) mostly If you need help with a WPF/WCF question post in the divide by zero NEW WPF & WCF forum and we will try help the best we can My site My blog, couding troubles and solutions Free online tools Reply With Quote Jul 8th, 2010,06:07 AM #3 Hack View Profile View Forum Posts Super Moderator Join Date Aug 2001 Location Searching for mendhak Posts 58,335 Re: 'Division By Zero' Error If the number to the left of the decimal is 0, and in this case it is, you will always get that error regardless of how the variables are declared. You can not divide buy 0...that is the reason for the error. The error will ALWAYS happen when attempting to divide by 0. The only way to fix it is to not attempt to divide by 0 Please use [Code]your code goes in here[/Code] tags when posting code. When you have received an answer to your question, please mark it as resolved using the Thread Tools menu. Before posting your question, did you look here? Got a question on Linux? Visit our Linux sister site. I dont answer coding questions via PM or EMail. Please post a thread in the appropriate forum section. Creating A Wizard In VB.NET Paging A Recordset What is wrong with using On Error Resume Next Good Article: Language Enhancements In Visual Basic 2010 Upgrading VB6 Code To VB.NET Microsoft MVP 2005/2006/2007/2008/2009/2010/2011/2012/Defrocked Reply With Quote Jul 8th, 2010,06:58 AM #4 anhn View Profile View Forum Posts Head Hunted Join Date Aug 2007 Locat
the problem occurs infrequently—if the "rule" is that a statement normally executes correctly, then the "exception to the rule" is that a problem occurs. Exception handling enables programmers to create applications that can resolve (or handle) exceptions. In many cases, handling an exception allows a program to continue executing as if no problem had been encountered. A more severe problem could prevent a program from continuing normal execution, instead requiring it to notify the user of the problem before terminating in a controlled manner. Exception handling enables programmers to write robust and fault-tolerant programs (i.e., programs that are able to deal with problems that may arise and continue executing). This tutorial demonstrates a program in which a exceptions occur. The program does not process these exceptions, which results in difficult-to-understand error messages being displayed to the user. In Part 2 of this tutorial, we demonstrate how to catch and handle these exceptions. This tutorial is intended for students and professionals who are familiar with classes and basic inheritance concepts in Visual Basic .NET or Visual Basic 2005. Download the code examples for this tutorial here. [Note: This three-part tutorial is an excerpt (Sections 12.3-4) of Chapter 12, Exception Handling, from our forthcoming textbook Visual Basic 2005 How to Program, 3/e. This tutorial may refer to other chapters or sections of the book that are not included here. Permission Information: Deitel, Harvey M. and Paul J., Visual Basic 2005 How to Program, ©2005. Electronically reproduced by permission of Pearson Education, Inc., Upper Saddle River, New Jersey.] 12.3 Example: Divide by Zero Without Exception Handling First we demonstrate what happens when errors arise in a console application that does not use exception handling. Figure 12.1 inputs two integers from the user, then divides the first integer by the second using integer division to obtain an Integer result. In this example, we will see that an exception is thrown (i.e., an exception occur