Results 1 to 11 of 11

Thread: Try and Catch's

  1. #1
    dougie1809 is offline Senior Member
    Join Date
    Nov 2011
    Posts
    116
    Rep Power
    0

    Default Try and Catch's

    Hi,

    If you have 3 try-catch blocks in a method, and you want to return just one result. how would you go about doing that?
    Because I getting an output of three integer outputs, when I only want one integer returned.

    Thanks

  2. #2
    MonkeyMan is offline Member
    Join Date
    Jan 2012
    Posts
    45
    Rep Power
    0

    Default Re: Try and Catch's

    Show me some code baby! :)

  3. #3
    pbrockway2 is online now Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,571
    Rep Power
    12

    Default Re: Try and Catch's

    Because I getting an output of three integer outputs, when I only want one integer returned.
    I'm not sure I understand this. Methods, in Java, are constrained to only ever return a single thing: a primitive value (like an int) or a reference value (String etc).

    You will get whatever output a method creates - with System.out.println() or similar - and this output has nothing to do with what the method returns.

    Perhaps you could post some code that illustrates your problem. Something brief, that compiles but does not do, at runtime, what you intend.

  4. #4
    dougie1809 is offline Senior Member
    Join Date
    Nov 2011
    Posts
    116
    Rep Power
    0

    Default Re: Try and Catch's

    Ok. This is the method, but it needs other classes to get it executing, so don't mind what the method does. Code is below:

    Java Code:
    //Method to get the Process ID of a port number
    	public int getPID(String portAdd)
    	{
    		convertTextFile();
    		int pid=0;
    		//loop through each line in the text file
    		for(String line: textfile) 
    		{
      			fields = p.split(line);
    		  for(String currField: fields)
    		  {
    		 		if(portAdd.equalsIgnoreCase(currField))
    				{
    					try
    					{
    						String portAddr = fields[KEY_ADDR_P];
      						pid = Integer.valueOf(fields[3]);
      						myPIDs.put(portAddr, pid);
    					}
    					catch(NumberFormatException nfe)
    					{
    					}
    				}
    				
    				if(portAdd.equalsIgnoreCase(currField))
    				{
    					try
    					{
    						String portAddr = fields[KEY_ADDR_P];
      						pid = Integer.valueOf(fields[4]);
      						myPIDs.put(portAddr, pid);
    					}
    					catch(NumberFormatException nfe)
    					{
    					}
    					catch(ArrayIndexOutOfBoundsException e)
    					{
    					}	
    				}
    				
    				if(portAdd.equalsIgnoreCase(currField))
    				{
    					try
    					{
    						String portAddr = fields[KEY_ADDR_P];
      						pid = Integer.valueOf(fields[5]);
      						myPIDs.put(portAddr, pid);
    						System.out.println(pid);
    					}
    					catch(NumberFormatException nfe)
    					{
    					}
    					catch(ArrayIndexOutOfBoundsException e)
    					{
    					}
    				}
    			}					
    		}
    		return pid;
    	}
    So in each if statement is a try-catch block to check it if a portNumber is the same as the number in an array, if not, catch it and do nothing. At least one of the those if statements will be correct and will return the value i need. Instead for an example its returning 0,0,4, where I just want the 4.?

    Thanks
    Last edited by pbrockway2; 03-15-2012 at 02:40 AM. Reason: code tags added

  5. #5
    pbrockway2 is online now Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,571
    Rep Power
    12

    Default Re: Try and Catch's

    Please use code tags.

  6. #6
    dougie1809 is offline Senior Member
    Join Date
    Nov 2011
    Posts
    116
    Rep Power
    0

    Default Re: Try and Catch's

    Yes sorry, I always forget using code tags.
    But do you understand my problem in the code?

  7. #7
    yellowledbet is offline Senior Member
    Join Date
    Feb 2011
    Location
    Georgia, USA
    Posts
    122
    Rep Power
    0

    Default Re: Try and Catch's

    Instead for an example its returning 0,0,4, where I just want the 4
    I am confused as an int will not return 0,0,4.

  8. #8
    yellowledbet is offline Senior Member
    Join Date
    Feb 2011
    Location
    Georgia, USA
    Posts
    122
    Rep Power
    0

    Default Re: Try and Catch's

    what are these lines supposed to be doing?

    Java Code:
    
    		 		if(portAdd.equalsIgnoreCase(currField))
    				{
    					try
    					{
    						String portAddr = fields[KEY_ADDR_P];
      						pid = Integer.valueOf(fields[3]);
      						myPIDs.put(portAddr, pid);
    					}
    					catch(NumberFormatException nfe)
    					{
    					}
    				}

  9. #9
    dougie1809 is offline Senior Member
    Join Date
    Nov 2011
    Posts
    116
    Rep Power
    0

    Default Re: Try and Catch's

    Sorry for the confusion. Ive found my problem. Thanks for your help :D

  10. #10
    pbrockway2 is online now Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,571
    Rep Power
    12

    Default Re: Try and Catch's

    Quote Originally Posted by dougie1809 View Post
    Yes sorry, I always forget using code tags.
    But do you understand my problem in the code?
    Not really - it really would help if what you posted would compile and if you said exactly what you expected it to output (possibly multiple things) and what it should return (always a single thing). Such a SSCCE takes more work than posting an extract of your code, but it is more productive.

  11. #11
    pbrockway2 is online now Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,571
    Rep Power
    12

    Default Re: Try and Catch's

    Didn't see your previous post - glad you've got it sorted out.

Similar Threads

  1. Try... Catch?
    By Callofdudey in forum New To Java
    Replies: 3
    Last Post: 10-06-2011, 10:29 PM
  2. how to catch two exceptions in one catch()?
    By arnab321 in forum New To Java
    Replies: 1
    Last Post: 11-06-2008, 11:54 AM
  3. try catch...
    By MarkWilson in forum New To Java
    Replies: 8
    Last Post: 06-27-2008, 06:39 PM
  4. try catch!?
    By Joe2003 in forum Advanced Java
    Replies: 2
    Last Post: 01-28-2008, 08:51 PM
  5. Use try and catch
    By zoe in forum New To Java
    Replies: 2
    Last Post: 07-25-2007, 08:50 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •