TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C# DivideByZeroException

This C# exception article demonstrates the DivideByZeroException.

DivideByZeroException. A DivideByZeroException is thrown.

It indicates that a statement attempted to evaluate a division by zero. The C# compiler can detect divisions by the constant zero value. The CLR can throw the exception during program execution.

Divide

Example. First, this program shows how an exception is thrown by the execution engine when you divide an int by zero. It uses a zero value determined at runtime through a parsing method, so that the program can be correctly compiled.

Int

Then: We discuss issues related to the compiler system and dividing a number by zero.

Based on:

.NET 4

C# program that divides by zero

using System;

class Program
{
    static void Main()
    {
	//
	// This expression evaluates to 100 / 0, which throws.
	// ... You can check the denominator in a separate step.
	//
	int result = 100 / int.Parse("0");
	Console.WriteLine(result);
    }
}

Output

Unhandled Exception: System.DivideByZeroException: Attempted to divide by zero.
   at Program.Main() in ... Program.cs:line 11

This program is meant to throw a System.DivideByZeroException. It uses the int.Parse("0") method to receive a zero value. It avoids using a zero constant because the C# compiler would detect that error before runtime (see below section).

Parse

The division is evaluated to 100 / 0, which of course cannot be done by the execution engine. The program terminates by throwing a DivideByZeroException. To avoid the exception, you must always test the denominator value for zero.

Tip: Alternatively you can use try and catch around the problematic division expression.

TryCatch

Discussion. Let's discuss the C# compiler's capacity to detect division by zero errors. If you try to divide by a constant zero (the number 0 in source code), the program will fail to compile and can never be executed.

The compiler does this to reduce errors in finished programs that were not tested. This can be confusing because it exposes how the system uses different compilation phases. The divide by zero error can be detected on different levels.

Compile-Time Error

Error 1:

Division by constant zero

Summary. We demonstrated the DivideByZeroException in the C# language, which is detected at two different stages: compile-time and runtime. This exception is built into the C# language itself. The specification contains the exact requirements.

Tip: To avoid the divide by zero exception, you obviously must check the denominator against the zero value.

And: This numeric check will be much faster than having an exception thrown in the runtime.


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf