• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

prob in exception....

 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
import java.io.*;
class MyException extends Exception
{
public static void main(String args[]) throws IOException
{DataInputStream in=new DataInputStream(System.in);
//String a[]=new String();
//in.readLine();
try
{ System.out.println("try block");
check_food(in.readLine());
}
catch(MyException me)
{
System.out.println("Exception caught");
}

}
static void check_food(String f) throws MyException
{
if(f=="good")
System.out.println("Its good");
else
throw new MyException();
}
}

I compiled it using javac -deprecation MyException.java
it compiled with one warning
executed as java MyException good
output is :
tryblock

Exception caught

shoudn't it be:
try block
its good
 
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Shashank Sharma:
shoudn't it be:
try block
its good



It should be, but (f == "good") != (f.equals("good"))
 
Ranch Hand
Posts: 375
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Shashank,

For simplicity, lets make an assumption, in.readLine() returns a new String object that was supplied on the command line.

1. Now if you supplied "good" on the command line you'll get new String("good") as a result of calling in.readLine(). The result of the new String is that a new String is created i.e. it is not picked up from the String pool(check the java api)
2. The String literal "good" defined in check_food is a compile-time constant i.e. it exists in the pool - JLS 15.28

Based on the 2 facts above, both the String instances do not point to the same reference & since == is a test for reference equality they do not evaluate to true with the exception being thrown in check_food.

To see the difference between String's created using the new operator & String literals try the following -
* check_food("good")
* check_food(new String("good"))

As a general rule, never test String's for equality using ==, always use the equals method supplied in the String class.

Ashish Hareet
 
Shashank Sharma
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Shashank,

According to my knowledge,using DataInputStream object,you cannot pass
command line argument , but it is used to enter data using keyboard.

So After java MyException command ,it will first print "try block"
and wait,so that you can enter string. If you enter "good" ,then it will print "Its good" , otherwise exception.

If you want to pass Ccommand Line Argument, then use

try
{ System.out.println("try block");
check_food(args[0]);
}

this code, means pass first String of Array args[] rather than in.readLine() method.So it will give desired output.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic