Exception handling

  • (2 Pages)
  • +
  • 1
  • 2

19 Replies - 2873 Views - Last Post: 28 April 2011 - 07:36 PM Rate Topic: -----

#1 zake2  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 41
  • Joined: 24-February 11

Exception handling

Posted 26 April 2011 - 06:26 AM

My program has an exception class called StringTooLongException. I also have a class called InputString. I want to prompt the user to input a strings and when the string is more than 20 x-ters.If its greater than 20, it will throw the problem and print a message. Here is what i have....(I have just attached coz its an assignment, pliz 4giv me for that)
You can highlight areas that need correction in the code

Attached File(s)


Is This A Good Question/Topic? 0
  • +

Replies To: Exception handling

#2 ShaneK  Icon User is offline

  • require_once("brain.php"); //Fatal error :/
  • member icon

Reputation: 240
  • View blog
  • Posts: 1,224
  • Joined: 10-May 09

Re: Exception handling

Posted 26 April 2011 - 06:42 AM

while (!str.equals("DONE"))
{
try
{
num = Integer.parseInt(str);
if(num > MAX)
throw problem;//****error here...saying incompatible types...required "Throwable"*****/
}
catch(StringTooLongException problem)
{
System.out.println("Input String is too long.");
}

}
System.out.println("End of main method.");
}


This has a lot of flaws...

First off, it's an infinite while loop because you never re-set str. Secondly, why do the try and catch at all if it's in the same area of the program? Thirdly, what's this? num = Integer.parseInt(str); I thought you were trying to get the size of the string, which would be num = str.size();. Fourthly, I don't see where you declared "problem". If it's a psudo word then ignore that and read this: It should be throw new StringTooLongException("Your string is too long.");

Fix these things and then report back and I'll see what else I can find if you have more problems.

Also, what's an x-ter?

Yours,
Shane~

This post has been edited by ShaneK: 26 April 2011 - 06:44 AM

Was This Post Helpful? 1
  • +
  • -

#3 zake2  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 41
  • Joined: 24-February 11

Re: Exception handling

Posted 26 April 2011 - 07:40 AM

There's an error where there's a comment
{
try
{
num= str.size();
if(num > MAX)
throw;//*Error says illegal start of expression*/
}
catch(StringTooLongException problem)
{
System.out.println("Input String is too long.");
}

}
System.out.println("End of main method.");
}

}




There's an error where there's a comment
{
try
{
num= str.size();
if(num > MAX)
throw;//*Error says illegal start of expression*/
}
catch(StringTooLongException problem)
{
System.out.println("Input String is too long.");
}

}
System.out.println("End of main method.");
}

}


Attached File(s)


Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10780
  • View blog
  • Posts: 40,145
  • Joined: 27-December 08

Re: Exception handling

Posted 26 April 2011 - 08:07 AM

You have to throw some Throwable, either an Exception or Error.
throw new Exception();



If you have other code with errors, please post it in the body of the thread using code tags, rather than as an attachment. Also post the specific errors as they appear on your compiler.
Was This Post Helpful? 1
  • +
  • -

#5 zake2  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 41
  • Joined: 24-February 11

Re: Exception handling

Posted 26 April 2011 - 08:58 AM

My program is okay
But my loop shows that if I enter "DONE", it execution is passed outside the Loop. There's no effect, I have tried it several times. What could be the problem again?

do{
      System.out.println("Enter the string: ");
      input = s.nextLine();
      if (input.length() > MAX)
      throw problem;
        }
      while(input!="DONE");


This post has been edited by zake2: 26 April 2011 - 09:01 AM

Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10780
  • View blog
  • Posts: 40,145
  • Joined: 27-December 08

Re: Exception handling

Posted 26 April 2011 - 09:05 AM

Don't compare Strings (or Objects in general) using the == and != operators, as it compares their locations in memory. Use the equals() method instead, which compares their values.
do{

}while(!myStr.equals("DONE"));


Was This Post Helpful? 3
  • +
  • -

#7 zake2  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 41
  • Joined: 24-February 11

Re: Exception handling

Posted 26 April 2011 - 10:04 AM

View Postmacosxnerd101, on 26 April 2011 - 09:05 AM, said:

Don't compare Strings (or Objects in general) using the == and != operators, as it compares their locations in memory. Use the equals() method instead, which compares their values.
do{

}while(!myStr.equals("DONE"));





This piece of code is not considering the first string that is entered, it only looks at 2nd and the others entered.
If I write a seperate code to cater for only that part of the 1st enrty, and then follow it with a that do.....while.
do{
      System.out.println("Enter the string: ");
      input = s.nextLine();
      if (input.length() > MAX)
      throw problem;
        }
      while(!myStr.equals("DONE"));




Was This Post Helpful? 0
  • +
  • -

#8 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10780
  • View blog
  • Posts: 40,145
  • Joined: 27-December 08

Re: Exception handling

Posted 26 April 2011 - 10:07 AM

A do loop executes initially at least once, then evaluates the condition. Also, I was using myStr to represent some String, not as copy/paste code. Make sure you use the appropriate variable name for the String you want to evaluate in your condition in the loop.
Was This Post Helpful? 2
  • +
  • -

#9 zake2  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 41
  • Joined: 24-February 11

Re: Exception handling

Posted 26 April 2011 - 10:20 AM

View Postmacosxnerd101, on 26 April 2011 - 10:07 AM, said:

A do loop executes initially at least once, then evaluates the condition. Also, I was using myStr to represent some String, not as copy/paste code. Make sure you use the appropriate variable name for the String you want to evaluate in your condition in the loop.




Hey, when I'm using an "else if" and I want the program to terminate once the else condition is not true
eg


else if(mystr=="DONE")//don't mind abt the syntax
print "YO STOPING HERE!"

// Though there are other codes, I want it to terminate if that condition is true



Was This Post Helpful? 0
  • +
  • -

#10 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10780
  • View blog
  • Posts: 40,145
  • Joined: 27-December 08

Re: Exception handling

Posted 26 April 2011 - 10:25 AM

You don't need the else if condition. That's the whole point of a do loop.

do{

}while(!myStr.equals("DONE"));

System.out.println("Terminated the loop");


Was This Post Helpful? 0
  • +
  • -

#11 zake2  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 41
  • Joined: 24-February 11

Re: Exception handling

Posted 26 April 2011 - 10:56 AM

Now if I want to continue with the entering of the strings.
The exception should be displayed as a message but other parts of the program below can continue. How can I achieve this?
Do I use the "finally" keyword?
Was This Post Helpful? 0
  • +
  • -

#12 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: Exception handling

Posted 26 April 2011 - 09:07 PM

View Postzake2, on 26 April 2011 - 12:56 PM, said:

Now if I want to continue with the entering of the strings.
The exception should be displayed as a message but other parts of the program below can continue. How can I achieve this?
Do I use the "finally" keyword?

So your a really misundertanding of what throwing an exception is.
When you throw an Exception it is to be catch by the method that called the method that throw the Exception.

Throwing and catching (or finally) in the same method is useless, you do not need to throw an exception if you are catching it in the same method. A simple if will do

if(string.length() > MAX)
... act here
Was This Post Helpful? 0
  • +
  • -

#13 zake2  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 41
  • Joined: 24-February 11

Re: Exception handling

Posted 27 April 2011 - 01:09 AM

My code below has 3 errors(commented lines)
My intension is to capture an exception, display the message and the proceed with entering other strings.



import java.util.Scanner;
public class Exceptions
{
   
   public static void main (String[] args) 
   {
      final int  MAX=20;
	 
	  StringTooLongException e= new StringTooLongException("string too long");//2 ERRORS ON THIS LINE

	  
      Scanner s = new Scanner (System.in);
      System.out.println("Enter another string: ");
      String input = s.nextLine();
      	
      while(!input.equals("DONE"))
           {
      System.out.println();
      System.out.println("Enter another string: ");
       input = s.nextLine();
       }
       System.out.println ("YOU CHOSE TO STOP DEAR!");
      
      //____________________________________________
      try{
      	 
      if (input.length() > MAX)
      System.out.println("Enter another string: ");
      input = s.nextLine();
      }
      //-------------------------------------------
      
      
      
      
      //_____________________________
      catch(StringTooLongException e)// ERROR ON THIS LINE
      	{
     System.out.println("Error message: " + e.getMessage());
	 System.out.println("call stack trace: " + e.getStackTrace());	
     	}
     //------------------------------
     
     
     
     
     
     //___________________________________________
     finally{
     	while(!input.equals("DONE"))
           {
      System.out.println();
      System.out.println("Enter another string: ");
      input = s.nextLine();
      		}
      System.out.println ("YOU CHOSE TO STOP DEAR!");
            }
      //------------------------------------------
     
   }
}



Was This Post Helpful? 0
  • +
  • -

#14 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10780
  • View blog
  • Posts: 40,145
  • Joined: 27-December 08

Re: Exception handling

Posted 27 April 2011 - 04:25 AM

And what exactly *are* the errors you are encountering? Post them exactly as they appear on your compiler.

Also, you're missing the concept of a try-catch. Code that should throw a StringTooLongException should be inside of the try-block. Also, you shouldn't be throwing an Exception in the try-block only to catch it later. The try-block is for where you invoke other code that throws that Exception. This deals with proper delegation of responsibilities.

has some good Exception handling tutorials that you should check out:
http://www.dreaminco...-catch-finally/
http://www.dreaminco...g-and-chaining/
http://www.dreaminco...ed-customizing/
Was This Post Helpful? 0
  • +
  • -

#15 zake2  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 41
  • Joined: 24-February 11

Re: Exception handling

Posted 27 April 2011 - 08:16 AM

Have alook at this
errors
--------------------Configuration: <Default>--------------------
F:\zips\ModifiedExceptions.java:21: 'catch' without 'try'
catch(StringTooLongException problem)
^
F:\zips\ModifiedExceptions.java:16: 'try' without 'catch' or 'finally'
try{
^
F:\zips\ModifiedExceptions.java:37: 'class' or 'interface' expected
}
^
F:\zips\ModifiedExceptions.java:38: 'class' or 'interface' expected
^
4 errors


import java.util.Scanner;
public class ModifiedExceptions
{
   
   public static void main (String[] args) throws  StringTooLongException
   {
      final int  MAX=20;// Setting max No. of characters
	 
	  StringTooLongException problem= new StringTooLongException();

	  
      Scanner s = new Scanner (System.in);
      System.out.println("Enter string: ");
      String input = s.nextLine();
      
      try{
      if (input.length() > MAX)
      
      
      
     catch(StringTooLongException problem)
      	  {
     System.out.println("String entered is too long!");	
     	  }
     
     //___________________________________________
     finally{
      System.out.println();
      while(!input.equals("DONE")}
      System.out.println("Enter another string: ");
      input = s.nextLine();
      } 
      System.out.println("End of loop");
            }
      //------------------------------------------
        } 
   }



Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2