Fundamentals of Software Development 1Slide 1 Exceptions What are they?What are they? Why are they...

6
Fundamentals of Softw are Development 1 Slide 1 Exceptions Exceptions What are they? What are they? Why are they useful? Why are they useful? What else can happen What else can happen at runtime…? at runtime…? Wouldn’t it be wonderful? Or would it? From website http://www.dribbleg lass.com/subpages/s trange/exceptions.h tm

Transcript of Fundamentals of Software Development 1Slide 1 Exceptions What are they?What are they? Why are they...

Page 1: Fundamentals of Software Development 1Slide 1 Exceptions What are they?What are they? Why are they useful?Why are they useful? What else can happen at.

Fundamentals of Software Development 1

Slide 1

ExceptionsExceptions• What are they? What are they? • Why are they useful?Why are they useful?• What else can happen What else can happen

at runtime…?at runtime…?

Wouldn’t it be wonderful?Or would it?

From website http://www.dribbleglass.com/subpages/strange/exceptions.htm

Page 2: Fundamentals of Software Development 1Slide 1 Exceptions What are they?What are they? Why are they useful?Why are they useful? What else can happen at.

Fundamentals of Software Development 1

Slide 2

What are they?What are they?• The program seems to be running along just fine, The program seems to be running along just fine,

minding its own business and all, when suddenly…minding its own business and all, when suddenly…Exception in thread "AWT-EventQueue-0" java.lang.ArithmeticException: / by zeroException in thread "AWT-EventQueue-0" java.lang.ArithmeticException: / by zero at EyeBall.look(EyeBall.java:47)at EyeBall.look(EyeBall.java:47) at Eye.look(Eye.java:78)at Eye.look(Eye.java:78) at Eye.mouseMoved(Eye.java:56)at Eye.mouseMoved(Eye.java:56) at java.awt.AWTEventMulticaster.mouseMoved(AWTEventMulticaster.java:271)at java.awt.AWTEventMulticaster.mouseMoved(AWTEventMulticaster.java:271) at java.awt.Component.processMouseMotionEvent(Component.java:5533)at java.awt.Component.processMouseMotionEvent(Component.java:5533) at java.awt.Component.processEvent(Component.java:5257)at java.awt.Component.processEvent(Component.java:5257) at java.awt.Container.processEvent(Container.java:1966)at java.awt.Container.processEvent(Container.java:1966) at java.awt.Window.processEvent(Window.java:1153)at java.awt.Window.processEvent(Window.java:1153) at java.awt.Component.dispatchEventImpl(Component.java:3955)at java.awt.Component.dispatchEventImpl(Component.java:3955) at java.awt.Container.dispatchEventImpl(Container.java:2024)at java.awt.Container.dispatchEventImpl(Container.java:2024) at java.awt.Window.dispatchEventImpl(Window.java:1766)at java.awt.Window.dispatchEventImpl(Window.java:1766) at java.awt.Component.dispatchEvent(Component.java:3803)at java.awt.Component.dispatchEvent(Component.java:3803) at java.awt.EventQueue.dispatchEvent(EventQueue.java:463)at java.awt.EventQueue.dispatchEvent(EventQueue.java:463) at at

java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:23java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:234)4)

at at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:163)java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:163)

at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157)at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149)at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149) at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)

Good grief!What’s all this mean?

Page 3: Fundamentals of Software Development 1Slide 1 Exceptions What are they?What are they? Why are they useful?Why are they useful? What else can happen at.

Fundamentals of Software Development 1

Slide 3

What are they? – Run-What are they? – Run-time errors!time errors!

• Java detects a problem in executing the Java detects a problem in executing the code.code.

• Java “throws an exception.”Java “throws an exception.”• If you don’t “catch” the exception, the If you don’t “catch” the exception, the

program stops and…program stops and…• Displays “where it was” at many levels – Displays “where it was” at many levels –

the “dump” you see like when JavaEyes the “dump” you see like when JavaEyes divided by zero.divided by zero.

Start at the top, looking at the dump’s Start at the top, looking at the dump’s info!info!

Page 4: Fundamentals of Software Development 1Slide 1 Exceptions What are they?What are they? Why are they useful?Why are they useful? What else can happen at.

Fundamentals of Software Development 1

Slide 4

Why are they useful?Why are they useful?

• Exceptions tell us about serious Exceptions tell us about serious problems running the program, problems running the program, which Java can’t deal with.which Java can’t deal with.

• We then have a chance to fix those…We then have a chance to fix those…Would we be better off if

Java hadn’t told us?Generic exception code:

try {

} catch (…)

Page 5: Fundamentals of Software Development 1Slide 1 Exceptions What are they?What are they? Why are they useful?Why are they useful? What else can happen at.

Fundamentals of Software Development 1

Slide 5

What else can happen…?What else can happen…?

• Exceptions are not the Exceptions are not the only run-time error…only run-time error…

• There are plenty of There are plenty of “runtime” errors you “runtime” errors you have to spot yourself.have to spot yourself.– For example…For example…

I wanted three eyeballs, not two!

That button was supposed to say “Exit”,

not “Quit”!

Why isn’t the red square moving?

Page 6: Fundamentals of Software Development 1Slide 1 Exceptions What are they?What are they? Why are they useful?Why are they useful? What else can happen at.

Fundamentals of Software Development 1

Slide 6

And of course…And of course…

• Hey – DoublePedant is producing the Hey – DoublePedant is producing the wrong output!wrong output!

Rule 1 of software development is:Rule 1 of software development is:Everything the Everything the program is supposed program is supposed to do must be to do must be tested or…tested or…It doesn’t work!!!