114
Riktig feilhåndtering ...og ja, checked exceptions er skadelig Trond Arve Wasskog – JavaZone 2008

Riktig feilhåndtering (og ja, checked exceptions er skadelige)

  • Upload
    ilmyggo

  • View
    242

  • Download
    4

Embed Size (px)

Citation preview

Page 1: Riktig feilhåndtering (og ja, checked exceptions er skadelige)

Riktig feilhåndtering

...og ja, checked exceptions er skadelig

Trond Arve Wasskog – JavaZone 2008

Page 2: Riktig feilhåndtering (og ja, checked exceptions er skadelige)

0Feilhåndtering

Page 3: Riktig feilhåndtering (og ja, checked exceptions er skadelige)

� Exception handling done right (and yes, checked exceptions are not necessary!)

- Trond Arve - BEKK - Confluence

� [JavaSpecialists 162] - Exceptions in Java

� Unchecked Exceptions — The Controversy (The Java™ Tutorials > Essential

Classes > Exceptions)

� java.net: Exception-Handling Antipatterns

� java.net: Three Rules for Effective Exception Handling

� Java Exceptions Handling

� The Art and Craft of Great Software Architecture and Development: Java

Exception Handling Anti-Patterns

� Neal Gafter's blog: Closures for Java

� Neal Gafter's blog: Removing Language Features?

Page 4: Riktig feilhåndtering (og ja, checked exceptions er skadelige)

� Exception handling problems in Java, Part I

� Exception handling problems in Java, Part II

� Bruce Eckel's MindView, Inc: Does Java need Checked Exceptions?

� Exceptions in Java: Nothing exceptional about them - Java World

� News & Ideas Forum (Closed for new topic posts) - Failure and Exceptions

� The Trouble with Checked Exceptions

� BEKK forum :: View topic - Håndtering av exceptions - erfaringer/diskusjoner

� Thinking Inside a Bigger Box » A Hopeful Idea: The End of Checked

Exceptions?

� Should checked exceptions be removed from Java?

� The RedirectException - The Daily WTF

Page 5: Riktig feilhåndtering (og ja, checked exceptions er skadelige)

� Dont Use Exceptions For Flow Control

� Exception Patterns

� Exception Tunneling

� Checked Exceptions Are Of Dubious Value

� Octopull/Java: More Exceptional Java

� Java theory and practice: The exceptions debate

� James Gosling Chimes In on Checked Exceptions

� Exception handling - Wikipedia, the free encyclopedia

� cat /dev/random » Remove checked exceptions?

� Java's checked exceptions were a mistake (and here's what I would like to do

about it)

Page 6: Riktig feilhåndtering (og ja, checked exceptions er skadelige)

� DOTNET Archives -- November 2000, week 1 (#349)

� Java Practices -> Checked versus unchecked exceptions

� Java Exception Handling - BEKK - Confluence

� Java Exception Handling - BEKK - Confluence

� Feilhåndtering - Prosjektkvalitet - Confluence

� XO.NET-metoden - Unntakshåndtering - BEKK - Confluence

� The Coad Letter: Modeling and Design Edition, Issue 90, Exceptional Strategies

� Effective Java Exceptions

� cache:DxQf_u_Vue8J:dev2dev.bea.com/lpt/a/541 java exception design

strategies - Google-søk

� David Walend's Blog: Design For Exceptions

Page 7: Riktig feilhåndtering (og ja, checked exceptions er skadelige)

� SwTech.com - Java Design Patterns

� Single Sign On - Login

� Exception Patterns

� Page 2 - Framework Patterns: Exception Handling, Logging, and Tracing

� Alexander Klimetschek's Blog : Exception Handling Best Practices Part 1

� towards_xcptn_hndling.pdf (application/pdf Object)

� Andre's Java Links

� JForum - Få slutt på galskapen: Stem "ja" til å fjerne checked exceptions fra

Java

� Beware the dangers of generic Exceptions - Java World

� Designing with exceptions - Java World

Page 8: Riktig feilhåndtering (og ja, checked exceptions er skadelige)

� Forward - Tips and Guidelines for Programming Java GUI Applications

� warning unchecked exception - Google-søk

� Exception (Java 2 Platform SE v1.4.2)

� LISTSERV 15.0 - RMI-USERS Archives

� Google Answers: The origin of checked exceptions

� Java (programming language) - Wikipedia, the free encyclopedia

� Hacking Architect : Weblog

� Old News: Java Checked vs. Unchecked Exceptions « Bees Are Social By Nature

� Opinion: Exception Handling Debate is Back

� Manageability - The Exception Debate: Why Gosling and Hejlsberg are Both In

Error

Page 9: Riktig feilhåndtering (og ja, checked exceptions er skadelige)

� Mistaeks I Hav Made: Generic Throws: Another Little Java Idiom

� DataAccessException.gif (GIF Image, 916x397 pixels)

� Antonio Goncalves' Weblog

� A Test-Driven Exploration of the Advanced Features of EJB 3.0

Page 10: Riktig feilhåndtering (og ja, checked exceptions er skadelige)

Dårlig feilhåndtering er vanlig

Page 11: Riktig feilhåndtering (og ja, checked exceptions er skadelige)

1972

ReturnCodes

Page 12: Riktig feilhåndtering (og ja, checked exceptions er skadelige)

1964

On ErrorExceptions

Page 13: Riktig feilhåndtering (og ja, checked exceptions er skadelige)

1964 1972 1983 1992 1995 2001

ReturnCodes

On ErrorExceptions

Page 14: Riktig feilhåndtering (og ja, checked exceptions er skadelige)

1964 1972 1983 1992 1995 2001

ReturnCodes

On ErrorExceptions

Exceptions

Page 15: Riktig feilhåndtering (og ja, checked exceptions er skadelige)

1964 1972 1983 1992 1995 2001

ReturnCodes

On ErrorExceptions

Exceptions

CheckedExceptions

Page 16: Riktig feilhåndtering (og ja, checked exceptions er skadelige)

1964 1972 1983 1992 1995 2001

ReturnCodes

On ErrorExceptions

Exceptions

CheckedExceptions

Exceptions

Page 17: Riktig feilhåndtering (og ja, checked exceptions er skadelige)

Java Exceptions

Throwable

Page 18: Riktig feilhåndtering (og ja, checked exceptions er skadelige)

Java Exceptions

Throwable

Exception

Checked

Exceptions

Page 19: Riktig feilhåndtering (og ja, checked exceptions er skadelige)

Java Exceptions

Throwable

Exception

Runtime

ExceptionChecked

Exceptions

Unchecked

Exceptions

Page 20: Riktig feilhåndtering (og ja, checked exceptions er skadelige)

Java Exceptions

Throwable

Error Exception

Runtime

ExceptionVM Errors

(Unchecked)Checked

Exceptions

Unchecked

Exceptions

Page 21: Riktig feilhåndtering (og ja, checked exceptions er skadelige)

Et alternativt design

Throwable

Error Exception

VM Errors

<interface>

Unchecked

Exception

Unchecked

Exceptions

Checked

Exceptions

Page 22: Riktig feilhåndtering (og ja, checked exceptions er skadelige)

1Hvem bryr seg om feilhåndtering?

Page 23: Riktig feilhåndtering (og ja, checked exceptions er skadelige)

Brukeren er opptatt av at en feil har oppstått og at situasjonen er håndtert,

ikke hva som har skjedd

Page 24: Riktig feilhåndtering (og ja, checked exceptions er skadelige)

Forretningen forstår funksjonelle feil og vil unngå kritiske feil

Page 25: Riktig feilhåndtering (og ja, checked exceptions er skadelige)

Drift vil vite hva de skal gjøre med feilen

Page 26: Riktig feilhåndtering (og ja, checked exceptions er skadelige)

Utviklere vil vite hva som har skjedd, hvor, når, hvorfor, hvem, tilstand og kontekst

Page 27: Riktig feilhåndtering (og ja, checked exceptions er skadelige)

Feilhåndtering har ulike interessenter med sine spesifikke behov

Typisk er få representert som kravstillere

Page 28: Riktig feilhåndtering (og ja, checked exceptions er skadelige)

2Feilhåndtering

Page 29: Riktig feilhåndtering (og ja, checked exceptions er skadelige)

Hva kan man gjøre når en feil oppstår?

Page 30: Riktig feilhåndtering (og ja, checked exceptions er skadelige)

Ignorere?

Page 31: Riktig feilhåndtering (og ja, checked exceptions er skadelige)

Håndtere?

Page 32: Riktig feilhåndtering (og ja, checked exceptions er skadelige)

Prøv igjen?

Page 33: Riktig feilhåndtering (og ja, checked exceptions er skadelige)

Prøv et alternativ?

Page 34: Riktig feilhåndtering (og ja, checked exceptions er skadelige)

Avslutte forespørselen

Page 35: Riktig feilhåndtering (og ja, checked exceptions er skadelige)

Hvor skal feil håndteres?

Page 36: Riktig feilhåndtering (og ja, checked exceptions er skadelige)

Lokal feilhåndtering

Page 37: Riktig feilhåndtering (og ja, checked exceptions er skadelige)

Sentral feilhåndtering

Page 38: Riktig feilhåndtering (og ja, checked exceptions er skadelige)

De aller fleste applikasjoner er tjent med sentral feilhåndtering

Page 39: Riktig feilhåndtering (og ja, checked exceptions er skadelige)

Feilhåndtering vs feilretting

Page 40: Riktig feilhåndtering (og ja, checked exceptions er skadelige)
Page 41: Riktig feilhåndtering (og ja, checked exceptions er skadelige)

Hva skjer om en annen Exception

eller Error kastes?

Page 42: Riktig feilhåndtering (og ja, checked exceptions er skadelige)
Page 43: Riktig feilhåndtering (og ja, checked exceptions er skadelige)

Er det viktig at operasjonen eller

applikasjonen fungerer?

Page 44: Riktig feilhåndtering (og ja, checked exceptions er skadelige)

Feilhåndtering sørger for konsistens

Page 45: Riktig feilhåndtering (og ja, checked exceptions er skadelige)

Feilhåndtering er kritisk for applikasjonen

Page 46: Riktig feilhåndtering (og ja, checked exceptions er skadelige)

Automatisert feilretting er ofte komplekst, kostbart og risikabelt

Page 47: Riktig feilhåndtering (og ja, checked exceptions er skadelige)

Feilhåndtering er viktigere enn feilretting

Page 48: Riktig feilhåndtering (og ja, checked exceptions er skadelige)

3Tre feiltyper

Page 49: Riktig feilhåndtering (og ja, checked exceptions er skadelige)

Application Exception

Page 50: Riktig feilhåndtering (og ja, checked exceptions er skadelige)

System Exception

Page 51: Riktig feilhåndtering (og ja, checked exceptions er skadelige)

Programming Exception

a.k.aRuntimeException

Page 52: Riktig feilhåndtering (og ja, checked exceptions er skadelige)

Type Håndter Feilfiks og

utrulling

Prøv igjen?

Application

Exception

Mulig Nei Nei

System

Exception

Nei Nei Mulig

Programming

Exception

Nei Ja Nei

Page 53: Riktig feilhåndtering (og ja, checked exceptions er skadelige)

4Overordnet Design

Page 54: Riktig feilhåndtering (og ja, checked exceptions er skadelige)

Lettvekts exception-hierarki - Distribuert

Page 55: Riktig feilhåndtering (og ja, checked exceptions er skadelige)

Felles exception-klasser med feilkode - Sentralisert

AbstractException- Long id

ApplicationException SystemException

java.lang

RuntimeException

Page 56: Riktig feilhåndtering (og ja, checked exceptions er skadelige)

Hybrid – Sentrale exception-klasser, distribuert hierarki

AbstractException- Long id

ApplicationException SystemException

java.lang

RuntimeException

dd<System Exceptions>

dd<Application Exceptions>

Page 57: Riktig feilhåndtering (og ja, checked exceptions er skadelige)

AbstractException-Long id

- Map context

-----------------------------

addContext(key, value)

Page 58: Riktig feilhåndtering (og ja, checked exceptions er skadelige)

Rammeverk = Distribuert

Applikasjon = Sentralisert

Page 59: Riktig feilhåndtering (og ja, checked exceptions er skadelige)

5Checked Exceptions

Page 60: Riktig feilhåndtering (og ja, checked exceptions er skadelige)
Page 61: Riktig feilhåndtering (og ja, checked exceptions er skadelige)

Ondskapens akse

JimWaldo

JamesGosling

Ann Wollrath

There was a time when Oak and the earliest version of Java did not have checked exceptions. Exception handling was advisory, and it was an unsafe world out there. It was our group (Jim Waldo and I in particular :-) that recommended that there be exceptions checked by the compiler. Jim was quite persuasive in his arguments, telling of a world where robust code would reign. After some consideration, Java was retooled to have checked exceptions. Only those exceptions for which there was norecovery or reflect application errors would be unchecked (e.g.,OutOfMemoryError, NullPointerException respectively). And the world was safe again

-- Ann Wollrathhttp://archives.java.sun.com/cgi-bin/wa?A2=ind9901&L=RMI-USERS&P=R25037

Page 62: Riktig feilhåndtering (og ja, checked exceptions er skadelige)

Ringbærerne

Item 58: Use chekced exceptions for recoverable conditions

and runtime exceptions for programming errors

Item 59: Avoid unnecessary use of checked exceptions

-- Joshua Bloch

I believe the language could be simplified by treating all exception types as unchecked without breaking existing programs. This could also result in a simplification of future

language extensions and APIs. But would the language and

platform be better off without checked exceptions?

-- Neil Gafter

The overhead of checked exceptions was having the opposite effect of what was intended, something that can happen when

you experiment (and I now believe that checked exceptions were

an experiment based on what someone thought was a good idea,

and which I believed was a good idea until recently).

-- Bruce Eckel

Page 63: Riktig feilhåndtering (og ja, checked exceptions er skadelige)

Source: http://java.net/pub/pq/163

Page 64: Riktig feilhåndtering (og ja, checked exceptions er skadelige)

“Checked exceptions are gold. Developers that don't like them are those that are too lazy to actually

think about error cases.”

http://www.javaworld.com/javaforums/showflat.php?Cat=0&Number=35228&Main=2629

Page 65: Riktig feilhåndtering (og ja, checked exceptions er skadelige)

“Were all exceptions unchecked, no exceptions would be caught until the corresponding bugs

began to present themselves.”

http://www.javaworld.com/javaforums/showflat.php?Cat=0&Number=35228&Main=2629

Page 66: Riktig feilhåndtering (og ja, checked exceptions er skadelige)

“Checked exceptions seem like a really good idea at first…

However, the kind of code you must write around these things and

the common phenomenon of "swallowed" exceptions begins to

suggest there's a problem. ”

http://www.mindview.net/Etc/Discussions/CheckedExceptions

Page 67: Riktig feilhåndtering (og ja, checked exceptions er skadelige)

“I am so tired of all the comments singing the praise of checked

exceptions on a theoretical basis. I don’t care about what’s good in

theory!”

http://www.javaworld.com/community/?q=comment/reply/1128

Page 68: Riktig feilhåndtering (og ja, checked exceptions er skadelige)

“The HibernateException, which wraps most of the errors that can occur in a Hibernate

persistence layer, is an unchecked exception (it wasn't in older versions of Hibernate).

In our opinion, we shouldn't force the application developer to catch an

unrecoverable exception at a low layer. In most systems, unchecked and fatal exceptions

are handled in one of the first frames of the method call stack”

http://www.hibernate.org/hib_docs/reference/en/html/transactions.html

Page 69: Riktig feilhåndtering (og ja, checked exceptions er skadelige)

Checked exceptions forurenser APIet

Page 70: Riktig feilhåndtering (og ja, checked exceptions er skadelige)

Checked exceptions fører til unnamanøvre som forpester

koden

Page 71: Riktig feilhåndtering (og ja, checked exceptions er skadelige)

Checked exceptions påtvinger lokal ”feilhåndtering”

Page 72: Riktig feilhåndtering (og ja, checked exceptions er skadelige)

java.text.ParseException

java.net.MalformedURLException

java.lang.CloneNotSupportedException

Page 73: Riktig feilhåndtering (og ja, checked exceptions er skadelige)

Checked exceptions kan brukes når klienten alltid både kan og må

håndtere feilen.

Page 74: Riktig feilhåndtering (og ja, checked exceptions er skadelige)

God feilhåndtering er viktig

Exceptions er bra

Dokumenter feilsituasjoner

Page 75: Riktig feilhåndtering (og ja, checked exceptions er skadelige)

Skal kompilatoren påtvinge feilhåndtering?

Page 76: Riktig feilhåndtering (og ja, checked exceptions er skadelige)
Page 77: Riktig feilhåndtering (og ja, checked exceptions er skadelige)
Page 78: Riktig feilhåndtering (og ja, checked exceptions er skadelige)

Checked exceptions påfører applikasjoner stor skade

Checked exceptions bør fjernes

Page 79: Riktig feilhåndtering (og ja, checked exceptions er skadelige)

6Slik, ikke slik!

Page 80: Riktig feilhåndtering (og ja, checked exceptions er skadelige)

Ikke bruk exceptions for normal forretningslogikk

Page 81: Riktig feilhåndtering (og ja, checked exceptions er skadelige)
Page 82: Riktig feilhåndtering (og ja, checked exceptions er skadelige)

Bruk eksisterende exceptions

Page 83: Riktig feilhåndtering (og ja, checked exceptions er skadelige)

IllegalStateExceptionUnsupportedOperationException

IllegalArgumentExceptionNoSuchElementException

NullPointerException

Page 84: Riktig feilhåndtering (og ja, checked exceptions er skadelige)

Ikke sluk exceptions

Page 85: Riktig feilhåndtering (og ja, checked exceptions er skadelige)
Page 86: Riktig feilhåndtering (og ja, checked exceptions er skadelige)
Page 87: Riktig feilhåndtering (og ja, checked exceptions er skadelige)
Page 88: Riktig feilhåndtering (og ja, checked exceptions er skadelige)
Page 89: Riktig feilhåndtering (og ja, checked exceptions er skadelige)

Fallgruver med try-catch-finally

Page 90: Riktig feilhåndtering (og ja, checked exceptions er skadelige)
Page 91: Riktig feilhåndtering (og ja, checked exceptions er skadelige)
Page 92: Riktig feilhåndtering (og ja, checked exceptions er skadelige)
Page 93: Riktig feilhåndtering (og ja, checked exceptions er skadelige)
Page 94: Riktig feilhåndtering (og ja, checked exceptions er skadelige)

Dokumenter exceptions

Page 95: Riktig feilhåndtering (og ja, checked exceptions er skadelige)
Page 96: Riktig feilhåndtering (og ja, checked exceptions er skadelige)
Page 97: Riktig feilhåndtering (og ja, checked exceptions er skadelige)

Russisk kone

Page 98: Riktig feilhåndtering (og ja, checked exceptions er skadelige)
Page 99: Riktig feilhåndtering (og ja, checked exceptions er skadelige)

ERROR [btpool0-3] JDBCExceptionReporter.logExceptions(78) | Violation

of unique constraint $$: duplicate value(s) for column(s) $$: SYS_CT_88 in statement [insert into

app_user (id, account_expired, account_locked, address, city, country, postal_code, province,

credentials_expired, email, account_enabled, first_name, last_name, password, password_hint, phone_number,

username, version, website) values (null, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?

org.springframework.dao.DataIntegrityViolationException: could not insert: [edu.ksu.model.User]; nested

exception is org.hibernate.exception.ConstraintViolationException: could not insert: [edu.ksu.model.User]

at

org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:

624)

at

org.springframework.orm.hibernate3.HibernateAccessor.convertHibernateAccessException(HibernateAccessor.java:412)

at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:424)

at

org.springframework.orm.hibernate3.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:374)

at org.springframework.orm.hibernate3.HibernateTemplate.saveOrUpdate(HibernateTemplate.java:744)

<snip/>

Caused by: org.hibernate.exception.ConstraintViolationException: could not insert: [edu.ksu.model.User]

at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:71)

at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)

<snip/>

Caused by: java.sql.SQLException: Violation of unique constraint $$:

duplicate value(s) for column(s) $$: SYS_CT_88 in statement [insert into app_user (id, account_expired,

account_locked, address, city, country, postal_code, province, credentials_expired, email, account_enabled,

first_name, last_name, password, password_hint, phone_number, username, version, website) values (null, ?, ?, ?,

?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)]

at org.hsqldb.jdbc.Util.throwError(Unknown Source)

at org.hsqldb.jdbc.jdbcPreparedStatement.executeUpdate(Unknown Source)

at

org.apache.commons.dbcp.DelegatingPreparedStatement.executeUpdate(DelegatingPreparedStatement.java:101)

at

org.apache.commons.dbcp.DelegatingPreparedStatement.executeUpdate(DelegatingPreparedStatement.java:101)

at org.hibernate.id.insert.AbstractSelectingDelegate.performInsert(AbstractSelectingDelegate.java:33)

... 143 more

Page 100: Riktig feilhåndtering (og ja, checked exceptions er skadelige)

Unngå både logging og kasting

Page 101: Riktig feilhåndtering (og ja, checked exceptions er skadelige)
Page 102: Riktig feilhåndtering (og ja, checked exceptions er skadelige)

Drift trenger unik feilkode for åidentifisere feilen

Page 103: Riktig feilhåndtering (og ja, checked exceptions er skadelige)
Page 104: Riktig feilhåndtering (og ja, checked exceptions er skadelige)

Utviklere trenger unik instans-ID for å identifisere feilen

Page 105: Riktig feilhåndtering (og ja, checked exceptions er skadelige)
Page 106: Riktig feilhåndtering (og ja, checked exceptions er skadelige)

Exceptions skal også testes

Page 107: Riktig feilhåndtering (og ja, checked exceptions er skadelige)
Page 108: Riktig feilhåndtering (og ja, checked exceptions er skadelige)
Page 109: Riktig feilhåndtering (og ja, checked exceptions er skadelige)

Bruk stubber for testing av eksterne systemer

Page 110: Riktig feilhåndtering (og ja, checked exceptions er skadelige)

<Stub>

Fagsystem A

<interface>

Fagsystem A

Fagsystem A

<interface>

Fagsystem A

Page 111: Riktig feilhåndtering (og ja, checked exceptions er skadelige)

Gjør logging enkelt

Page 112: Riktig feilhåndtering (og ja, checked exceptions er skadelige)

Gjør logging enkelt

Page 113: Riktig feilhåndtering (og ja, checked exceptions er skadelige)

Legg til kontekst der feilen oppstår

Page 114: Riktig feilhåndtering (og ja, checked exceptions er skadelige)

BEKK CONSULTING ASSKUR 39, VIPPETANGEN. P.O. BOX 134 SENTRUM, 0102 OSLO, NORWAY. WWW.BEKK.NO

Takk for oppmerksomheten!

trond.arve.wasskog[at]bekk.no