| | |
How can I create a runtime error?
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Jul 2008
Posts: 17
Reputation:
Solved Threads: 0
This is my code:
Instead of return false I want to make a runtime error with my massage. Something like "the value must be 4 digits"
How can I do it?
Java Syntax (Toggle Plain Text)
public boolean SetYear (String year) { if (year.matches("[0-9]{4}")) { _year=year; return true; } return false; }
Instead of return false I want to make a runtime error with my massage. Something like "the value must be 4 digits"
How can I do it?
Specifically, you probably want to throw an IllegalArgumentException. You can set whatever message you want in the constructor.
•
•
•
•
Define your own exception handler and use try/catch
Ezzaral's suggestion of throwing an IllegalArgumentException for me hits the nail on the head.
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand."
"How to ask questions the smart way ?"
"How to ask questions the smart way ?"
<Ignore>
Last edited by stephen84s; Mar 27th, 2009 at 6:05 am.
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand."
"How to ask questions the smart way ?"
"How to ask questions the smart way ?"
You can set the "message" for most (if not all) inbuilt exceptions including the IllegalArgumentException (for whom this constructor can be used which was also mentioned by Ezzaral in his second post).
Last edited by stephen84s; Mar 27th, 2009 at 6:18 am.
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand."
"How to ask questions the smart way ?"
"How to ask questions the smart way ?"
![]() |
Similar Threads
- runtime error 91 object variable or With block variable not set (Visual Basic 4 / 5 / 6)
- Runtime Error (ASP.NET)
- Create a Runtime Error List (Visual Basic 4 / 5 / 6)
- Runtime Error help needed (Java)
- runtime error#58 (Visual Basic 4 / 5 / 6)
- "error has occured in script on this page" (Windows NT / 2000 / XP)
- Runtime Error 424 object required (was: Please Help!) (Visual Basic 4 / 5 / 6)
Other Threads in the Java Forum
- Previous Thread: I need help with Program
- Next Thread: Char "[-.!? ,\n]" after space detect as uppercase?
| Thread Tools | Search this Thread |
2dgraphics account android api apple applet application array arrays automation banking binary binarytree bluetooth chat chatprogramusingobjects class classes client code component data database derby design draw eclipse encryption error event exception fractal game givemetehcodez graphics gui html ide if_statement image inheritance input integer interface j2me java javadesktopapplications javaprojects jlabel jni jpanel jtextfield julia linux list loop map method methods midlethttpconnection mobile monitoring netbeans newbie nullpointerexception open-source oracle print printing problem program programming project property recursion reference ria scanner screen search server set size sms sort sourcelabs splash sql static stop string swing testautomation threads time tree ui unicode validation windows







