Results 1 to 8 of 8
  1. #1
    senorbum is offline Member
    Join Date
    Aug 2009
    Posts
    76
    Rep Power
    0

    Default Throwing exceptions

    I have a quick question about throwing/catching exceptions. The question arrives with the following.

    Java Code:
    public class MyException extends Exception
    if I have code that does the following

    Java Code:
    public void someMethod() throws Exception{
    throw new MyException("Zomg, bad things");
    }
    and other code that catches the exception
    Java Code:
    public void doStuffs(){
    try{
    //dothings
    } catch(MyException e){
    //handle
    } catch(Exception e){
    //handle other things here
    }
    Will the exception be caught in the "handle" or the "handle other things"? Or do I have to explicitly throw MyException as part of someMethod?

    ex.

    Java Code:
    public void someMethod() throws MyException, Exception{
    throw new MyException("Zomg, bad things");
    }

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    What happened when you tried it?
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  3. #3
    gafa's Avatar
    gafa is offline Senior Member
    Join Date
    Jun 2010
    Posts
    107
    Rep Power
    0

    Default

    I think the code is not correct because you must use try/catch if it is needed... my IDE shows red underlines if I do so :(

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,327
    Rep Power
    25

    Default

    @gafa what happens to your code when you compile it and execute it?

  5. #5
    gafa's Avatar
    gafa is offline Senior Member
    Join Date
    Jun 2010
    Posts
    107
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    @gafa what happens to your code when you compile it and execute it?
    I used to have try block keep empty and then my IDE doesns't compile it :(
    Maybe a standard console compilation would do any problem but my IDE does...

  6. #6
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    Study this.....

    package org.marshy.monopoly;
    Java Code:
    /**
     * A Class to represent the exceptions that can occur while coding the Monopoly Game
     * 
     * @author alan
     * @version 1.0 2010.6.02
     */
    public class MonopolyException extends Exception{
    	
    	/**
    	 * The exception with a default message: 'An Exception Has Occured!'
    	 */
    	public MonopolyException()
    	{
    		super("An Exception Has Occured!");
    	}
    	
    	/**
    	 * The exception with a message provided by the programmer
    	 * 
    	 * @param msg The message set by the programmer
    	 */
    	public MonopolyException(final String msg)
    	{
    		super(msg);
    	}
    
    }
    
    and quick code of implementing this class
    Code:
    
    public void setRent(int propertyType,int amount) throws MonopolyException
    	{
    
    		switch(propertyType)
    		{
    		case WITHOUT_HOUSE:
    			rentWithoutHouse=amount;
    			break;
    
    		case ONE_HOUSE:
    			rentWithOneHouse=amount;
    			break;
    
    		case TWO_HOUSES:
    			rentWithTwoHouses=amount;
    			break;
    
    		case THREE_HOUSES:
    			rentWithThreeHouses=amount;
    			break;
    
    		case FOUR_HOUSES:
    			rentWithFourHouses=amount;
    			break;
    
    		case HOTEL:
    			rentWithHotel=amount;
    			break;
    
    		default:
    			throw new MonopolyException("Invalid Property Type Entered!!");
    		}
    	}
    Last edited by al_Marshy_1981; 07-22-2010 at 01:57 AM. Reason: Code Tags

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,327
    Rep Power
    25

    Default

    @al_Marshy_1981
    What happened to the code tags?

  8. #8
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

Similar Threads

  1. Exception throwing
    By andy16 in forum New To Java
    Replies: 9
    Last Post: 06-07-2010, 05:57 PM
  2. throwing exceptions in Iterator subclasses
    By hirop35 in forum New To Java
    Replies: 4
    Last Post: 10-15-2009, 09:19 PM
  3. Replies: 4
    Last Post: 08-30-2009, 11:22 AM
  4. throwing bug value
    By Dangi in forum New To Java
    Replies: 3
    Last Post: 10-25-2008, 06:14 AM
  5. throwing Exception
    By bugger in forum New To Java
    Replies: 3
    Last Post: 11-09-2007, 09:35 PM

Posting Permissions

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