Get the App
SLTechnology News&Howtos  ›  Development  › 

How can C # throw an exception when divided by 00:00

Shulou Source: shulou.com Published: 2022-06-01 01:52:21 09月21日 Update

This article mainly introduces "how to achieve C# when divided by 00:00 when throwing an exception". In daily operation, I believe many people have doubts about how to achieve C# when divided by 00:00 when throwing an exception. The editor consulted all kinds of data and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubt that "how to achieve C# when divided by 00:00"! Next, please follow the editor to study!

Exception handling

C # provides a structured exception handling scheme in the form of try and catch blocks. Use these blocks to separate core program statements from error handling statements.

These error handling blocks are implemented using the try, catch, and finally keywords. The following is an example of an exception thrown when divided by 00:00:

Using System

Namespace ErrorHandlingApplication

{

Class DivNumbers

{

Int result

DivNumbers ()

{

Result = 0

}

Public void division (int num1, int num2)

{

Try

{

Result = num1 / num2

}

Catch (DivideByZeroException e)

{

Console.WriteLine ("Exception caught: {0}", e)

}

Finally

{

Console.WriteLine ("Result: {0}", result)

}

}

Static void Main (string [] args)

{

DivNumbers d = new DivNumbers ()

D.division (25,0)

Console.ReadKey ()

}

}

}

When the above code is compiled and executed, it produces the following results:

Exception caught: System.DivideByZeroException: Attempted to divide by zero. At... Result: at this point, the study on "how to implement C # when divided by 00:00 throws an exception" is over. I hope I can solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Tags: Caching processing learning more statements errors help practical next code keywords keywords examples forms articles schemes methods core theory knowledge Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB Redmi vpn Shulou Technology Microsoft