This week's book giveaway is in the Server-Side JavaScript and NodeJS forum.
We're giving away four copies of Modern JavaScript for the Impatient and have Cay Horstmann on-line!
See this thread for details.
Win a copy of Modern JavaScript for the Impatient this week in the Server-Side JavaScript and NodeJS forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Exception handling...

 
Ranch Hand
Posts: 265
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,i got it from master exam

class outer
{
static int x=1;

public static void main(String args[])throws Throwable
{
try
{
if(x==1)
throw new Throwable();
System.out.println("try");
}
catch(Exception e)
{
System.out.println("exe");
}
finally
{
System.out.println("fin");
}

}
}

output: fin followed by runtimeexception.

I thought this will give compiler error since "try" is unreachable to execute.

and i know if this throw statement is inside any method then it wont give any error,i want to know the reason for this..

please someone clear this...

Thanks in advance
Preetha
 
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if(x==1)//this is the reason Arun, exception is thrown under condition.
throw new Throwable();

It is like :
if(x==1){
throw new Throwable();
}
 
Preethi Dev
Ranch Hand
Posts: 265
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so,if throw statement is under some condition it wont affect the statement next to that block. am i correct?
any special reason behind it?

Thanks
Preetha
 
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But why it is showing runtimeException???
 
Ranch Hand
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What you said about unreachable statement is right BUT here,
If the if() condition fails the print statement will be executed.
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by geeta vemula:
But why it is showing runtimeException???




if(x==1)
throw new Throwable();
System.out.println("try");
}
catch(Exception e)
{
System.out.println("exe");
}

Throwable is super class of Exception class, so it cannot be caught with Exception. Thats why it is throwing runtime exception.
 
Preethi Dev
Ranch Hand
Posts: 265
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but throwable is not runtime exception.
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ya you are right Arun, Throwable is not RuntimeException. Thats why if you run the program, you will get Throwable Exception:

Exception in thread "main" java.lang.Throwable

 
Does this tiny ad smell okay to you?
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic