Results 1 to 9 of 9
Thread: Exceptions
- 05-17-2010, 09:04 PM #1
Member
- Join Date
- Mar 2010
- Location
- Lithuania
- Posts
- 22
- Rep Power
- 0
Exceptions
Hi, I want to learn more about exceptions. I have an idea to create 2 exception classes:
I have other class:Java Code:public class MyError extends Exception { public MyError(){ } } public class Messages extends MyError { public Messages(){ } } }
Maybe someone can give me more information about creation of java exception classes? :)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 } } }
- 05-17-2010, 09:10 PM #2
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" is not a command/statement. See throw.throws new MyError();
- 05-17-2010, 09:31 PM #3
Member
- Join Date
- Mar 2010
- Location
- Lithuania
- Posts
- 22
- Rep Power
- 0
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? :)
- 05-17-2010, 10:44 PM #4
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.
- 05-18-2010, 09:35 AM #5
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
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.
- 05-18-2010, 11:58 AM #6
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:
- 05-18-2010, 12:19 PM #7
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
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.
- 05-18-2010, 01:29 PM #8
If we want to display our meaningful messages ,then we should override toString() meth.
Ramya:cool:
- 05-18-2010, 01:44 PM #9
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Similar Threads
-
Exceptions & More
By besweeet in forum New To JavaReplies: 12Last Post: 04-29-2010, 09:06 PM -
a new way to handle exceptions
By brodie in forum Web FrameworksReplies: 10Last Post: 01-20-2010, 04:50 PM -
Exceptions
By hedonist in forum New To JavaReplies: 10Last Post: 09-08-2009, 08:38 AM -
Need Help With Exceptions
By maggie_2 in forum New To JavaReplies: 5Last Post: 12-15-2008, 07:12 PM -
Project of Exceptions
By Albert in forum Advanced JavaReplies: 1Last Post: 07-06-2007, 03:09 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks