Lec09 - Exceptions & Assertions.pdf - Unauthorized...

This preview shows page 1 - 5 out of 32 pages.

Copyright © 2012, Oracle and/or its affiliates. All rights reserved.Exceptions and AssertionsPeter Muturi ([email protected]ฺacฺke) has a non-transferable license touse this Student GuideฺUnauthorized reproduction or distribution prohibitedฺ Copyright© 2013, Oracle and/or its affiliatesฺ
Java SE 7 Programming 9 - 2Copyright © 2012, Oracle and/or its affiliates. All rights reserved.ObjectivesAfter completing this lesson, you should be able to:Define the purpose of Java exceptionsUse the tryand throwstatements Use the catch, multi-catch, and finallyclausesAutoclose resources with a try-with-resources statementRecognize common exception classes and categoriesCreate custom exceptions and auto-closeable resourcesTest invariants by using assertionsPeter Muturi ([email protected]ฺacฺke) has a non-transferable license touse this Student GuideฺUnauthorized reproduction or distribution prohibitedฺ Copyright© 2013, Oracle and/or its affiliatesฺ
Returning a Failure ResultSome programming languages use the return value of a method to indicate whether or not a method completed successfully. For instance, in the C example int x = printf("hi");, a negative value in xwould indicate a failure. Many of C’s standard library functions return a negative value upon failure. The problem is that the previous example could also be written as printf("hi");where the return value is ignored. In Java, you also have the same concern; any return value can be ignored.When a method you are writing in the Java language fails to execute successfully, consider using the exception-generating and handling features available in the language instead of using return values.Java SE 7 Programming 9 - 3Copyright © 2012, Oracle and/or its affiliates. All rights reserved.Error HandlingApplications will encounter errors while executing. Reliable applications should handle errors as gracefully as possible. Errors:Should be the “exception” and not the expected behaviorMust be handled to create reliable applicationsCan occur as the result of application bugsCan occur because of factors beyond the control of the applicationDatabases becoming unreachableHard drives failingPeter Muturi ([email protected]ฺacฺke) has a non-transferable license touse this Student GuideฺUnauthorized reproduction or distribution prohibitedฺ Copyright© 2013, Oracle and/or its affiliatesฺ
The Handle or Declare RuleMany libraries that you use will require knowledge of exception handling. They include:File IO (NIO: java.nio)Database access (JDBC: java.sql)Handling an exception means you use a try-catchstatement to transfer control to an exception-handling block when an exception occurs. Declaring an exception means to add a throws clause to a method declaration, indicating that the method may fail to execute in a specific way. To state it another way, handling means it is your problem to deal with and declaring means that it is someone else’s problem to deal with.Java SE 7 Programming 9 - 4Copyright © 2012, Oracle and/or its affiliates. All rights reserved.

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture