Section 5 Errors

1918 days ago, 651 views
PowerPoint PPT Presentation
Cambridge. . Stroustrup/Programming. 2. CERN. . Stroustrup/Programming. 3. Unique. When we program, we need to manage mistakes. Our most essential point is rightness, however we must manage deficient issue details, inadequate projects, and our own particular mistakes. Here, we\'ll focus on a key territory: how to manage sudden capacity contentions. We\'ll additionally talk about systems for discovering blunder

Presentation Transcript

Slide 1

Part 5 Errors Bjarne Stroustrup

Slide 2

Abstract When we program, we need to manage mistakes. Our most essential point is rightness, yet we should manage inadequate issue particulars, fragmented projects, and our own particular mistakes. Here, we'll focus on a key range: how to manage unforeseen capacity contentions. We'll additionally talk about strategies for discovering mistakes in projects: investigating and testing. Stroustrup/Programming Sep'10

Slide 3

Overview Kinds of blunders Argument checking Error announcing Error recognition Exceptions Debugging Testing Stroustrup/Programming Sep'10

Slide 4

Errors " … I understood that starting now and into the foreseeable future a huge piece of my life would be spent finding and remedying my own slip-ups." Maurice Wilkes, 1949 When we compose programs, mistakes are normal and unavoidable; the question is, how would we manage them? Compose programming to limit blunders. Wipe out the greater part of the blunders we made at any rate. Troubleshooting Testing Make beyond any doubt the rest of the mistakes are not genuine. My figure is that abstaining from, finding, and revising blunders is at least 95% of the exertion for genuine programming improvement. You can improve for little projects. then again more regrettable, in case you're messy Stroustrup/Programming Sep'10

Slide 5

Your Program Should create the wanted outcomes for every lawful info Should give sensible mistake messages for unlawful sources of info Need not stress over getting out of hand equipment Need not stress over acting mischievously framework programming Is permitted to end subsequent to finding a blunder 3, 4, and 5 are valid for fledgling's code; regularly, we need to stress over those in genuine programming. Stroustrup/Programming Sep'10

Slide 6

Sources of blunders Poor detail "What's this expected to do?" Incomplete projects "however I'll not get around to doing that until tomorrow" Unexpected contentions "yet sqrt() should be called with - 1 as its contention" Unexpected information "yet the client should include a whole number" Code that just doesn't do what it should do "so settle it!" Stroustrup/Programming Sep'10

Slide 7

Kinds of Errors Compile-time mistakes Syntax mistakes Type blunders Link-time mistakes Run-time mistakes Detected by PC (crash) Detected by library (exemptions) Detected by client code Logic mistakes Detected by software engineer (code runs, yet delivers mistaken yield) Stroustrup/Programming Sep'10

Slide 8

Check your contributions Before attempting to utilize an info esteem, watch that it lives up to your desires/necessities Function contentions Data from info ( istream ) Stroustrup/Programming Sep'10

Slide 9

Bad capacity contentions The compiler numbers: and sorts of contentions must match int range( int length, int width) { return length*width; } int x1 = area(7); //blunder: wrong number of contentions int x2 = area("seven", 2); //blunder: 1 st contention has a wrong sort int x3 = area(7, 10); //alright int x5 = area(7.5, 10); //alright, yet unsafe: 7.5 truncated to 7; //most compilers will caution you int x = area(10, - 7); //this is a troublesome case: //the sorts are right ,/yet the qualities have neither rhyme nor reason Stroustrup/Programming Sep'10

Slide 10

Bad Function Arguments So, how about int x = area(10, - 7 ); ? Options Just don't do that Rarely a tasteful answer The guest ought to check Hard to do efficiently The capacity ought to check Return a "mistake esteem" (not general, dangerous) Set a blunder status marker (not general, tricky – don't do this) Throw a special case Note: some of the time we can't change a capacity that handles mistakes in a way we don't care for Someone else composed it and we can't or don't have any desire to change their code Stroustrup/Programming Sep'10

Slide 11

Bad capacity contentions Why stress? You need your projects to be right Typically the essayist of a capacity has no power over how it is called Writing "do it along these lines" in the manual (or in remarks) is no arrangement – many individuals don't read manuals The start of a capacity is frequently a decent place to check Before the calculation gets convoluted When to stress? In the event that it doesn't bode well to test each capacity, test some Stroustrup/Programming Sep'10

Slide 12

How to report a blunder Return a "mistake esteem" (not general, dangerous ) int region( int length, int width)/give back a negative an incentive for terrible information { if(length <=0 || width <= 0) return - 1; return length*width; } So, "let the guest be careful" int z = range( x,y ); if (z<0) error("bad region computation"); //… Problems What on the off chance that I neglect to watch that arrival esteem ? For a few capacities there isn't an "awful esteem" to return (e.g. max()) Stroustrup/Programming Sep'10

Slide 13

How to report a mistake Set a blunder status marker (not general, risky, don't!) int errno = 0; //used to show mistakes int zone( int length, int width) { if (length<=0 || width<=0) errno = 7; //|| implies or return length*width; } So, "let the guest check" int z = territory( x,y ); if ( errno ==7) error("bad zone computation"); //… Problems What on the off chance that I neglect to check errno ? How would I pick an incentive for errno that is unique in relation to all others? How would I manage that mistake? Stroustrup/Programming Sep'10

Slide 14

How to report a blunder Report a mistake by tossing a special case class Bad_area { };/a class is a client characterized sort/Bad_area is a sort to be utilized as an exemption int territory( int length, int width) { if (length<=0 || width<=0) toss Bad_area ();/take note of the () return length*width; } Catch and manage the blunder (e.g., in principle() ) attempt { int z = range( x,y ); //if zone() doesn't toss a special case } //make the task and continue get( Bad_area ) { //if region() tosses Bad_area (), react cerr << "oops! Awful territory estimation – settle program\n"; } Stroustrup/Programming Sep'10

Slide 15

Exceptions Exception taking care of is general You can't disregard a special case: the program will end on the off chance that somebody doesn't deal with it (utilizing an attempt … get ) Just about each sort of mistake can be accounted for utilizing exemptions despite everything you need to make sense of what to do around an exemption (each exemption tossed in your program) Error taking care of is never truly basic Stroustrup/Programming Sep'10

Slide 16

Out of range Try this vector< int > v(10); //a vector of 10 int s , //each introduced to the default esteem, 0 , //alluded to as v[0] .. v[9] for ( int i = 0; i < v.size (); ++ i ) v[ i ] = i ; //set qualities for ( int i = 0; i <=10; ++ i ) //print 10 values (???) cout << "v[" << i << "] == " << v[ i ] << endl ; vector's operator[ ] (subscript administrator) reports a terrible list (its contention) by tossing a Range_error on the off chance that you utilize #include " std_lib_facilities.h " The default conduct can contrast Stroustrup/Programming Sep'10

Slide 17

Exceptions – for the time being For now, simply utilize special cases to end programs effortlessly, similar to this int primary () try {/… } catch ( out_of_range &) { //out_of_range exemptions cerr << "oops – some vector file out of range\n"; } catch (… ) { //every other special case cerr << "oops – some exception\n"; } Stroustrup/Programming Sep'10

Slide 18

A capacity blunder() Here is a straightforward mistake() work as gave in std_lib_facilities.h This permits you to print a mistake message by calling blunder() It works by camouflaging tosses, this way: void error(string s) //one mistake string { throw runtime_error (s ); } void error(string s1, string s2) //two mistake strings { error(s1 + s2); /connects } Stroustrup/Programming Sep'10

Slide 19

Using blunder( ) Example cout << "please enter number in range [1..10]\n"; int x = - 1; //introduce with unsuitable esteem (if conceivable) cin >> x; if (! cin )/watch that cin read a whole number blunder ("didn't get a value"); if (x < 1 || 10 < x)/look at if esteem is of range mistake ("x is out of range");/in the event that we get this far, we can utilize x with certainty Stroustrup/Programming Sep'10

Slide 20

How to search for blunders When you have composed (drafted?) a program, it'll have mistakes (ordinarily called "bugs") It'll accomplish something, yet not what you expected How would you discover what it really does? How would you remedy it? This procedure is generally called "investigating" Stroustrup/Programming Sep'10

Slide 21

Debugging How not to do it while ( program doesn't seem to work ) {/pseudo code Randomly take a gander at the program for something that "looks odd" Change it to "look better" } Key question How might I know whether the program really worked accurately? Stroustrup/Programming Sep'10

Slide 22

Program structure Make the program simple to peruse with the goal that you have a possibility of detecting the bugs Comment Explain plan thoughts Use significant names Indent Use a reliable design Your IDE tries to help (yet it can't do everything) You are the one answerable Break code into little capacities Try to maintain a strategic distance from capacities longer than a page Avoid confounded code groupings Try to evade settled circles, settled if-articulations, and so on. (In any case, clearly, you now and then need those) Use library offices Stroustrup/Programming Sep'10

Slide 23

First get the program to order Is each string strict ended? cout << "Hello, << name << '\n'; /oh no ! Is each character strict ended? cout << "Hello, " << name << '\n; /uh oh ! Is each piece ended? in the event that (a>0) {/* accomplish something */else {/* accomplish something else */}/oh no ! Is each arrangement of brackets coordinated? on the off chance that (a //oh no ! x = f(y); The co