This week's book giveaway is in the Reactive Progamming forum.
We're giving away four copies of Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams and have Adam Davis on-line!
See this thread for details.
Win a copy of Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams this week in the Reactive Progamming 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
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

throw an Exception....

 
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
SOURCE:www.javabeat.net


(1) Compilation error
(2) Runtime error
(3) Compile successfully, nothing is printed
(4) Inside throwMethod. followed by caught: java.lang.IllegalAccessExcption: demo

My answer is (4).
Because exception which is thrown in throwMethod() will be caught by catch and it displays
�Inside throwMethod. followed by caught: java.lang.IllegalAccessExcption: demo�.
But the answer is (1).
Can anyone clarify me?

 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The method throwMethod() explicitly throws an exception. But it is not handled within that method nor it specifies that it throws an exception.

Thus the compilation error.

If instead of a explicit exception it was a runtime exception ex. divide by zero or array out of bounds then your answer would be correct.


alt: add a throws IllegalAccessException to the signature of the throwMethod and yiour answer will be displayed.
 
Ganeshkumar cheekati
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if i modified to this



output is: Inside throwMethod.
finally :

why it is not showing "Caught."?


 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

By rule if you throw a exception in your method, the method must handle or declare the exception.

Just declaring that the method may throw IllegalAccessException (as you did) does not ensure that exception will be thrown in that method.

You have commented the line "throw new IllegalAccessException("demo");" in your throwMethod(). The code has executed normally and the catch block has not executed.
 
Ranch Hand
Posts: 826
Eclipse IDE Oracle Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See the throws keyword tells to the complier that "I know the throwMethod() is risky and it throws IllegalStateException". By doing this you don't have to handle the exception as it occurs. When the exception is thrown the control goes to the calling method...

When you do throw new IllegalAccessException("demo") a new exception is thrown and the control goes to the caller method where it is handled and the message is printed inside catch...

But when you comment the line no new exception is thrown and the execution is completed without any exception...
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ganeshkumar,

throws never means it will throw all the time. Its just safety that
compiler demands in case of checked exception when you are not handing
the exception but declaring it.

Exception may or may not be thrown, irrespective of whether you have declared
that in method signature.
 
Ganeshkumar cheekati
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi chandra...

do you mean sometimes it can throw an exception?

if i remove try,catch,finally in main method it is showing a compilation error.

but if i mention it is not showing statement in output which means exception is not handled...
 
sudipto shekhar
Ranch Hand
Posts: 826
Eclipse IDE Oracle Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Ganeshkumar cheekati:

if i remove try,catch,finally in main method it is showing a compilation error.



If you remove try,catch from the main method and an exception is thrown...the exception might be saying exception is not handled by your code.
As i said when an exception occurs either you handle it or declare it...

If the exception occurs and you declare it but not handle it the execution is sent back to the caller method...which in turn is supposed to handle the exception....In your case the main() is the caller...So execution is ducked to main() from the risky method...And in main() the compiler finds that neither have you handled the exception nor have you declared it.
Hence the compilation fails.
[ December 04, 2008: Message edited by: Sudipto Shekhar ]
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ganeshkumar,

By declaring an Exception, its not necessary that you must throw the
exception. But compiler wont escape if you are throwing a checked exception
without declaring or handing it with try catch.


I think its clear now.
 
Maybe he went home and went to bed. And took this tiny ad with him:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!