Results 1 to 8 of 8
Thread: Throwing exceptions
- 07-19-2010, 06:24 PM #1
Member
- Join Date
- Aug 2009
- Posts
- 76
- Rep Power
- 0
Throwing exceptions
I have a quick question about throwing/catching exceptions. The question arrives with the following.
if I have code that does the followingJava Code:public class MyException extends Exception
and other code that catches the exceptionJava Code:public void someMethod() throws Exception{ throw new MyException("Zomg, bad things"); }
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?Java Code:public void doStuffs(){ try{ //dothings } catch(MyException e){ //handle } catch(Exception e){ //handle other things here }
ex.
Java Code:public void someMethod() throws MyException, Exception{ throw new MyException("Zomg, bad things"); }
- 07-19-2010, 06:35 PM #2
What happened when you tried it?
Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
The Ubiquitous Newbie Tips
- 07-19-2010, 08:09 PM #3
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 :(
- 07-19-2010, 08:34 PM #4
@gafa what happens to your code when you compile it and execute it?
- 07-19-2010, 08:42 PM #5
- 07-22-2010, 01:09 AM #6
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 4
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
- 07-22-2010, 01:47 AM #7
@al_Marshy_1981
What happened to the code tags?
- 07-22-2010, 01:57 AM #8
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 4
Similar Threads
-
Exception throwing
By andy16 in forum New To JavaReplies: 9Last Post: 06-07-2010, 05:57 PM -
throwing exceptions in Iterator subclasses
By hirop35 in forum New To JavaReplies: 4Last Post: 10-15-2009, 09:19 PM -
Database Project keeps throwing Exceptions.
By Loop in forum JDBCReplies: 4Last Post: 08-30-2009, 11:22 AM -
throwing bug value
By Dangi in forum New To JavaReplies: 3Last Post: 10-25-2008, 06:14 AM -
throwing Exception
By bugger in forum New To JavaReplies: 3Last Post: 11-09-2007, 09:35 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks