Exception throw and handling

Source: Internet
Author: User

 

First, I declare that I am not a great Bull. This article is based on exchanges with the ox and my own experiences. If I understand what is different, I hope you can point it out !!!!!!

 

Let's look at an exception. The detailed code is as follows:

// Write a ticket class with a distance attribute (this attribute is read-only and assigned a value in the constructor). It cannot be a negative number. It has a price attribute and the price attribute is read-only, based on the distance calculation price (1 yuan/km): 0-9.5 km fare is not discounted, 300 off for the total-km, off for the total km, and off for the total km or more

// There is a way to display the ticket information.

 

Public class ticket

{

Int distance; // distance

Decimal price; // fare

// Method

Public ticket (INT distance)

{

// Assign a value

This. Distance = distance;

// Judge

If (distance> 300)

{

Price = distance * 0.8 m;

}

Else if (didistance> 200)

{

Price = distance * 0.9 m;

}

Else if (didistance> 100)

{

Price = distance * 0.95 m;

}

Else if (distance> 0)

{

Price = distance;

}

Else

{// An exception occurs when the input data is smaller than zero.

Throw new exception ("The Exception Code has occurred. The distance here cannot be negative ...");

}

}

Public void view ()

{

Console. writeline ("the distance of this ticket is {0}, discount price {1}", distance, this. Price );

}

Public int distance

{

Get {return this. distance ;}

}

Public decimal price

{

Get {return this. Price ;}

}

}

Call the main method of the main function.

Static void main (string [] ARGs)

{

While (true)

{

Console. writeline ("hello, please enter the distance ...");

Ticket T1;

Try

{

T1 = new ticket (convert. toint32 (console. Readline ()));

T1.view ();

}

Catch (exception ex)

{

Console. writeline (ex. Message );

}

Console. readkey ();

}

}

}

 

 

Model diagram of exception throw and handling:

1. when running to the ticket class, the system will first call the ticket constructor for initialization. When we check that the input data is smaller than zero, we will manually throw an exception, throw new exception ("The Exception Code has occurred. The distance here cannot be negative... "); new exception is to create a new exception object, throw is to throw this new exception. Note that the exception occurs at this time in the Ticket Class → the ticket constructor → else. At this time, the program will stay here and then throw the exception (who is calling it, if the exception is not handled, it will always die here.

2. Since an exception has been thrown out, we have to handle the exception to make the program continue to run. So how to handle it? First, we must first receive this exception before we can operate it. Then catch (catch) is used to catch this exception. Since it throws an exception object, it must be received by the exception object. Catch can be regarded as a method. exception is a parameter. It is easy to handle exceptions. There are many methods to handle exceptions, such as console. writeline () and logging .......

 

Note: What throws an exception is not the called function. That is to say, the exception thrown here is not the main method, but the convert. toint32 () program actually stops here ......

 

As for throwing an exception, it is thrown to the caller. We can throw the exception layer by layer on the console. writeline (ex. message), right-click on ex, and select "quick monitor"> stacktrace> text visualization tool. We can see four sections, from the bottom.

1. The bottom is where the error occurred, which file, and which line of the file can be seen by yourself.

2. The exception occurs in convert. toint32.

3. parseintformartinfo

4. stringtonumber. here we can see that the most primitive part of the exception throw should be here.

 

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.