Results 1 to 3 of 3

Thread: exception

  1. #1
    Oktam is offline Member
    Join Date
    Mar 2008
    Posts
    6
    Rep Power
    0

    Default exception

    I'm new to java and got an exception problem...if an exception occurs in my programme i must print the output "can't execute", but with using a new exception: throw new IllegalArgumentException();
    Java Code:
    try{
    ...
    ..
    }
    catch(NumberFormatException e) {
    	throw new IllegalArgumentException();
    }
    Where and how do i have to define this IllegalArgumentException so that it can print a certain message??

  2. #2
    CaptainMorgan's Avatar
    CaptainMorgan is offline Moderator
    Join Date
    Dec 2007
    Location
    NewEngland, US
    Posts
    835
    Rep Power
    9

    Default

    The Sun tutorials show you one way to perform an output upon a thrown exception. There's also another way(as stated in the API for IllegalArgumentException - yes, exceptions have an API page too :) ) which is not much different from your existing code:
    Java Code:
    ...
       throw new IllegalArgumentException("Exception: cannot execute.");
    ...
    Pretty easy, right? ;)
    Vote for the new slogan to our beloved Java Forums! (closes on September 4, 2008)
    Want to voice your opinion on your IDE/Editor of choice? Vote now!
    Got a little Capt'n in you? (drink responsibly)

  3. #3
    Oktam is offline Member
    Join Date
    Mar 2008
    Posts
    6
    Rep Power
    0

    Default

    Tnx Capt :)...but...it seems i didn't define my problem well enough...here goes.. i'll put a part of my programme here...i made a few classes... one extends the other...

    Java Code:
    public class GeometricFigure{
    
    	private String name;
    		
    	public GeometricFigure(String ime) {
    		this.ime = ime;
    	}
    	
    	
    	public String getName() {
    		return this.name;
    	}
    	
    
    }
    Java Code:
    public class Line extends GeometricFigure {
    	
    	public Line(int x0, int y0, int x1, int y1) {//coordinates
    		super("Line"); 
    		this.x0 = x0;
    		this.y0 = y0;
    		this.x1 = x1;
    		this.y1 = y1;
    	}
    ...
    ..
    ..	
    	[I]public static GeometricFigure fromStringArray(String[] params) throws IllegalArgumentException{
    	try{
    		 int arg_x0 = Integer.parseInt(params[0]);
    		 int arg_y0 = Integer.parseInt(params[1]);
    		 int arg_x1 = Integer.parseInt(params[2]);
    		 int arg_y1 = Integer.parseInt(params[3]);
    		 
    		return new Line(arg_x0, arg_y0, arg_x1, arg_y1);
    	}[B]
    	catch(NumberFormatException e)
    	     {
    	throw new IllegalArgumentException();
    	}[/B]
    	
    	}[/I]
    }
    how can i make the bolded part print "Exception" when i call for example
    GeometricFigure figure = Line.fromStringArray(subarray); from some other function from somewhere (one of the elements of subarray is a string).
    ... how to make it work ... help :confused:

    EDIT: anyone...?
    Last edited by Oktam; 03-25-2008 at 01:23 PM. Reason: deadline

Similar Threads

  1. Need help on Exception
    By Deon in forum New To Java
    Replies: 7
    Last Post: 02-11-2010, 06:46 PM
  2. Where does the exception go?
    By aytidaalkuhs in forum New To Java
    Replies: 3
    Last Post: 04-07-2008, 03:24 PM
  3. Replies: 1
    Last Post: 03-08-2008, 07:41 PM
  4. JSP Exception Hanling
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-24-2007, 10:57 AM
  5. Exception
    By Camden in forum New To Java
    Replies: 2
    Last Post: 11-27-2007, 12:50 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
  •