Tải bản đầy đủ - 0 (trang)
Chapter 69.  Establish a rational error handling policy, and follow it strictly

Chapter 69.  Establish a rational error handling policy, and follow it strictly

Tải bản đầy đủ - 0trang

Summary

Consciouslyspecify,andconscientiouslyapply,whatsomany

projectsleavetoad-hoc(mis)judgment:Developapractical,

consistent,andrationalerrorhandlingpolicyearlyindesign,

andthensticktoit.Ensurethatitincludes:

Identification:Whatconditionsareerrors.

Severity:Howimportantorurgenteacherroris.

Detection:Whichcodeisresponsiblefordetectingtheerror.

Propagation:Whatmechanismsareusedtoreportand

propagateerrornotificationsineachmodule.

Handling:Whatcodeisresponsiblefordoingsomething

abouttheerror.

Reporting:Howtheerrorwillbeloggedorusersnotified.

Changeerrorhandlingmechanismsonlyonmoduleboundaries.



Discussion

FromthisItemonward,thissectionfocusesondealingwith

run-timeerrorsthatarenotduetofaultycodinginternaltoa

moduleorsubsystem.(AsItem68coversseparately,preferto

useassertionstoflaginternalprogrammingerrors,onesthat

arejustoutrightcodingerrorsonsomeprogrammer'spart.)

Determineanoverallerrorreportingandhandlingpolicyfor

yourapplicationandforeachmoduleorsubsystem,andstickto

it.Includeapolicyforatleasteachofthefollowingpoints.

Universally:

Erroridentification:Foreachentity(e.g.,eachfunction,

eachclass,eachmodule),documenttheentity'sinternal

andexternalinvariants.

Foreachfunction:

Erroridentification:Foreachfunction,documentits

preconditionsandpostconditions,theinvariantsitshares

responsibilityformaintaining,andtheerror-safety

guaranteeitsupports.(SeeItems70and71.)Notethat

destructorsanddeallocationfunctionsinparticularmust

alwaysbewrittentosupporttheno-failguarantee,because

otherwiseit'softenimpossibletoreliablyandsafelyperform

cleanup(seeItem51).

Foreacherror(seethedefinitionof"error"inItem70):

Errorseverityandcategorization:Foreacherror,identifya

severitylevel.Preferablyprovideawaytofine-tune

diagnosticsforparticularerrorcategoriesandlevelsto



facilitateremoteuserassistance.

Errordetection:Foreacherror,documentwhichcodeis

responsiblefordetectingwhicherror,followingtheadviceof

Item70.

Errorhandling:Foreacherror,identifythecodethatis

responsibleforhandlingtheerror,followingtheadvicein

Item74.

Errorreporting:Foreacherror,identifyappropriate

reportingmethod(s).Thesecommonlyincluderecordingthe

errorindiskfilelogs,printedlogs,electronicdump

transmissions,orpossiblyinconvenientandannoyingpager

callsinthecaseofsevereerrors.

Foreachmodule:

Errorpropagation:Foreachmodule(note:eachmodule,

noteacherror),identifywhichcodingmechanismwillbe

usedtopropagateerrors(e.g.,C++exceptions,COM

exceptions,CORBAexceptions,returncodes).

Weemphasizethaterrorhandlingstrategiesshouldchangeonly

onmoduleboundaries(seeItems62and63).Eachmodule

shouldconsistentlyuseasingleerrorhandlingstrategyand

mechanisminternally(e.g.,moduleswritteninC++shoulduse

exceptionsinternally;seeItem72)andconsistentlyusea

single,possiblydifferent,errorhandlingstrategyand

mechanisminitsinterface(e.g.,themodulemightpresenta

flatCAPItoaccommodatecallersthatcouldbewrittenin

variouslanguages,oraCOMwrapperthatpresentsCOM

exceptions).

Allfunctionsthatareentrypointsintothemodulearedirectly

responsiblefortranslatingfromtheinternaltotheexternal



strategyiftheyaredifferent.Forexample,inamodulethat

usesC++exceptionsinternallybutpresentsaCAPIboundary,

allCAPIsmustcatch()allexceptionsandtranslatethemto

errorcodes.

Noteinparticularthatcallbackfunctionsandthreadmainlines

bydefinitionare(orcanbe)onamoduleboundary.Each

callbackfunctionbodyandthreadmainlinebodyshould

translateitsinternalerrormechanismtotheappropriate

interfaceerrorstrategy(seeItem62).



References

[Abrahams01b][Allison98]Đ13[McConnell93]Đ5.6

[Stroustrup94]Đ16.2,ĐE.2[Stroustrup00]Đ14.9,Đ19.3.1

[Sutter04b]



70.Distinguishbetweenerrorsandnonerrors

Summary

Discussion

Examples

References



Summary

Abreachofcontractisanerror:Afunctionisaunitofwork.

Thus,failuresshouldbeviewedaserrorsorotherwisebasedon

theirimpactonfunctions.Withinafunctionf,afailureisan

errorifandonlyifitviolatesoneoff'spreconditionsor

preventsffrommeetinganyofitscallees'preconditions,

achievinganyoff'sownpostconditions,orreestablishingany

invariantthatfsharesresponsibilityformaintaining.

Inparticular,hereweexcludeinternalprogrammingerrors(i.e.,

wherethecallerandcalleearetheresponsibilityofthesame

personorteam,suchasinsideamodule),whichareaseparate

categorynormallydealtwithusingassertions(seeItem68).



Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Chapter 69.  Establish a rational error handling policy, and follow it strictly

Tải bản đầy đủ ngay(0 tr)

×