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

Do Static methods throw exceptions?

 
Ranch Hand
Posts: 267
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the result when you compile and run the following code from J@WhizTrail mock test?
public class ThrowsDemo
{
static void throwMethod()
{
System.out.println("Inside throwMethod.");
throw new IllegalAccessException("demo");
}

public static void main(String args[])
{
try
{
throwMethod();
}
catch (IllegalAccessException e)
{
System.out.println("Caught " + e);
}
}
}
 
High Plains Drifter
Posts: 7292
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This code won't compile because the method that contains the throw statement doesn't declare it. If you modify the method signature accordingly:

it compiles and runs just fine. The output will look like this:
Inside throwMethod
demo
------------------
Michael Ernest, co-author of: The Complete Java 2 Certification Study Guide
 
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The throwMethod() should look like:

Otherwise it wont complie
 
Roopa Bagur
Ranch Hand
Posts: 267
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Michael but my confusion was whether static methods can throw exceptions..Or is it that Static blocks cannot throw exceptions?..Please clarify.

Originally posted by Michael Ernest:
[B]This code won't compile because the method that contains the throw statement doesn't declare it. If you modify the method signature accordingly:

it compiles and runs just fine. The output will look like this:
Inside throwMethod
demo
[/B]


 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The result is Compilar error, Michal,faisal is right please go throw,Throw statement method signature.
when I changed the code
static void throwMethod() throw IllegalAccessException
{
System.out.println("Inside throwMethod");
throw new IllegalAccessException("demo);
}
This will compile and the output:
Inside throwMethod
Caughtjava.lang.IllegalAccessException: demo
 
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What would happend if instead of throwing IllegalAccessException
java.awt.color.CMMException is thrown in the throwMethod?? Does the code compile (the catch part also with java.awt.color.CMMException )?? how could I know ?.

My point is ? should I memorize all the Exception hierarchy to know if a exception instance is subclass of RuntimeException or not ?
How could I determine this ? :-/
Bye
Zkr Ryz
=======


Originally posted by Michael Ernest:
[B]This code won't compile because the method that contains the throw statement doesn't declare it. If you modify the method signature accordingly:

it compiles and runs just fine. The output will look like this:
Inside throwMethod
demo
[/B]


 
Michael Ernest
High Plains Drifter
Posts: 7292
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Roopa Bagur:
Thanks Michael but my confusion was whether static methods can throw exceptions..Or is it that Static blocks cannot throw exceptions?..Please clarify.


Static methods can throw exceptions. If by static block you mean a static initializer, as in:

The answer is no, you can't do that. Static initializer code must be able to complete normally, since it's tied into class initialization.
------------------
Michael Ernest, co-author of: The Complete Java 2 Certification Study Guide
 
Michael Ernest
High Plains Drifter
Posts: 7292
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Zkr Ryz:

My point is ? should I memorize all the Exception hierarchy to know if a exception instance is subclass of RuntimeException or not ?
How could I determine this ? :-/


You could memorize the Exception class hierarchy if you wanted to. I've memorize some just through repetition. The rest I just look up in the documentation. You'll notice of course that static initializers do not allow RuntimeExceptions either. You'll get one less compiler complaint, but that doesn't quite get you to bytecode.

------------------
Michael Ernest, co-author of: The Complete Java 2 Certification Study Guide
 
Ranch Hand
Posts: 2596
Android Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Michael Ernest:
You could memorize the Exception class hierarchy if you wanted to. I've memorize some just through repetition. The rest I just look up in the documentation. You'll notice of course that static initializers do not allow RuntimeExceptions either. You'll get one less compiler complaint, but that doesn't quite get you to bytecode.


Hi Michael,
I had also asked this question abt. remembering exception hierarchy from test point of view and I was informed that in the actual test the question will certainly denote which class is a subclass of which class, as it is given in the sample questions of your book. Here is my original question again -


I have 2 questions
(1) Do I need to remember the hierarchy of different exceptions in order to correctly identify in the exam if the subclass overriden method is throwing an exception which is not a subclass of an exception which is thrown in the superclass overriding method? Does the real exam provide hierarchy of the used exceptions in the question?
example:
Superclass overriding method throws exception IOException and subclass overriden method throws exception MalformedURLException; in this case does the real exam mention that MalformedURLException is a subclass of IOException?
2)Do I need to know abt methods of Throwable/Exception such as getMessage(), fillInStackTrace(), getLocalizedMessage() etc for the exam? If yes, what sort of details do I need to know abt them?


Could you pls. clarify?
TIA,
- Manish

[This message has been edited by Manish Hatwalne (edited October 24, 2001).]
 
Michael Ernest
High Plains Drifter
Posts: 7292
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Doh! I forgot the context of the forum which is why I answered the way I did -- must have thought I was in Intermediate Java. Sorry about the confusion.
You should know the differences between the Error, Exception and RuntimeException classes and their uses, for sure. I'd have a working knowledge of Exception's methods, and I wouldn't spend a lot of time thinking about Throwable.
More importantly, you should feel comfortable with exception handling, the 'handle-or-declare' rule, and the notions of 'checked' and unchecked' exceptions.
Those of you fascinated with perfecting your exam, spare no effort.
------------------
Michael Ernest, co-author of: The Complete Java 2 Certification Study Guide
 
Proudly marching to the beat of a different kettle of fish... while reading this tiny ad
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!