90 likes | 250 Views
Chapter 11. Risky Behavior . Overview. Handling Risky Code Exceptions Agreeing to Risk try/catch blocks finally block Declaring an Exception Handle or Declare law. Handling Risky Code. Risky code is code that may not work at Run Time
E N D
Chapter 11 Risky Behavior
Overview • Handling Risky Code • Exceptions • Agreeing to Risk • try/catch blocks • finally block • Declaring an Exception • Handle or Declare law
Handling Risky Code • Risky code is code that may not work at Run Time • Lots of risky code is code that relies on outside resources • We deal with them in GUIs, Serial, File IO, and sound.
Exceptions • In java, and exception is an object of type Exception • Since it is an object, we as apply standard Object methods to it as well as Exception Methods • Exceptions need to be 'caught' in Java
Agreeing to Risk • Exceptions are going to occur • By using Java try/catch blocks you are acknowledging that exceptions can happen • This lets you deal with them rather than ignore them
try/catch blocks • To catch and exception, use a try/catch block • Example
finally block • try/block execute in order. The try is run, then as needed the catch is run. • At the end the finally block is run regardless of if an exception was returned or not.
Declaring an Exception • What is you are writing the risky code? • You should warn people using your methods that you have an unhandled exception • This tells someone using your code that they have to deal with it, not you.