GIDForums  

Go Back   GIDForums > Computer Programming Forums > Java Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 11-Mar-2009, 06:57
bostero22 bostero22 is offline
Junior Member
 
Join Date: Oct 2008
Posts: 50
bostero22 is on a distinguished road

Catching & Throwing Exceptions


Hello, 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  
Old 11-Mar-2009, 07:59
blakekl's Avatar
blakekl blakekl is offline
Junior Member
 
Join Date: Nov 2007
Posts: 35
blakekl will become famous soon enough

Re: Catching & Throwing Exceptions


Sounds 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  
Old 11-Mar-2009, 10:50
bostero22 bostero22 is offline
Junior Member
 
Join Date: Oct 2008
Posts: 50
bostero22 is on a distinguished road

Re: Catching & Throwing Exceptions


Thank 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:
public class Main {

    public static void main(String[] args) {

        
        Scanner input = new Scanner(System.in);
        int integer = 0;


        try{
            System.out.println("Please enter an int");
            integer = input.nextInt();
        }
        catch (InputMismatchException e)
        {
            System.out.println("Incorrect Type");
            
        }


    }
}
that would terminate the program of course, but how would I ask the user again for the integer?
  #4  
Old 11-Mar-2009, 15:39
blakekl's Avatar
blakekl blakekl is offline
Junior Member
 
Join Date: Nov 2007
Posts: 35
blakekl will become famous soon enough

Re: Catching & Throwing Exceptions


The 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:
public void getInput() {
   //Code to get input
}
public static void main(string[] args) {
   try {
      getInput()
   }
   catch {
      System.out.println("Invalid input. Valid inputs are: blah blah blah");
      getInput()
   }
}
Of course, if they put in an invalid input twice, the program will still terminate.
  #5  
Old 16-Mar-2009, 04:28
mamz mamz is offline
New Member
 
Join Date: Jan 2008
Posts: 19
mamz is on a distinguished road

Re: Catching & Throwing Exceptions


this 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:
public void getInput() {
   //Code to get input
}
public static void main(string[] args) {
getInputRecurse(){
 try {
      getInput()
   }
   catch {
      System.out.println("Invalid input. Valid inputs are: blah blah blah");
      getInputRecurse()
   }}  

}
 
 

Recent GIDBlogNot selected for officer school by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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

All times are GMT -6. The time now is 18:51.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.