Results 1 to 2 of 2
Thread: e.getMessage()
- 01-28-2010, 06:06 PM #1
Member
- Join Date
- Dec 2009
- Posts
- 76
- Rep Power
- 0
e.getMessage()
HelloJava Code:class TemperatureException extends Exception { } class TooColdException extends TemperatureException { } class TooHotException extends TemperatureException { } [b]class UnusualTasteException extends Exception { UnusualTasteException() { } UnusualTasteException(String msg) { super(msg); } }[/b] class VirtualPerson { public void drinkCoffee(CoffeeCup cup) throws TooColdException, TemperatureException, UnusualTasteException { try { int i = (int) (Math.random() * 4.0); switch (i) { case 0: throw new TooHotException(); case 1: throw new TooColdException(); case 2: throw new UnusualTasteException(); default: throw new TemperatureException(); } } catch (TooHotException e) { System.out.println("This coffee is too hot."); } } //... } class VirtualCafe { public static void serveCustomer(VirtualPerson cust, CoffeeCup cup)throws TemperatureException, UnusualTasteException { try { cust.drinkCoffee(cup); } catch (UnusualTasteException e) { System.out.println( "Customer is complaining of an unusual taste."); [b]String s = e.getMessage(); if (s != null) { System.out.println(s); }[/b] // Deal with an unhappy customer... } } } class Example2 { public static void main(String[] args) throws TemperatureException { // Create a new coffee cup. CoffeeCup cup = new CoffeeCup(); // Create and serve a virtual customer. try { VirtualPerson cust = new VirtualPerson(); VirtualCafe.serveCustomer(cust, cup); } catch (UnusualTasteException e) { System.out.println("This coffee has an unusual taste."); } } }
Question: How can I use from String s = e.getMessage(); in this program?
And how can I place following codes in this program?
Java Code:1. new UnusualTasteException() 2. new UnusualTasteException("This coffee tastes like tea.")
- 01-28-2010, 09:43 PM #2
Member
- Join Date
- Dec 2009
- Posts
- 76
- Rep Power
- 0
Similar Threads
-
What does e.getMessage return?
By new2java2009 in forum New To JavaReplies: 1Last Post: 10-10-2009, 12:01 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks