Exception Mastery


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: Exception Mastery

  1. #1
    Join Date
    Feb 2004
    Posts
    8

    Exception Mastery

    Hi this exception thingy has always bothered me but i just never really cared until now. Now it's just annoying
    and time consumming. I was wondering if any one knows what's happening

    let 's say i have this method :

    public foo( int whatever ) throws IllegalArgumentExc....
    {
    if (whatever > 5 )
    throw new Ille.... ("Value is Invalid"):

    int x = 5;

    }

    and then in my main () kinda look like this
    try {

    int it = 67;

    foo(it);

    }
    catch (IllegalArgumentException e )
    {
    e.getMessage(); // doesn't not print anything
    System.err.println("") // whatever this is will oviously get printed

    }

    Why isn't the message in the function get printed?, i tried other command from the Exception class
    but i never get anything out. i always have to put a
    System.out in there to see what happen. and even worse if i have more than one functions then i don't even know who threw it.

    The way around this that i use is creating my exception class extending whatever i wanted to catch
    and from there it would always print.
    ex :

    class CatchErrorException () extends Illegal...
    {}

    if i caught this in main, and foo was throwing this instead then it would display the "Value is Invalid " from the food method. is this the only way? i'm getting tired
    of making classes that basically do the same thing just so i can have the Error output and know where it came from. In longer programs it's almost impossible to know where the Exception came from unless your write extension of all the exceptions you want to catch and no way i'm doing that .

    If anyone still reading this far and know why java does this, please let me know, no rush on this.

    thanks,
    Mel


    }
    If A equals success, then the formula is: A = X + Y + Z where X is work; Y is play; Z is keep your mouth shut. Albert Einstein

  2. #2
    Join Date
    Mar 2004
    Posts
    635
    Not sure what you're doing, but this works fine for me.

    Code:
    import java.lang.*;
    
    public class Test
    {
    
        public static void main()
        {
            try
            {
                int it = 67;
                thing(it);
            }
            catch(IllegalArgumentException e)
            {
                System.out.println(e.getMessage());
            }
            
        }
    
    
        public static void thing(int t) throws IllegalArgumentException
        {
            if (t > 5)
                throw new IllegalArgumentException("Invalid data in method 'thing'");
            
            int x = 5;
        }
    
    }

  3. #3
    Join Date
    Feb 2004
    Posts
    8

    Thanks .. a temporay Brain freeze i hope

    Hi,

    "Much to learn i still have" Yoda would say ,
    It work just like you said, i don't know i must have had a logic error or just went brain dead.

    thanks a lot friend.

    Mel
    If A equals success, then the formula is: A = X + Y + Z where X is work; Y is play; Z is keep your mouth shut. Albert Einstein

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center