Exception Handling in C #

Source: Internet
Author: User

Exception Handling is similar to java syntax. First, let's talk about the exception classes used in it. These exception classes are often seen during java programming:

OverflowException: An exception caused by overflow due to arithmetic, type conversion, or conversion operations;

Try {checked // use the checked keyword {int i1; int i2; int num; i1 = 5000000; i2 = 5000000; num = i1 * i2 ;}} catch (OverflowException) {Console. writeLine ("OverflowException exception thrown ");}

* ******* The checked keyword is always being checked. An exception is caught when overflow is found;

DivideByZeroException: An exception occurs when the divisor is 0;

FormatException: An exception occurs when the input format does not meet the specifications;

ArithmeticException: An exception occurred during arithmetic operations;

IndexOutOfRangeException: An exception occurs when coordinates smaller than 0 or beyond the array limit are used;

NullReferenceExcption: an exception that occurs when the object to be referenced is null;

The bottom two exceptions are often seen when writing java programs...

I see an example of custom exception output in my book. I feel that the exception handling process is relatively simple:

Class Sec {public int myint (string a, string B) {int int1, int2, num; try {int1 = int. parse (a); int2 = int. parse (B); if (int2 = 0) {throw new DivideByZeroException (); // if the denominator is 0, an exception is thrown.} num = int1/int2; return num ;} catch (DivideByZeroException de) // catch exceptions {Console. writeLine ("an exception is thrown when a zero-division integer is used! "); Console. writeLine (de. message); return 0 ;}} static void Main1 (string [] args) {try {Console. writeLine ("Enter the molecule:"); string str1 = Console. readLine (); Console. writeLine ("Enter denominator:"); string str2 = Console. readLine (); Sec sec = new Sec (); Console. writeLine ("numerator divided by denominator:" + sec. myint (str1, str2);} catch (FormatException) {Console. writeLine ("Please enter the value format data ");}}}


This article from "tired also happy D" blog, please be sure to keep this source http://zhangzhang.blog.51cto.com/6250085/1261637

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.