| | |
How can I create a runtime error?
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Jul 2008
Posts: 20
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?
Views: 518 | Replies: 9
| Thread Tools | Search this Thread |
Tag cloud for Java
-xlint android animated api appinventor apple applet application arguments array arrays automation bi binary blackberry bluetooth chat class classes client code compile compiler component database draw eclipse error event exception file fractal freeze functiontesting game gameprogramming givemetehcodez graphics gui helpwithhomework html ide image input integer j2me java javaprojects jetbrains jmf jni jpanel jtable julia learningresources linux list login loop main map method methods mobile myregfun netbeans newbie notdisplaying number object oracle page print problem program programming project qt recursion scanner screen server set size sms socket sort spamblocker sql string swing system test threads time transfer tree variablebinding windows xor







