Top Banner
Exceptions Session 21
33

Exceptions Session 21. Memory Upload Creating Exceptions Using exceptions to control object creation and validation.

Dec 14, 2015

Download

Documents

Noelle Knill
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Exceptions Session 21. Memory Upload Creating Exceptions Using exceptions to control object creation and validation.

ExceptionsExceptions

Session 21

Page 2: Exceptions Session 21. Memory Upload Creating Exceptions Using exceptions to control object creation and validation.

Memory UploadMemory Upload

• Creating Exceptions

• Using exceptions to control object creation and validation

Page 3: Exceptions Session 21. Memory Upload Creating Exceptions Using exceptions to control object creation and validation.

ExceptionsExceptions

• To create exceptions we need to extend the Exception class

• The exception class has one instance variable

Page 4: Exceptions Session 21. Memory Upload Creating Exceptions Using exceptions to control object creation and validation.

Checked ExceptionsChecked Exceptions

• A checked exception must: Be caught Be listed in the throws clause of the

method where it may occur

Page 5: Exceptions Session 21. Memory Upload Creating Exceptions Using exceptions to control object creation and validation.

Unchecked ExceptionsUnchecked Exceptions

• An unchecked are: Descendants of the

RuntimeException

Page 6: Exceptions Session 21. Memory Upload Creating Exceptions Using exceptions to control object creation and validation.

ExceptionException

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

Page 7: Exceptions Session 21. Memory Upload Creating Exceptions Using exceptions to control object creation and validation.

The “Other” ClassThe “Other” Class

• The class with the main method is called the driver class

• We create a class that can throw an exception

Page 8: Exceptions Session 21. Memory Upload Creating Exceptions Using exceptions to control object creation and validation.

The “Other” ClassThe “Other” Class

public class ExcThr{ public static void main(String [ ] arg) throws

Exception{

MyException me = new MyException( ); throw me; } //main method} //class ends

Page 9: Exceptions Session 21. Memory Upload Creating Exceptions Using exceptions to control object creation and validation.

The “Other” ClassThe “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

Page 10: Exceptions Session 21. Memory Upload Creating Exceptions Using exceptions to control object creation and validation.

Exception PropagationException 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.

Page 11: Exceptions Session 21. Memory Upload Creating Exceptions Using exceptions to control object creation and validation.

Running Several ClassesRunning 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

Page 12: Exceptions Session 21. Memory Upload Creating Exceptions Using exceptions to control object creation and validation.

Time to Try it OutTime to Try it Out

Creating and throwing

exceptions.Exception messages

Page 13: Exceptions Session 21. Memory Upload Creating Exceptions Using exceptions to control object creation and validation.

Why do that?Why do that?

• You can create exceptions to make your classes independent.

• Think: How can you prevent

users from creating invalid objects?

User, please do not create invalid objects

Page 14: Exceptions Session 21. Memory Upload Creating Exceptions Using exceptions to control object creation and validation.

Where is the validation?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!!!!

Page 15: Exceptions Session 21. Memory Upload Creating Exceptions Using exceptions to control object creation and validation.

A simple classA simple class

public class Name{ String name = “”; public Name(String name){ this.name = name; } public String getName( ){ return this.name; }

constructor

get method

Page 16: Exceptions Session 21. Memory Upload Creating Exceptions Using exceptions to control object creation and validation.

A simple classA 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

Page 17: Exceptions Session 21. Memory Upload Creating Exceptions Using exceptions to control object creation and validation.

Creating a Name ExceptionCreating a Name Exception

• Write down the rules for a valid name. Name needs to be at least 3

characters long Name cannot be blank

Page 18: Exceptions Session 21. Memory Upload Creating Exceptions Using exceptions to control object creation and validation.

Creating a Name ExceptionCreating 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

Page 19: Exceptions Session 21. Memory Upload Creating Exceptions Using exceptions to control object creation and validation.

NameExceptionNameException

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

Page 20: Exceptions Session 21. Memory Upload Creating Exceptions Using exceptions to control object creation and validation.

Name Class ModificationsName Class Modifications

//the constructor changes

public Name(String name) throws Exception{ this.name = this.setName(name);}

The strategy?Centralized validation

Page 21: Exceptions Session 21. Memory Upload Creating Exceptions Using exceptions to control object creation and validation.

Name Class ModificationsName 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;}

Page 22: Exceptions Session 21. Memory Upload Creating Exceptions Using exceptions to control object creation and validation.

Name Class ModificationsName 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

Page 23: Exceptions Session 21. Memory Upload Creating Exceptions Using exceptions to control object creation and validation.

The Driver ClassThe 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

Page 24: Exceptions Session 21. Memory Upload Creating Exceptions Using exceptions to control object creation and validation.

Name Class ModificationsName 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

Page 25: Exceptions Session 21. Memory Upload Creating Exceptions Using exceptions to control object creation and validation.

The Driver ClassThe 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

Page 26: Exceptions Session 21. Memory Upload Creating Exceptions Using exceptions to control object creation and validation.

Name Class ModificationsName 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

Exception

1

Page 27: Exceptions Session 21. Memory Upload Creating Exceptions Using exceptions to control object creation and validation.

Exception PropagationException Propagation

NameExceptionString messageConstructor NameException( )String getMessage( )void setMessage(String newMessage)

NameString nameConstructor Name(String name)String getMessage( )void setMessage(String newMessage)

UsingNamesvoid main(String arg)

Exception ClassCreated by JAVAException

Page 28: Exceptions Session 21. Memory Upload Creating Exceptions Using exceptions to control object creation and validation.

One Exception Several MessagesOne 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

Page 29: Exceptions Session 21. Memory Upload Creating Exceptions Using exceptions to control object creation and validation.

Validation in set methodsValidation 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.

Page 30: Exceptions Session 21. Memory Upload Creating Exceptions Using exceptions to control object creation and validation.

Exceptions in AppletsExceptions in Applets

Page 31: Exceptions Session 21. Memory Upload Creating Exceptions Using exceptions to control object creation and validation.

Time to Try it OutTime to Try it Out

Exceptions

and Objects

Page 32: Exceptions Session 21. Memory Upload Creating Exceptions Using exceptions to control object creation and validation.

• Creating Exceptions

• Setting exception messages

• Validating objects using our own exceptions

• E-mail any questions to [email protected]

Memory DefragmenterMemory Defragmenter

Page 33: Exceptions Session 21. Memory Upload Creating Exceptions Using exceptions to control object creation and validation.

Task ManagerTask Manager

• Answer the 5 webct questions

• Read your e-mail

• Visit WebCT, webct.hawaii.edu