60 likes | 145 Vues
Learn about Java exceptions, runtime errors, and why they are essential in software development. Find out how to handle exceptions and troubleshoot common issues. Discover the importance of testing code thoroughly.
E N D
Exceptions • What are they? • Why are they useful? • What else can happen at runtime…? Wouldn’t it be wonderful? Or would it? From website http://www.dribbleglass.com/subpages/strange/exceptions.htm
What are they? • The program seems to be running along just fine, minding its own business and all, when suddenly… Exception in thread "AWT-EventQueue-0" java.lang.ArithmeticException: / by zero at EyeBall.look(EyeBall.java:47) at Eye.look(Eye.java:78) at Eye.mouseMoved(Eye.java:56) at java.awt.AWTEventMulticaster.mouseMoved(AWTEventMulticaster.java:271) at java.awt.Component.processMouseMotionEvent(Component.java:5533) at java.awt.Component.processEvent(Component.java:5257) at java.awt.Container.processEvent(Container.java:1966) at java.awt.Window.processEvent(Window.java:1153) at java.awt.Component.dispatchEventImpl(Component.java:3955) at java.awt.Container.dispatchEventImpl(Container.java:2024) at java.awt.Window.dispatchEventImpl(Window.java:1766) at java.awt.Component.dispatchEvent(Component.java:3803) at java.awt.EventQueue.dispatchEvent(EventQueue.java:463) at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:234) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:163) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149) at java.awt.EventDispatchThread.run(EventDispatchThread.java:110) Good grief! What’s all this mean?
What are they? – Run-time errors! • Java detects a problem in executing the code. • Java “throws an exception.” • If you don’t “catch” the exception, the program stops and… • Displays “where it was” at many levels – the “dump” you see like when JavaEyes divided by zero. Start at the top, looking at the dump’s info!
Why are they useful? • Exceptions tell us about serious problems running the program, which Java can’t deal with. • We then have a chance to fix those… Would we be better off if Java hadn’t told us? Generic exception code: try { … } catch (…)
What else can happen…? • Exceptions are not the only run-time error… • There are plenty of “runtime” errors you have to spot yourself. • For example… I wanted three eyeballs, not two! That button was supposed to say “Exit”, not “Quit”! Why isn’t the red square moving?
And of course… • Hey – DoublePedant is producing the wrong output! Rule 1 of software development is:Everything the program is supposed to do must be tested or… It doesn’t work!!!