java loose ends
play

Java Loose Ends 27 December 2019 OSU CSE 1 What Else? A few - PowerPoint PPT Presentation

Java Loose Ends 27 December 2019 OSU CSE 1 What Else? A few Java issues introduced earlier deserve a more in-depth treatment: Try-Catch and Exceptions Members (static vs. instance) Nested interfaces and classes Access


  1. Java Loose Ends 27 December 2019 OSU CSE 1

  2. What Else? • A few Java issues introduced earlier deserve a more in-depth treatment: – Try-Catch and Exceptions – Members (static vs. instance) – Nested interfaces and classes – Access modifiers – “Final” 27 December 2019 OSU CSE 2

  3. Exceptions • An exception indicates a problem with an application that entails (in Java) a dramatic change of control flow • Vocabulary: Exceptions (and Errors) are – “thrown” by a component implementation – “caught” by a client 27 December 2019 OSU CSE 3

  4. Syntax of Try-Catch • In a client, a try-catch statement is used to catch exceptions: try { statements } catch(exceptionType1 identifier1) { handler for type1 } catch(exceptionType2 identifier2) { handler for type2 } . . . 27 December 2019 OSU CSE 4

  5. Execution of Try-Catch • If nothing is thrown during execution of the statements in the try block: – The try block finishes successfully – All catch clauses are ignored (skipped) – Execution continues after the try-catch 27 December 2019 OSU CSE 5

  6. Catching an Exception • If something is thrown during execution of the statements in the try block: 1. The rest of the code in the try block is skipped 2. The catch clauses are examined top to bottom for the first matching catch 3. If an appropriate catch clause is found:  The body of the catch clause is executed  The remaining catch clauses are skipped 4. If no such catch clause is found:  The exception is thrown to the outer block, which is either – A try block (that potentially handles it, in the same manner) – A method body (resulting in it being thrown to its client) 27 December 2019 OSU CSE 6

  7. Use Exceptions in Your Design? • Best practice suggests exceptions should be reserved for unexpected situations: – Problems external to the application – Resource exhaustion – Problems that cannot be handled with checkable preconditions in contracts 27 December 2019 OSU CSE 7

  8. Use Exceptions in Your Design? • Best practice suggests exceptions should be reserved for unexpected situations: – Problems external to the application – Resource exhaustion – Problems that cannot be handled with checkable preconditions in contracts Example: there is a hardware problem with a disk drive. 27 December 2019 OSU CSE 8

  9. Use Exceptions in Your Design? • Best practice suggests exceptions should be reserved for unexpected situations: – Problems external to the application – Resource exhaustion – Problems that cannot be handled with checkable preconditions in contracts Example: the JVM is out of memory for this application. 27 December 2019 OSU CSE 9

  10. Use Exceptions in Your Design? • Best practice suggests exceptions should be reserved for unexpected situations: – Problems external to the application – Resource exhaustion – Problems that cannot be handled with checkable preconditions in contracts Example: a file does not exist—because it has been deleted after its existence has already been “confirmed”. 27 December 2019 OSU CSE 10

  11. Hierarchy of Classes Throwable Error Exception 27 December 2019 OSU CSE 11

  12. Hierarchy of Classes Throwable Error Exception “Unrecoverable”: “Recoverable”: hardware, JVM, or application problem application error (e.g., “file not found”) (e.g., “out of memory”) 27 December 2019 OSU CSE 12

  13. Hierarchy of Error Classes Throwable Error Exception VirtualMachine- AssertionError Error OutOfMemory- Error 27 December 2019 OSU CSE 13

  14. Hierarchy of Error Classes Throwable Error Exception VirtualMachine- AssertionError Error This is thrown by an assert statement when OutOfMemory- the condition is false : Error something is wrong with your program . 27 December 2019 OSU CSE 14

  15. Hierarchy of Error Classes Throwable Error Exception VirtualMachine- AssertionError Error OutOfMemory- There are many more Error subclasses of Error ! 27 December 2019 OSU CSE 15

  16. Hierarchy of Exception Classes Throwable Error Exception VirtualMachine- Runtime- AssertionError IOException Error Exception OutOfMemory- NullPointer- Arithmetic- IndexOutOf- Error Exception BoundsException Exception 27 December 2019 OSU CSE 16

  17. Hierarchy of Exception Classes Throwable Error Exception There are many more subclasses of Exception , RuntimeException , and VirtualMachine- Runtime- AssertionError IOException IOException ! Error Exception OutOfMemory- NullPointer- Arithmetic- IndexOutOf- Error Exception BoundsException Exception 27 December 2019 OSU CSE 17

  18. Hierarchy of Exception Classes Throwable Error Exception With RuntimeException and its subclasses, something is wrong with VirtualMachine- Runtime- AssertionError IOException your program . Error Exception OutOfMemory- NullPointer- Arithmetic- IndexOutOf- Error Exception BoundsException Exception 27 December 2019 OSU CSE 18

  19. Unchecked vs. Checked • Unchecked exceptions are: – Error and its subclasses – RuntimeException and its subclasses • The rest are checked exceptions – “Checked” means that the compiler checks that a method whose body contains a statement that might throw the exception either catches it, or explicitly “propagates it up the call chain” by declaring that it also throws the exception 27 December 2019 OSU CSE 19

  20. Unchecked vs. Checked Throwable Error Exception VirtualMachine- Runtime- AssertionError IOException Error Exception OutOfMemory- NullPointer- Arithmetic- IndexOutOf- Error Exception BoundsException Exception 27 December 2019 OSU CSE 20

  21. Members • A class may have different kinds of members : – Variables/fields/data members – Constructors – Methods – Nested classes • All except constructors may be either static members or instance members 27 December 2019 OSU CSE 21

  22. Members • A class may have different kinds of members : Static methods and – Variables/fields/data members instance methods – Constructors have already been discussed... – Methods – Nested classes • All except constructors may be either static members or instance members 27 December 2019 OSU CSE 22

  23. Members • A class may have different kinds of members : Static members of a – Variables/fields/data members class are also called class members , for – Constructors reasons that will – Methods presently become clear. – Nested classes • All except constructors may be either static members or instance members 27 December 2019 OSU CSE 23

  24. Static vs. Instance Variables • At run-time, a Java program has separate representations for: – All static variables for each class C – All instance variables for each instance of C , i.e., for each object with dynamic type C • Bytecode for C constructors, methods, and nested classes is part of the run-time representation of class C 27 December 2019 OSU CSE 24

  25. Example Class C public class C implements cInterface { private static String sVar; private int iVar; public C(String s, int i) { C.sVar = s; this. iVar = i; } public static void sMethod(...) {...} public void iMethod(...) {...} } ... cInterface x1 = new C("foo", 1); cInterface x2 = new C("bar", 2); 27 December 2019 OSU CSE 25

  26. Example Class C public class C implements cInterface { private static String sVar; The relevant info for private int iVar; upcoming run-time public C(String s, int i) { pictures is the part C.sVar = s; shown in red. this. iVar = i; } public static void sMethod(...) {...} public void iMethod(...) {...} } ... cInterface x1 = new C("foo", 1); cInterface x2 = new C("bar", 2); 27 December 2019 OSU CSE 26

  27. Run-Time Picture "bar" instanceof instanceof 1 2 27 December 2019 OSU CSE 27

  28. Run-Time Picture There is a run-time "bar" representation of each class that holds all its static variables . instanceof instanceof 1 2 27 December 2019 OSU CSE 28

  29. Run-Time Picture Type erasure : the "bar" picture would be the same even if C were generic, say C<T> , and x1 and instanceof x2 had different T . instanceof 1 2 27 December 2019 OSU CSE 29

  30. Run-Time Picture Surprise! This "bar" means that even if C were generic, there would still be only one “copy” of instanceof sVar . instanceof 1 2 27 December 2019 OSU CSE 30

  31. Run-Time Picture There is a run-time "bar" representation of each instance that holds all its instance instanceof variables . instanceof 1 2 27 December 2019 OSU CSE 31

  32. Run-Time Picture Each instance "bar" “knows” its dynamic type at run-time. instanceof instanceof 1 2 27 December 2019 OSU CSE 32

  33. Static Initialization Blocks • To initialize static variables in a class, you may write a static initialization block that looks like this: static { // Code to initialize static variables } • This code is automatically executed when the class is loaded , i.e., once at the very beginning of program execution 27 December 2019 OSU CSE 33

  34. Nested Interfaces • An interface may be nested within another interface – Example (from OSU CSE components) Map.Pair<K,V> – Example (from Java libraries) Map.Entry<K,V> 27 December 2019 OSU CSE 34

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend