| | |
How to handle exceptions in constructor?
![]() |
•
•
Join Date: Nov 2004
Posts: 189
Reputation:
Solved Threads: 0
Hello everyone,
When exceptions occur in constructor so that the instance of the object can not be created, should we throw the exception to the invoker of the constructor or simply catch the exception (and not throw it again)?
I think we should not catch the exception (and not throw it again), since in this approach, the invoker can not see whether the instance of the object has been created successfully.
I think we should throw the exception to let the invoker know that the instance of the object has not been created successfully, so that the invoker can peform appropriate actions. But I am not quite sure whether I am correct since I have not seen any constructors which throw exceptions before.
Could anyone help?
Thanks in advance,
George
When exceptions occur in constructor so that the instance of the object can not be created, should we throw the exception to the invoker of the constructor or simply catch the exception (and not throw it again)?
I think we should not catch the exception (and not throw it again), since in this approach, the invoker can not see whether the instance of the object has been created successfully.
I think we should throw the exception to let the invoker know that the instance of the object has not been created successfully, so that the invoker can peform appropriate actions. But I am not quite sure whether I am correct since I have not seen any constructors which throw exceptions before.
Could anyone help?
Thanks in advance,
George
•
•
Join Date: Jun 2004
Posts: 2,108
Reputation:
Solved Threads: 18
You have to be careful when throwing exceptions in a constructor, that is if you want clean running code. Constructors should also return valid objects but sometimes can't due to errors. I guess the best way to deal with something of this senario is present clear error messages that let the user know what they failed to give or present, and second is to use the finally clause. The finally clause will give about the only way to "clean up" your code after failure, although you must be careful here also. Using the finally clause means that it will execute every time the code is run(you know this already), which means some kind of flag must be created in order for proper cleanup.
Hope that makes a little sense and helps out some. I'm sure you know all of that already and are seeking more advanced techniques, but that's about the only path you can go down on.
Hope that makes a little sense and helps out some. I'm sure you know all of that already and are seeking more advanced techniques, but that's about the only path you can go down on.
•
•
Join Date: Nov 2004
Posts: 189
Reputation:
Solved Threads: 0
Thanks server_crash,
Your reply is very helpful! You are always very warm-heated.
regards,
George
•
•
•
•
Originally Posted by server_crash
You have to be careful when throwing exceptions in a constructor, that is if you want clean running code. Constructors should also return valid objects but sometimes can't due to errors. I guess the best way to deal with something of this senario is present clear error messages that let the user know what they failed to give or present, and second is to use the finally clause. The finally clause will give about the only way to "clean up" your code after failure, although you must be careful here also. Using the finally clause means that it will execute every time the code is run(you know this already), which means some kind of flag must be created in order for proper cleanup.
Hope that makes a little sense and helps out some. I'm sure you know all of that already and are seeking more advanced techniques, but that's about the only path you can go down on.
regards,
George
![]() |
Similar Threads
- C / C++ FAQ's and Practice problems (C++)
- Round-Robin Scheduling Algorithm (Java)
- I need help for connecting to Ms access database (VB.NET)
- need help plz (JSP)
- Program, help (Java)
- Compiling And Running (Java)
- Populating & Retrieving Data in a listbox : ASP.NET (w/ VB.NET) (ASP.NET)
Other Threads in the Java Forum
- Previous Thread: Exception in thread "QueuedExecutor"
- Next Thread: Making searching faster
| Thread Tools | Search this Thread |
-xlint actionlistener android api applet application array automation bi binary blackberry block bluetooth character class client code compile compiler component consumer database desktop developmenthelp eclipse error fractal freeze ftp functiontesting game gameprogramming givemetehcodez graphics gui health html hyper ide image int j2me j2seprojects java javac javaee javaprojects jetbrains jni jpanel jtable julia learningresources lego linked linux mac main map method mobile myregfun netbeans nonstatic notdisplaying number online pearl printf problem program project qt researchinmotion rotatetext rsa scanner screen server set singleton sms sort spamblocker sql string swing system textfields thread threads time title tree tutorial-sample update variablebinding windows working xor






