1 / 33

Exceptions

Learn how to create and throw exceptions in Java to control object creation and validation. Understand the differences between checked and unchecked exceptions and how exceptions propagate back to the source.

gstephen
Download Presentation

Exceptions

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 0 Exceptions Session 21

  2. 0 Memory Upload • Creating Exceptions • Using exceptions to control object creation and validation

  3. 0 Exceptions • To create exceptions we need to extend the Exception class • The exception class has one instance variable

  4. 0 Checked Exceptions • A checked exception must: • Be caught • Be listed in the throws clause of the method where it may occur

  5. 0 Unchecked Exceptions • An unchecked are: • Descendants of the RuntimeException

  6. 0 Exception • public class MyException extends Exception{ • private String message; • public MyException( ){ • //empty constructor • } • public void setMessage(String newMsg){ • this.message = newMsg; • } • public String getMessage( ){ • return this.message; • } • } constructor set method get method

  7. 0 The “Other” Class • The class with the main method is called the driver class • We create a class that can throw an exception

  8. 0 The “Other” Class • public class ExcThr{ • public static void main(String [ ] arg) throws Exception{ • MyException me = new MyException( ); • throw me; • } //main method • } //class ends

  9. 0 The “Other” Class • public class ExcThr{ • public static void main(String [ ] arg) throws Exception{ • MyException me = new MyException( ); • me.setMessage(“ICS111 Exception”); • throw me; • } //main method • } //class ends

  10. 0 Exception Propagation • If an exception is not caught and handled where it occurs it will propagate back to the source. • In applications this will be the main method. Exception

  11. 0 Running Several Classes • All classes must be in the same directory • When compiling a class that uses others Java will automatically find the other classes. • This applies to exceptions as well

  12. 0 Time to Try it Out Creating and throwing exceptions. Exception messages

  13. 0 Why do that? User, please do not create invalid objects • You can create exceptions to make your classes independent. • Think: • How can you prevent users from creating invalid objects?

  14. 0 Where is the validation? • We can validate in the driver class. • This makes the Name class dependent in another class • So what can we do? Throw an exception within the Name class!!!!

  15. 0 A simple class • public class Name{ • String name = “”; • public Name(String name){ • this.name = name; • } • public String getName( ){ • return this.name; • } constructor get method

  16. 0 A simple class • public void setName(String newName){ • this.name = name; • } • public String toString( ){ • String s = “Name: ”+ this.name; • return s; • } • } //class name ends set method toString method

  17. 0 Creating a Name Exception • Write down the rules for a valid name. • Name needs to be at least 3 characters long • Name cannot be blank

  18. 0 Creating a Name Exception • So the exception will be thrown if: • Name is composed of only blanks even if it is longer than 2 characters • Name is less than 3 characters long. • Lets translate the rules into Java

  19. 0 NameException • public class NameException extends Exception{ • private String message; • public NameException( ){ • //empty constructor • } • public void setMessage(String newMsg){ • this.message = newMsg; • } • public String getMessage( ){ • return this.message; • } • } constructor set method get method

  20. 0 Name Class Modifications • //the constructor changes • public Name(String name) throws Exception{ • this.name = this.setName(name); • } The strategy? Centralized validation

  21. 0 Name Class Modifications • public void setName(String newName ) throws Exeption{ • int len = 0; • newName = newName.trim( ); • len = newName.length( ); • if(len<3){ • NameException ne = new NameException( ); • ne.setMessage(“Invalid name length”); • throw ne; • } • this.name = newName; • }

  22. 0 Name Class Modifications • public String getName( ){ • return this.name; • } • public String toString( ){ • String s = “Name: ”+ this.name; • return s; • } • } //class name ends Not in these methods

  23. 0 The Driver Class • public class UsingNames{ • public static void main(String [ ] arg) throws Exception{ • Name n1 = new Name(“ABC”); • System.out.println(n1.toString( )); • Name n2 = new Name(“ W ”); • System.out.println(n1.toString( )); • } • } ABC

  24. 0 Name Class Modifications • public void setName(String newName ) throws Exeption{ • int len = 0; • newName = newName.trim( ); • len = newName.length( ); • if(len<3){ • NameException ne = new NameException( ); • ne.setMessage(“Invalid name length”); • throw ne; • } • this.name = newName; • } ABC 3 ABC

  25. 0 The Driver Class • public class UsingNames{ • public static void main(String [ ] arg) throws Exception{ • Name n1 = new Name(“ABC”); • System.out.println(n1.toString( )); • Name n2 = new Name(“ W ”); • System.out.println(n1.toString( )); • } • } W

  26. 0 Name Class Modifications • public void setName(String newName ) throws Exeption{ • int len = 0; • newName = newName.trim( ); • len = newName.length( ); • if(len<3){ • NameException ne = new NameException( ); • ne.setMessage(“Invalid name length”); • throw ne; • } • this.name = newName; • } W W 1 Exception

  27. 0 Exception Propagation UsingNames void main(String arg) Name String name Constructor Name(String name) String getMessage( ) void setMessage(String newMessage) NameException String message Constructor NameException( ) String getMessage( ) void setMessage(String newMessage) Exception Exception Class Created by JAVA

  28. One Exception Several Messages • One Exception can display several messages as the program crashes • The message can be set on each of the different methods that may throw the exception

  29. Validation in set methods • The methods that validate are usually the set methods. • The constructor validates by calling on the set methods • Each set method can add a different exception message in case of error.

  30. Exceptions in Applets

  31. 0 Time to Try it Out Exceptions and Objects

  32. 0 Memory Defragmenter • Creating Exceptions • Setting exception messages • Validating objects using our own exceptions • E-mail any questions to blanca@hawaii.edu

  33. 0 Task Manager • Answer the 5 webct questions • Read your e-mail • Visit WebCT, webct.hawaii.edu

More Related