Results 1 to 2 of 2

Thread: e.getMessage()

  1. #1
    arefeh is offline Member
    Join Date
    Dec 2009
    Posts
    76
    Rep Power
    0

    Default e.getMessage()

    Java 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.");
              }
            }
    }
    Hello
    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.")

  2. #2
    arefeh is offline Member
    Join Date
    Dec 2009
    Posts
    76
    Rep Power
    0

    Default

    I found the answer of my question:
    I should wrote:
    case 2:
    throw new UnusualTasteException("This coffee tastes like tea.");

    in this program.

Similar Threads

  1. What does e.getMessage return?
    By new2java2009 in forum New To Java
    Replies: 1
    Last Post: 10-10-2009, 12:01 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •