Top Banner
Exception Handling
33

Exception Handling. Outline What is an Exception How to use exceptions catch ing throw ing Extending the Exception class Declaring using the throws clause.

Dec 30, 2015

Download

Documents

Maria Wilson
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: Exception Handling. Outline What is an Exception How to use exceptions catch ing throw ing Extending the Exception class Declaring using the throws clause.

Exception Handling

Page 2: Exception Handling. Outline What is an Exception How to use exceptions catch ing throw ing Extending the Exception class Declaring using the throws clause.

Outline

• What is an Exception• How to use exceptions• catching• throwing• Extending the Exception class• Declaring using the throws clause• GMS_WS exceptions• GridChem exceptions

Page 3: Exception Handling. Outline What is an Exception How to use exceptions catch ing throw ing Extending the Exception class Declaring using the throws clause.

What is an Exception?

• Exceptions are the customary way in Java to indicate to a calling method that an abnormal condition has occurred.

• In Java, exceptions are objects• Only objects whose classes descend from

Throwable can be thrown.

Page 4: Exception Handling. Outline What is an Exception How to use exceptions catch ing throw ing Extending the Exception class Declaring using the throws clause.

How do we use Exceptions?

• You can throw objects of your own design or members of the java.lang family.

• Depends on situation– If invalid argument passed to a method ->

java.lang.IllegalArgumentException.– If bad password sent, may want to throw custom exception.

• In GridChem, most situations call for custom exception handling.– Custom exceptions descend from java.lang.Exception family

Page 5: Exception Handling. Outline What is an Exception How to use exceptions catch ing throw ing Extending the Exception class Declaring using the throws clause.

Example

• Program simulating someone drinking a cup of coffee

Exception hierarchy for coffee sipping.

Page 6: Exception Handling. Outline What is an Exception How to use exceptions catch ing throw ing Extending the Exception class Declaring using the throws clause.

Example

• If coffee is too cold, throw TooColdException• If coffee is too hot, throw TooHotException• They are exceptions because they are NOT

the normal state of the coffee.

class TemperatureException extends Exception {}class TooColdException extends TemperatureException {}class TooHotException extends TemperatureException {}

Page 7: Exception Handling. Outline What is an Exception How to use exceptions catch ing throw ing Extending the Exception class Declaring using the throws clause.

Throwing Exceptions

• To throw an exception use the throw keyword• Can only throw objects of type Throwable or one

of its subclasses.

Page 8: Exception Handling. Outline What is an Exception How to use exceptions catch ing throw ing Extending the Exception class Declaring using the throws clause.

Example Throwing Exceptions

class VirtualPerson { private static final int tooCold = 65; private static final int tooHot = 85;

public void drinkCoffee(CoffeeCup cup) throws TooColdException, TooHotException {

int temperature = cup.getTemperature(); if (temperature <= tooCold) { throw new TooColdException(); } else if (temperature >= tooHot) { throw new TooHotException(); } //... } //...}

Page 9: Exception Handling. Outline What is an Exception How to use exceptions catch ing throw ing Extending the Exception class Declaring using the throws clause.

Catching Exceptions

• To catch an exception in Java, you write a try block with one or more catch clauses.

• Each catch clause specifies one exception type that it is prepared to handle.

• The try block isolates a section of code and places it under the watch of the assocated catchers.

• If a line of code in the try block throws and exception, the associated catch block will execute.

Page 10: Exception Handling. Outline What is an Exception How to use exceptions catch ing throw ing Extending the Exception class Declaring using the throws clause.

Example Catching an Exception

class Example1 {public static Logger log =

Logger.getLogger(Example1.class.getName());

public static void main(String[] args) {int temperature = 0;// Create a new coffee cup and set the temperature of// its coffee.CoffeeCup cup = new CoffeeCup();cup.setTemperature(temperature);

try { VirtualPerson vp = new VirtualPerson(); vp.drinkCoffee(cup);} catch (TemperatureException e) { log.error(e);}

}}

Page 11: Exception Handling. Outline What is an Exception How to use exceptions catch ing throw ing Extending the Exception class Declaring using the throws clause.

Example Catching an Exception

• can have many catch clauses…try {

VirtualPerson vp = new VirtualPerson();

vp.drinkCoffee(cup);

} catch (TooHotException e) {

log.error(“Coffee is too hot!!”);

} catch (TooHotException e) {

log.error(“Coffee is too cold!!”);

} catch (Exception e) {

log.error(“Don’t know why, coffee is just bad!!”);

}

Page 12: Exception Handling. Outline What is an Exception How to use exceptions catch ing throw ing Extending the Exception class Declaring using the throws clause.

Extending Exception Class

• Providing customized exception classes can – increase the readability of the exception– allow program to take futher corrective action

Page 13: Exception Handling. Outline What is an Exception How to use exceptions catch ing throw ing Extending the Exception class Declaring using the throws clause.

Extending Exception Class

• Overriding existing constructorsclass UnusualTasteException extends Exception { UnusualTasteException() { } UnusualTasteException(String msg) { super(msg); }}

• First constructor the same• Second allows us to customize the error

message.

Page 14: Exception Handling. Outline What is an Exception How to use exceptions catch ing throw ing Extending the Exception class Declaring using the throws clause.

Extending Exception Class

class VirtualCafe { public static void serveCustomer(VirtualPerson cust, CoffeeCup cup)

{try {

cust.drinkCoffee(cup); System.out.println("Coffee tastes just right."); } catch (UnusualTasteException e) { System.out.println( "Customer is complaining of an unusual taste."); String s = e.getMessage(); if (s != null) { System.out.println(s); } // Deal with an unhappy customer... } }}

Page 15: Exception Handling. Outline What is an Exception How to use exceptions catch ing throw ing Extending the Exception class Declaring using the throws clause.

Extending Exception Class

• Sometimes more info may be necessary.• Can inbed other info in exception class and

provide getters and setters.

Page 16: Exception Handling. Outline What is an Exception How to use exceptions catch ing throw ing Extending the Exception class Declaring using the throws clause.

Extending Exception Class

abstract class TemperatureException extends Exception { private int temperature; // in Celsius public TemperatureException(int temperature) { this.temperature = temperature; } public int getTemperature() { return temperature; }}class TooColdException extends TemperatureException { public TooColdException(int temperature) { super(temperature); }}class TooHotException extends TemperatureException { public TooHotException(int temperature) { super(temperature); }}

Page 17: Exception Handling. Outline What is an Exception How to use exceptions catch ing throw ing Extending the Exception class Declaring using the throws clause.

Extending Exception Class

• Can now rewrite VirtualPerson class to leverage the improved exception class.

class VirtualPerson { private static final int tooCold = 65; private static final int tooHot = 85;

public void drinkCoffee(CoffeeCup cup) throws TooColdException, TooHotException {

int temperature = cup.getTemperature(); if (temperature <= tooCold) { throw new TooColdException(temperature); } else if (temperature >= tooHot) { throw new TooHotException(temperature); } }}

Page 18: Exception Handling. Outline What is an Exception How to use exceptions catch ing throw ing Extending the Exception class Declaring using the throws clause.

• Can also use catch clause to take further actions:try { VirtualPerson vp = new VirtualPerson(); vp.drinkCoffee(cup);} catch (TooHotException e) { // wait 30 seconds for coffee to cool while (cup.getTemperature() > CoffeeCup.MAX_TEMP) {

Thread.sleep(30*1000); }

}

Page 19: Exception Handling. Outline What is an Exception How to use exceptions catch ing throw ing Extending the Exception class Declaring using the throws clause.

Extending Exception Class

• Can now rewrite VirtualPerson class to leverage the improved exception class.

class VirtualPerson { private static final int tooCold = 65; private static final int tooHot = 85;

public void drinkCoffee(CoffeeCup cup) throws TooColdException, TooHotException {

int temperature = cup.getTemperature(); if (temperature <= tooCold) { throw new TooColdException(temperature); } else if (temperature >= tooHot) { throw new TooHotException(temperature); } }}

Page 20: Exception Handling. Outline What is an Exception How to use exceptions catch ing throw ing Extending the Exception class Declaring using the throws clause.

Extending Exception Class

class VirtualCafe {public static void serveCustomer(VirtualPerson cust,

CoffeeCup cup) {try {

cust.drinkCoffee(cup); System.out.println("Coffee is just right."); } catch (TooColdException e) { int temperature = e.getTemperature(); System.out.println("Coffee temperature is " + temperature + " degrees Celsius."); if (temperature > 55 && temperature <= 65) { System.out.println("Coffee is cooling off."); // Add more hot coffee... } else if (temperature > 0 && temperature <= 55) { System.out.println("Coffee is too cold."); // Give customer a new cup of coffee with the // proper temperature... } else if (temperature <= 0) { System.out.println("Coffee is frozen."); // Deal with an irate customer... } }

Page 21: Exception Handling. Outline What is an Exception How to use exceptions catch ing throw ing Extending the Exception class Declaring using the throws clause.

Extending Exception Class

catch (TooHotException e) { int temperature = e.getTemperature(); System.out.println("Coffee temperature is " + temperature + " degrees Celsius."); if (temperature >= 85 && temperature < 100) { System.out.println("Coffee is too hot."); // Ask customer to let it cool a few minutes... } else if (temperature >= 100 && temperature < 2000) { System.out.println( "Both coffee and customer are steamed."); // Deal with an irate customer... } else if (temperature >= 2000) { System.out.println( "The coffee is plasma."); // Deal with a very irate customer... } } }}

Page 22: Exception Handling. Outline What is an Exception How to use exceptions catch ing throw ing Extending the Exception class Declaring using the throws clause.

The throws Clause

• There are two kinds of exceptions in Java:– checked– unchecked

• Only checked exceptions need appear in throws clauses.

• Any checked exceptions that may be thrown in a method must either be caught or declared in the method's throws clause.

• Checked exceptions are so called because both the Java compiler and the Java virtual machine check to make sure this rule is obeyed.

Page 23: Exception Handling. Outline What is an Exception How to use exceptions catch ing throw ing Extending the Exception class Declaring using the throws clause.

The throws Clause

• Rules of thumb:– Don’t worry about java.lang.Error – Subclass java.lang.RuntimeException for our custom

exceptions.– If the programmer should handled the exception EVERY

time, then declare the exception in a throws clause.– If the exception signals an improper use of the method, then

it should subclass a RuntimeException which would be unchecked.

– If the exception signals an abnormal situation with which the programmer should deal, then it should be checked.

• We need to implement exception handling, not error handling!!

Page 24: Exception Handling. Outline What is an Exception How to use exceptions catch ing throw ing Extending the Exception class Declaring using the throws clause.

Exceptions in GMS_WS

• Come to the client wrapped as AxisFaults.• Unwrapped in the GMS.java class into an

org.gridchem.client.exception.GMSException.LoginException.javaUserException.javaPermissionException.javaCredentialManagementException.javaProjectException.javaResourceException.javaPreferenceException.javaNotificationException.javaJobSubmissionException.javaJobException.javaInvalidJobRequestException.javaInfrastructureException.javaFileManagementException.javaFileException.java

Login exceptions

VO exceptions

Job Mgmt exceptions

Persistence exceptions

File Mgmt exceptions

Page 25: Exception Handling. Outline What is an Exception How to use exceptions catch ing throw ing Extending the Exception class Declaring using the throws clause.

Exceptions in GridChem

• Found in the org.gridchem.client.exceptions package.

• Not many right now. Need to be expanded.• Will eventually handle all client-side issues.• Since client depends on service so much, the

client exceptions will probably always be far fewer.

Page 26: Exception Handling. Outline What is an Exception How to use exceptions catch ing throw ing Extending the Exception class Declaring using the throws clause.
Page 27: Exception Handling. Outline What is an Exception How to use exceptions catch ing throw ing Extending the Exception class Declaring using the throws clause.
Page 28: Exception Handling. Outline What is an Exception How to use exceptions catch ing throw ing Extending the Exception class Declaring using the throws clause.
Page 29: Exception Handling. Outline What is an Exception How to use exceptions catch ing throw ing Extending the Exception class Declaring using the throws clause.
Page 30: Exception Handling. Outline What is an Exception How to use exceptions catch ing throw ing Extending the Exception class Declaring using the throws clause.
Page 31: Exception Handling. Outline What is an Exception How to use exceptions catch ing throw ing Extending the Exception class Declaring using the throws clause.
Page 32: Exception Handling. Outline What is an Exception How to use exceptions catch ing throw ing Extending the Exception class Declaring using the throws clause.
Page 33: Exception Handling. Outline What is an Exception How to use exceptions catch ing throw ing Extending the Exception class Declaring using the throws clause.