Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Exception

 
sudha siva
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class Test2
{
public static void main(String[] args)
{
int d = 0;
try
{
int i = 1 / (d* doIt());
} catch (Exception e)
{
System.out.println(e);
}
}
public static int doIt() throws Exception
{
throw new Exception("Forget It");
}
}
The output of the above code is java.lang.Exception Forget it.I don't how the Forget it in doIt() printed out.Can anyone explain me.

Thanks
sudha
 
Fei Ng
Ranch Hand
Posts: 1245
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by sudha siva:
class Test2
{
public static void main(String[] args)
{
int d = 0;
try
{
//1 int i = 1 / (d* doIt());
//4 } catch (Exception e)
{
//5 System.out.println(e);
}
}
//2 public static int doIt() throws Exception
{
//3 throw new Exception("Forget It");
}
}
The output of the above code is java.lang.Exception Forget it.I don't how the Forget it in doIt() printed out.Can anyone explain me.

Thanks
sudha

Follow the red number... t
This (d* doIt()), calls the doIt() method that throws Exception.
In the method, it throw new Exception("Forget It"). So there is a exception and since doIt() throws exception (this means let the method which called doIt() to handle the exception) it goes back to main() which catch the exception with catch (Exception e){ }.
The it just print it out.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic