Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Objectives 
• Define exceptions 
• Use try, catch, and finally statements 
• Describe exception categories 
• Identify com...
Exceptions and Assertions 
• Exceptions handle unexpected situations: 
▼ File not found, network failure, illegal argument...
Exceptions 
• Conditions that can readily occur in a correct program 
are checked exceptions 
▼ Represented by the Excepti...
Exception Example 
1 public class HelloWorld { 
2 public static void main (String[] args) { 
3 int i = 0; 
4 
5 String gre...
The tryand catch Statements 
1 try { 
2 // code that might throw a particular exception 
3 } catch (MyExceptionType myExce...
Call Stack Mechanism 
• If an exception is not handled in the current try-catch 
block, it is thrown to the caller of that...
The finally Statement 
1 try { 
2 startFaucet(); 
3 waterLawn(); 
4 } catch (BrokenPipeException e) { 
5 logProblem(e); 
6...
Exception Example Revisited 
1 public class HelloWorld { 
2 public static void main(String[] args) { 
3 int i = 0; 
4 
5 S...
Exception Categories 
Throwable 
Error 
Exception 
RuntimeException 
IOException 
ArithmeticException 
NullPointerExceptio...
Common Exceptions 
• ArithmeticException 
• NullPointerException 
• NegativeArraySizeException 
• ArrayIndexOutOfBoundsExc...
The Handle or Declare Rule 
• Handle the exception by using the try-catch-finally 
block. 
• Declare that the code causes ...
Method Overriding and Exceptions 
The overriding method: 
• Can throw exceptions that are subclasses of the 
exceptions be...
Method Overriding 
Examples 
1 public class TestA { 
2 public void methodA() throws IOException { 
3 // do some number cru...
Creating Your Own Exceptions 
1 public class ServerTimedOutException extends Exception { 
2 private int port; 
3 
4 public...
Handling User-Defined Exceptions 
1 public void connectMe(String serverName) 
2 throws ServerTimedOutException { 
3 int su...
Assertions 
• assert <<boolean_expression> ; 
• assert <<boolean_expression>> : 
<<detail_expression>> ; 
• If<<boolean_ex...
Upcoming SlideShare
Loading in …5
×

Pii tema2

2,000 views

Published on

  • Be the first to comment

  • Be the first to like this

Pii tema2

  1. 1. Objectives • Define exceptions • Use try, catch, and finally statements • Describe exception categories • Identify common exceptions • Develop programs to handle your own exceptions • Use assertions • Distinguish appropriate and inappropriate uses of assertions • Disable assertions at runtime
  2. 2. Exceptions and Assertions • Exceptions handle unexpected situations: ▼ File not found, network failure, illegal argument • Assertions document and test programming assumptions ▼ "This can never be negative here" • Assertion tests can be removed entirely from code at runtime, so the code is not slowed down at all
  3. 3. Exceptions • Conditions that can readily occur in a correct program are checked exceptions ▼ Represented by the Exception class • Severe problems that are normally treated as fatal or situations that probably reflect program bugs are unchecked exceptions ▼ Fatal situations are represented by the Error class ▼ Probable bugs are represented by the RuntimeException class • The API lists checked exceptions from a method
  4. 4. Exception Example 1 public class HelloWorld { 2 public static void main (String[] args) { 3 int i = 0; 4 5 String greetings [] = { 6 "Hello world!", 7 "No, I mean it!", 8 "HELLO WORLD!!" 9 }; 10 11 while (i < 4) { 12 System.out.println (greetings[i]); 13 i++; 14 } 15 } 16 }
  5. 5. The tryand catch Statements 1 try { 2 // code that might throw a particular exception 3 } catch (MyExceptionType myExcept) { 4 // code to execute if a MyExceptionType exception is thrown 5 } catch (Exception otherExcept) { 6 // code to execute if a general Exception exception is thrown 7 }
  6. 6. Call Stack Mechanism • If an exception is not handled in the current try-catch block, it is thrown to the caller of that method. • If the exception gets back to the main method and is not handled there, the program is terminated abnormally.
  7. 7. The finally Statement 1 try { 2 startFaucet(); 3 waterLawn(); 4 } catch (BrokenPipeException e) { 5 logProblem(e); 6 } finally { 7 stopFaucet(); 8 }
  8. 8. Exception Example Revisited 1 public class HelloWorld { 2 public static void main(String[] args) { 3 int i = 0; 4 5 String[] greetings = { 6 "Hello world!", 7 "No, I mean it!", 8 "HELLO WORLD!!" 9 }; 10 11 while (i < 4) { 12 try { 13 System.out.println(greetings[i]); 14 i++; 15 } catch (ArrayIndexOutOfBoundsException e){ 16 System.out.println("Re-setting Index Value"); 17 i = 0; 18 } finally { 19 System.out.println("This is always printed"); 20 } 21 } 22 } 23 }
  9. 9. Exception Categories Throwable Error Exception RuntimeException IOException ArithmeticException NullPointerException IndexOutOfBoundsException FileNotFoundException VirtualMachineError AWTError StackOverflowError OutOfMemoryError EOFException
  10. 10. Common Exceptions • ArithmeticException • NullPointerException • NegativeArraySizeException • ArrayIndexOutOfBoundsException • SecurityException
  11. 11. The Handle or Declare Rule • Handle the exception by using the try-catch-finally block. • Declare that the code causes an exception by using the throws clause. • Declare exception or exceptions a method can throw: void trouble() throws IOException { void trouble() throws IOException, OtherException { • You do not need to handle or declare runtime exceptions or errors.
  12. 12. Method Overriding and Exceptions The overriding method: • Can throw exceptions that are subclasses of the exceptions being thrown by the overridden method
  13. 13. Method Overriding Examples 1 public class TestA { 2 public void methodA() throws IOException { 3 // do some number crunching 4 } 5 } 1 public class TestB1 extends TestA { 2 public void methodA() throws EOFException { 3 // do some number crunching 4 } 5 } 1 public class TestB2 extends TestA { 2 public void methodA() throws Exception { 3 // do some number crunching 4 } 5 }
  14. 14. Creating Your Own Exceptions 1 public class ServerTimedOutException extends Exception { 2 private int port; 3 4 public ServerTimedOutException(String message, int port) { 5 super(message); 6 this.port = port; 7 } 8 9 // Use getMessage method to get the reason the exception was made 10 11 public int getPort() { 12 return port; 13 } 14 }
  15. 15. Handling User-Defined Exceptions 1 public void connectMe(String serverName) 2 throws ServerTimedOutException { 3 int success; 4 int portToConnect = 80; 5 6 success = open(serverName, portToConnect); 7 8 if (success == -1) { 9 throw new ServerTimedOutException("Could not connect", 10 portToConnect); 11 } 12 } 1 public void findServer() { 2 try { 3 connectMe(defaultServer); 4 } catch (ServerTimedOutException e) { 5 System.out.println("Server timed out, trying alternative"); 6 try { 7 connectMe(alternativeServer); 8 } catch (ServerTimedOutException e1) { 9 System.out.println("Error: " + e1.getMessage() + 10 " connecting to port " + e1.getPort()); 11 } 12 } 13 }
  16. 16. Assertions • assert <<boolean_expression> ; • assert <<boolean_expression>> : <<detail_expression>> ; • If<<boolean_expression>> evaluates false, then an AssertionError is thrown. • The second argument is converted to a string and used as descriptive text in the AssertionError message. • Compile using javac -source 1.4 MyClass.java

×