Results 1 to 9 of 9

Thread: Exceptions

  1. #1
    Nerijus is offline Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    22
    Rep Power
    0

    Smile Exceptions

    Hi, I want to learn more about exceptions. I have an idea to create 2 exception classes:

    Java Code:
    public class MyError extends Exception {
       public MyError(){   }
    }
    
    public class Messages extends MyError {
       public Messages(){   }
    }
    }
    I have other class:
    Java Code:
    public class Bank {
    public int Bank_money;
       public Bank(){   }
    
    public void take_money(int money)throws MyError{
    if (Bank_money < money){
    throws new MyError();
    //And there is error, because this class don't now what  MyError() are
    }
    }
    }
    Maybe someone can give me more information about creation of java exception classes? :)

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

    Default

    When you get errors, please copy and paste the full text here. Often info in the text will help solve the problem.

    Where are the class files located for your new exception classes? Can the compiler find them?

    throws new MyError();
    "throws" is not a command/statement. See throw.

  3. #3
    Nerijus is offline Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    22
    Rep Power
    0

    Default

    All mine code is really complicated. It have to be throw new MyError();
    Thanks, maybe you can give some info about these kind of stuff? :)

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

    Default

    For testing out new ideas like these, it's better to start with small and simple programs to get the techniques working. After they are debugged and working, then move them into the larger code.

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,820
    Rep Power
    19

    Default

    Well, I would say an Exception should be called an Exception.
    Error is an unchecked type of Exception dealing with serious problems in the system, and you will probably never find a need to actually create one of those.

  6. #6
    RamyaSivakanth's Avatar
    RamyaSivakanth is online now Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    806
    Rep Power
    6

    Default

    When you are defining your own exception class

    1.Extend the Exception class.

    2.Override toString method and give some error message as a string and return it like this below.
    public String toString()
    {
    return " u are wrong";
    }

    3.In your class under particular method ,if you want to make of ur exception class then,
    use throw new MyException() and in throws mention that this method is throwing MyException object using "throws MyException".


    4.when u try to call the above method in step 3, automatically ur error message will get displayed.Here toString() will be called on that Object.
    Last edited by RamyaSivakanth; 05-18-2010 at 12:03 PM.
    Ramya:cool:

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,820
    Rep Power
    19

    Default

    toString() has already been overridden by Throwable, such that it prints the exception name and the message. Overriding it again is pointless.

    Add to that that you'd nto generally use toString. Stacktraces are far more useful.

  8. #8
    RamyaSivakanth's Avatar
    RamyaSivakanth is online now Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    806
    Rep Power
    6

    Default

    If we want to display our meaningful messages ,then we should override toString() meth.
    Ramya:cool:

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,820
    Rep Power
    19

    Default

    Nope, just give the exception a meaningful message.
    That's what the message sttribute is for.
    There really is no need at all to override toString().

Similar Threads

  1. Exceptions & More
    By besweeet in forum New To Java
    Replies: 12
    Last Post: 04-29-2010, 09:06 PM
  2. a new way to handle exceptions
    By brodie in forum Web Frameworks
    Replies: 10
    Last Post: 01-20-2010, 04:50 PM
  3. Exceptions
    By hedonist in forum New To Java
    Replies: 10
    Last Post: 09-08-2009, 08:38 AM
  4. Need Help With Exceptions
    By maggie_2 in forum New To Java
    Replies: 5
    Last Post: 12-15-2008, 07:12 PM
  5. Project of Exceptions
    By Albert in forum Advanced Java
    Replies: 1
    Last Post: 07-06-2007, 03:09 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
  •