![]() |
|
#1
|
|||
|
|||
Catching & Throwing ExceptionsHello, I just wanted to get some advice.. I been reading about catching and throwing exceptions but I still don't know if i got the concept right...
so catching an exception would be an exception that is caught and there is something being done about it to handle it.. like try to recover from it... throwing an exception is that if the exception is actually thrown.. terminate the program? Any advice would be helpfull... thank you! |
|||
|
#2
|
||||
|
||||
Re: Catching & Throwing ExceptionsSounds like you've got it just about right. Make sure you put any code that does throw an exception inside a try{} statement. Immediately following that should be the catch. Say you prompted the user for a file name that you wanted to open. Opening the file could throw a few different kinds of exceptions: the file might not exist or have different permissions. The catch statement should report the error and reprompt for a new file name. You don't always want to end the program if an exception is caught.
|
|
#3
|
|||
|
|||
Re: Catching & Throwing ExceptionsThank you.. so let's say I want to catch if the user enters an invalid type of data but I don't want to terminate the program... how would I do that? i cant figure it out.. for example:
JAVA Code:
|
|
#4
|
||||
|
||||
Re: Catching & Throwing ExceptionsThe best thing would be to put the code you have inside the try{} block inside a seperate method that you call inside the try{} block. Then, you can print an error message inside the catch{} block and recall that same function. This isn't working code, or even complete, but it should give you an idea.
JAVA Code:
|
|
#5
|
|||
|
|||
Re: Catching & Throwing Exceptionsthis will ask for the correct input for unlimited attempts. if u want to limit it to few attempts keep a count on attempts variable and check it all the time.
JAVA Code:
|
Recent GIDBlog
Not selected for officer school by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Throwing exceptions | BobLewiston | .NET Forum | 1 | 06-Apr-2009 06:29 |
| Terminate called after throwing an instance of 'std::bad_alloc' what(): St9bad_all | upadhyad | C++ Forum | 8 | 12-Jul-2008 15:42 |
| Memory Allocation Exceptions in C++ | David52 | C++ Forum | 10 | 19-Apr-2006 13:13 |
| catching exception | alcoholic | C++ Forum | 2 | 09-Feb-2006 21:48 |
| Need help with "atol". Exceptions handling? | nikp | C++ Forum | 1 | 02-Dec-2005 08:51 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The