Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

regarding exception

  Asked By: Alma    Date: Apr 27    Category: Java    Views: 538
  

i have
a code like this:

class test
{
public static void main(String a[])
{
try
{ int i=2;
int j=i/0;
}
catch(Exception e)
{
return;
}
}
}
-----
by running above code i seen my program get terminated
and control back to operating system.
i like to know what the return statement do in such
situation. pls help.

Share: 

 

4 Answers Found

 
Answer #1    Answered By: Miriam Green     Answered On: Apr 27


return always returns back  from the method to the
point where the method was called. This is valid only
if the called method ('main' in this case) has void  as
the return  parameter.
Here, since 'main' is called by the java environment,
return takes you back to the OS.

 
Answer #2    Answered By: Alberta Miller     Answered On: Apr 27

It propably be

class test
>{
> public static  void main(String args[])

 
Answer #3    Answered By: Debbie Reyes     Answered On: Apr 27

The return  statement means the that system.exit(0)

 
Answer #4    Answered By: Leroy Schmidt     Answered On: Apr 27

Your Porgram is correct.It would be more accurate if you
use "java.lang.ArithmeticException " instead of generic exception.
Because you are of exception  goin to occuer.
The exception get caught and the control  goes to the catch block. it
finds only return  statement. so it comes out of the programme and so
only to the command prompt.the follwoing example (little modification
of your prog) would lt you to understand better.

class test
{
public static  void main(String a[])
{
try
int  i=2;
int j=i/0;
}
catch(java.lang.ArithmeticException e)
{
System.out.println("I have caught exception "+e);
return;
}
}
}

let me know if you have any other query

 
Didn't find what you were looking for? Find more on regarding exception Or get search suggestion and latest updates.




Tagged: