SIDE-BANNERSIDE-BANNER * Oh God! Let there be PEACE. Make them calm, gentle, humble and stable. Make them realize their mistakes. Make them to confess, seek and pray for forgiveness. Let there be forgiveness for true hearts. Show them the right path on which they can walk. Let there be justice, satisfaction and happiness. Let them know that you are the justice and truth. Let them know that the innocent is never punished. Give them the strength, courage and wisdom to quit bad and to achieve good purpose. Let there be leaders who are followed by goodness. Make them competitive to achieve good. Remove their greed, wickedness and bad ambitions. Let them know that your love is infinite. Speak to them in simple ways so they understand. May your heart hear their pure hearts. * Never say die.Live and let live.* Accept Good if not, it Perishes.* What you Sow is what you Reap.* United we Stand divided we Fall.* Where there is a Will there is a Way.* Love is the strongest Power on earth.* When going gets Tough the Tough gets going.* For every problem created there is a perfect Solution.* When your hand starts paining you don't Cut it, Cure it.* A good purpose of life is the persistence to be constructive.* The arch of justice is wide, but it bends towards freedom and justice.* Lion king says he wants all the Power but none of the Responsibilities.* Life is a puzzle which can be solved by Hard work, Character and Discipline.* Money is like Manure, Spread it and it does good.* Pile it up in one place and it Stinks.* When a man wants to be a King he should exactly know the responsibilities of a King.* The only thing necessary for evil to triumph is for good men to do nothing - Edmund Burke.* Face is the reflection of mind.* A purpose of life is a life of purpose.* Beauty lies in the eyes of the beholder.* Necessity is the mother of all inventions. Real friends are those who help us in troubles.* Freedom and Power are lost if it’s miss utilized. Repeating mistakes will lead to sin and blunders.* Quantity is appreciated only when it has Quality.* Everyday is a new day, which brings hope with it.* Ego of superiority is the destruction of individuality.* We cannot learn to swim without going into the water.* Everything happens for good and thus leads to destiny.* Every problem has a perfect solution, we need to find them.* A good purpose of life is the persistence for constructiveness.* It’s hard to create good things where as it’s easy to break them.* Ideas are appreciated and respected only when we express them.* Mistakes do happen by humans, they are forgiven when we pray.* Freedom means giving others the right to achieve good purposes.* We have to put our efforts and leave the rest for destiny to decide.* All big things start with a first step.* First step is sometimes difficult.* Prayers come true when the purpose is good, thus pray for everyone.* Dreams come true when we have faith and pray for good things in life.* We got to have strength, courage and wisdom to achieve good things in life.* Every relationship has a meaning; we have to give them the proper meaning.* The only thing necessary for the triumph of evil is for good men to do nothing.* If wealth is lost, nothing is lost. If health is lost, something is lost. But, if character is lost, everything is lost.* “Stand up, be bold, be strong. Take the whole responsibility on your own shoulders, and know that you are the creator of your own destiny.” - Swami Vivekananda.
HOME-PAGEREALIZATIONQUOTESPUZZLESPRAYERSPERCEPTIONSMUSIC-DOWNLOADSTORIESJOKES
BOOKSBITTER-TRUTHANCIENT-SCRIPTURESBEAUTIFUL-LIFETHOUGHTSFRIENDSHIPPRAYERS-TO-WORSHIPWinning-Publications

QUANTITY is appreciated only when it has QUALITY. Recitation is the mother of Memory. Necessity is the mother of Invention. Knowledge grows when distributed. Enrichment for Information Technology. Persistence to be constructive is the key to success.

Thursday, February 14, 2008

CORE-JAVA (Exceptions)

*[READ: Why-JAVA] *[Start-Learning-JAVA] *[Why-Java's-Hot] *[Start-Using-LINUX] *[Java-Q-&-A] *[Question] *[Java-SE-6-Doc] *[Struts-Doc] *[Java-Tutorial-Index] *[Java-Certification]
*[Tech.] *[Struts.] *[Servlet.] *[JSP.] *[EJB.] *[JNDI-JMS] *[SQL.] *[JDBC.]
CORE-JAVA: *[OOP] *[CLASS] *[ABSTRACT] *[EXCEPTIONS] *[THREADS] *[UTILS] *[PACKAGES] *[JVM] *[CASTING] *[NETWORKING] *[RMI-XML] * Add to Google

* What is the purpose of garbage collection in Java, and when is it used? (garbage)
A: Garbage collection is known for Automatic Memory Management as JVM automatically removes the unused variables/objects (value is null) from the memory. User program can't directly free the object from memory, instead it is the job of the garbage collector to automatically free the objects that are no longer referenced by a program. Every class inherits finalize() method from java.lang.Object, the finalize() method is called by garbage collector when it determines no more references to the existing objects. In Java, it is good idea to explicitly assign null value to a variable, thus by calling System.gc() and Runtime.gc(), JVM tries to recycle the unused objects, but there is no guarantee as to when all the objects will be garbage collected. The purpose of garbage collection is to identify and discard objects that are no longer needed by a program so that their resources can be reclaimed and reused. A Java object is subject to garbage collection when it becomes "UnReachable" to the program in which it is used. (GC Tuning.) (Reducing GC time.)

* Can an object be garbage collected while it is still reachable? (garbage)
A reachable object cannot be garbage collected. Only unreachable objects may be garbage collected.

* Can an object’s finalize() method be invoked while it is reachable? (garbage)
An object’s finalize() method cannot be invoked by the garbage collector while the object is still reachable. However, an object’s finalize() method may be invoked by other objects.

* Can an unreachable object become reachable again? (garbage)
A: An unreachable object may become reachable again when the object's finalize() method is invoked and the object performs an operation which causes it to become accessible to reachable objects. The finalize method may take any action, including making this object available again to other threads. The purpose of finalize, is to perform cleanup actions before the object is discarded.

* If an object is garbage collected, can it become reachable again? (garbage)
Once an object is garbage collected, it ceases to exist. It can no longer become reachable again.

* Does garbage collection guarantee that a program will not run out of memory? (garbage)
A: Garbage collection does not guarantee that a program will not run out of memory. It is possible for programs to use up memory resources faster than they are garbage collected. It is also possible for programs to create objects that are not subject to garbage collection.

* What is the purpose of finalization? (garbage)
The purpose of finalization is to give an unreachable object the opportunity to perform any "cleanup processing" before the object is garbage collected.

* How many times may an object's finalize() method be invoked by the garbage collector? (garbage)
An object's finalize() method may only be invoked once by the garbage collector.

* What are Checked and UnChecked Exceptions? (exception)
A: A checked exception is a SUBCLASS of EXCEPTION (or Exception itself), excluding class RuntimeException and its subclasses. Making an exception checked forces client programmers to deal with the possibility that the exception will be THROWN. eg, IOException thrown by java.io.FileInputStream's read() method. All exceptions are checked exceptions, except for those indicated by Error, RuntimeException, and their subclasses. (different exceptions)
* Unchecked exceptions are RuntimeException and any of its subclasses. Class Error and its subclasses are also unchecked. With an unchecked exception, however, the compiler doesn't force client programmers either to catch the exception or declare it in a throws clause. In fact, client programmers may not even know that the exception could be thrown. eg, StringIndexOutOfBoundsException thrown by String's charAt() method.
Checked exceptions must be caught at compile time. UnChecked Runtime exceptions do not need to be. Note: THROWABLE is the super class of ERROR and EXCEPTION. RUNTIMEEXCEPTION is a subclass of Exception.
* ERROR is a third kind of UnChecked exception. In case of ERROR the application usuall
y prints a stack trace and exits. Errors often CANNOT be caught. (download java examples)

* What are runtime exceptions? (unchecked exception)
A: Runtime exceptions are those exceptions that are thrown at runtime because of either wrong input data or because of wrong business logic etc. These are not checked by the compiler at compile time.

* What class of exceptions are generated by the Java run-time system? (unchecked exception)
The Java runtime system generates RuntimeException and Error exceptions.

* What happens if an exception is not caught? (checked exception)
An uncaught exception results in the uncaughtException() method of the thread's ThreadGroup being invoked, which eventually results in the termination of the program in which it is thrown. It is called by the JVM when a thread in this thread group stops because of an uncaught exception.

* Which arithmetic operations can result in the throwing of an ArithmeticException? (exception)
Integer "divide by zero" that is, "/" and "%" can result in the throwing of an ArithmeticException.


* What is user defined exception? (checked exception)
Apart from the exceptions already defined in Java package libraries, user can define his own exception classes by extending the "Exception" class.

* How to create custom exceptions? (checked exception)
A: Our class should extend the class Exception, or some more specific type there of. EXCEPTION
is a subclass of THROWABLE.

* What is the catch or declare rule for method declarations? (checked exception)
A: If a checked exception may be thrown within the body of a method, the method must either catch the exception or declare it in its throws clause.

* What are checked exceptions? (exception)
A: Checked exception are those which the Java compiler forces us to CATCH. e.g. IOException are checked Exceptions.

* What is the difference between Error and an Exception? (exception)
A: An Error is an irrecoverable condition occurring at runtime. Such as OutOfMemory error. These are JVM errors and we can not repair them at runtime.
While Exceptions are conditions that occur because of bad input etc. e.g. FileNotFoundException will be thrown if the specified file does not exist. Or a NullPointerException will take place if you try using a null reference. In most of the cases it is possible to recover from an exception (probably by giving user a feedback for entering proper values etc.).

* If my class already extends from some other class what should I do if I want an instance of my class to be thrown as an exception object? (exception)
A: One can not do anything in this scenario. Because Java does not allow multiple inheritance and does NOT provide any exception INTERFACE as well.

* How does an exception permeate through the code? (exception)
A: An unhandled exception moves up the method stack in search of a matching When an exception is thrown from a code which is wrapped in a try block followed by one or more catch blocks, a search is made for matching catch block. If a matching type is found then that block will be invoked. If a matching type is not found then the exception moves up the method stack and reaches the caller method. Same procedure is repeated if the caller method is included in a try catch block. This process continues until a catch block handling the appropriate type of exception is found. If it does not find such a block then finally the program terminates.

* What are the different ways to handle checked exceptions? (exception)
A: There are two ways to handle exceptions, (1) By wrapping the desired code in a TRY block followed by a CATCH block to catch the exceptions. (2) List the desired exceptions in the THROWS clause of the method and let the caller of the method handle those exceptions.

* What is the basic difference between the 2 approaches to exception handling:
(1) TRY-CATCH block and (2) specifying the candidate exceptions in the THROWS clause? Which approach should be used when? (checked exception)
A: In the first approach as a programmer of the method, we are dealing with the exception. This is fine if you are in a best position to decide what should be done in case of an exception. Whereas if it is not the responsibility of the method to deal with it's own exceptions, then do not use this approach. In this case use the second approach. In the second approach we are forcing the caller of the method to catch the exceptions, that the method is likely to throw. This is often the approach library creators use. They list the exception in the throws clause and we must catch them. You will find the same approach throughout the java libraries we use.

* Can an exception be rethrown? (exception)
Yes, an exception can be rethrown.

* What is the relationship between a method's throws clause and the exceptions that can be thrown during the method's execution? (checked exception)
A method's throws clause must declare any checked exceptions that are not caught within the body of the method.

* How to Retrieve Warnings? (exception)
SQLWarning objects are a subclass of SQLException that deal with database connection warnings. Warnings do not stop the execution of an application, as exceptions happen they simply alert the user that something did not happen as planned. A warning can be reported on a Connection object, or to a Statement object (including PreparedStatement and CallableStatement objects), or a ResultSet object. Each of these classes has a getWarnings method, which must be invoked in order to see the first warning reported on the calling object.
SQLWarning warning = stmt.getWarnings();
-if (warning != null) {
--while (warning != null) {
--System.out.println("Message: " + warning.getMessage());
--System.out.println("SQLState: " + warning.getSQLState());
--System.out.print("Vendor error code: ");
--system.out.println(warning.getErrorCode());
--warning = warning.getNextWarning(); } }
(compile online)

* What should we do, if we want an object of our class to be thrown as an exception object? (checked exception)
A: The class should extend from Exception class. Or you can extend your class from some more precise exception type also.
Only objects that are instances of this class can be thrown by the throw statement or can be the argument type in a catch clause. throw MUST be declared in a try block.
(Ex:) public class Exp {
--public static void main(String[] args) { new Exp(); }
--public Exp() {
---try { int size=0; System.out.println("In TRY");
---cantBeZero(0); }
---catch (MyError t1) { System.out.println("In CATCH 1"); } }
--public void cantBeZero(int i) throws MyError { if (i == 0) { throw new MyError(); } } }
Below is the subclass of Throwable class.
class MyError extends Throwable { // it can extend either Exception or Error also.
--MyError() { System.out.println("In MyError"); } }
(compile online)

* What happens if a try-catch-finally statement does not have a catch clause to handle an exception that is thrown within the body of the try statement? (catch)
The exception propagates up to the next higher level try-catch statement (if any) or results in the program's termination.
The catch blocks catches the exceptions of try block and thus allows the program to continue its execution.
The finally block always executes when the try block exits. This ensures that the finally block is executed even if an unexpected exception occurs. When closing a file or otherwise recovering resources, place the code in a finally block to insure that resource is always recovered.

* Can try statements be nested? (catch)
Yes it can be. Nested try-catch statements are compiled very much like a try statement with multiple catch clauses:
--void nestedCatch() {
---try {
----try { tryItOut(); } // try within try
----catch (TestExc1 e) { handleExc1(e); }
---} catch (TestExc2 e) { handleExc2(e); }
--}
(compile online)

* How does a try statement determine which catch clause should be used to handle an exception? (try-catch)
When an exception is thrown within the body of a try statement, the catch clauses of the try statement are examined in the order in which they appear. The first catch clause that is capable of handling the exception is executed. The remaining catch clauses are ignored.
Each catch block is an exception handler and handles the type of exception indicated by its argument. Appropriate catch block will be called depending on the exception thrown. Checked exceptions can be thrown within the try statement.
try { }
catch (ExceptionType1 name1) { }
catch (ExceptionType2 name2) { }

* What classes of exceptions may be caught by a catch clause?
(try-catch)
A catch clause can catch any exception that may be assigned to the Throwable type. This includes the Error and Exception types. (checked and unchecked)
The Throwable class is the superclass of all errors and exceptions. Only objects that are instances of this class can be thrown by the throw statement or can be the argument type in a catch clause. ERROR and EXCEPTIONS classes are the subclasses of THROWABLE class.

* Is it necessary that each try block must be followed by a catch block? (try-catch)
A: It is not necessary that each try block must be followed by a catch block. It should be followed by either a catch block OR a finally block. And whatever exceptions are likely to be thrown should be declared in the throws clause of the method.

* If I write return at the end of the try block, will the finally block still execute? (
try-catch)
A: Yes even if we write return as the last statement in the try block and no exception occurs, the finally block will execute. The finally block will execute and then the control return.

* What is the difference between final, finally and finalize? (try-catch)
FINAL - keyword used to declare constants.
FINALLY - keyword handles exception.
FINALIZE - helps in garbage collection. (Examples)
Finalize method is called by the garbage collector on an object. The call System.gc() is effectively equivalent to the call: Runtime.getRuntime().gc()

* If I write System.exit (0); at the end of the try block, will the finally block still execute? (try-catch)
A: No in this case the finally block will not execute because when you say System.exit (0); the control immediately goes out of the program, and thus finally never executes. The call System.exit(n) is effectively equivalent to the call: Runtime.getRuntime().exit(n)

* Is it true that the code in a finally clause will never fail to execute? (try-catch)
Using System.exit(1); in try block will NOT allow finally code to execute.
System.exit(n) processes return zero(0) if there is NO error and any other value if there is an error. The parameter tells why the program is being terminated.

* What is the purpose of the finally clause of a try-catch-finally statement? (try-catch)
The finally clause is used to provide the capability to execute code no matter whether or not an exception is thrown or caught.


Read: Peace Home Download Music Prayers Life Is Beautiful

*Realizations. *Realizations In Real Life. *Amazing Constructive Vibrations. *Astrology. *Creating Leaders. *How ideas are concluded and decisions are materialized. *Relationships & Partnerships. *The path of victory. *An attempt for definition. *Speak with a heart. *These are contagious. *Happy kingdom. *MIRACLES. *Better to create one ! *Cast, God and religion ! *Things become inevitable ! *We are all looking for! *Phase of Life. *Destiny, Karma and God. *Struggle, perfection and Money. *Independence and Freedom. *Relationships and Happiness.

No comments:

About Me

My photo
Simple guy who believes in being Competitive rather than being Ambitious. Persistence to be constructive, without frustrations, is a good purpose of life.
.

* * * * * * * * * * * * *

MeetUp Message Board:
Read: Wishes-and-Prayers. *Issue-of-Illegal-Immigration-&-Happy-Kingdom. *The-New-York-Hillary-Rodham-Clinton *Cast-God-Religion! *Barack-Obama-Meetup *Duty- Responsibility-Law-and-Order *John-Edwards-One-America *Good-Wishes-Life-is-Meaningful-&-Beautiful *Dennis-Kucinich-Meetup *Let-there-be-peace! *Bill-Richardson-for-President-2008 *Logic-of-Good-and-Bad-convert-bad-to-good *MORAL-STORY-Elephants-held-by-small-ropes.
* * * * * * * * * * * * *

Realizations.
*Realizations-In-Real-Life-Please-be-gentle-and-humble. *Manoj-D-Kargudri. *Amazing-Constructive-Vibrations. *Astrology. *Creating-Leaders. *How-ideas-are-concluded-and-decisions-are-materialized. *“Relationships-in-Life”-“Partnerships-in-Life”. *The-path-of-victory-the-path-of-life-winning-in-looseing. *An-attempt-for-definition. *Speak-with-a-heart. *These-are-contagious. *Key-to-happy-kingdom. *MIRACLES. *Better-to-create-one! *Cast-God-and-Religion! *Manoj-Kargudri. *Things-become-inevitable! *We-are-all-looking-for! *Phase-of-Life. *Destiny-Karma-and-God. *Struggle-perfection-and-Money. *Independence-and-Freedom. *Relationships-and-Happiness.
* * * * * *

Quotes.
*Love-Compassion-Tolerance-Forgiveness-Courtesy. *Manoj-D-Kargudri. *True-to-Heart-going-back-to-Basics!
* * * * * *

Puzzles-Riddles-Think.
*River-Crossing-Puzzles-Brain-Teasers. *Manoj-Kargudri. *Perpetual-Motion-Gravity-and-Kinetics. *Illusions-Paradoxes-Perpetually-ascending-staircase. *Milk-man-with-no-measureing-jar. *Amazing-Horoscope-Mind-Reader. *Find-the-hidden-images-in-these-STEREOGRAMS. *Are-they-12-or-13? *What-would-U-do? *ABCD-Four-Digit-Number. *10-Digit-Number. *4-QUESTIONS...GOOD-LUCK! *Think-Wise.
* * * * * *

Prayers.
*God-gave-us-everything-we-ever-needed. *Good-Wishes. *Manoj-Kargudri. *Love-Compassion-Tolerance-Forgiveness-Courtesy. *Interview-With-God! *He-is-the-one! *Candle-of-Hope! *Let-there-be-Peace! *Manoj-Kargudri.
* * * * * *

Perceptions.
*Issue-of-Illegal-Immigration. *To-which-religion-does-this-universe-belongs-to? *Law-and-order-helps-to-maintain-justice. *Implementing-regulations-by-justice. *Putting-our-sincere-efforts. *Religion-and-cast. *Impact-of-reservation-based-on-religion-and-cast. *Free-and-Fare-Education-system-Electoral-system.
* * * * * *

Stories.
*The-Monkey’s-Justice-for-two-cats. *The-Blind-Men-And-The-Elephant. *Manoj-Kargudri. *Two-rich-merchants-and-a-thief. *Healing-is-contagious. *Two-saints-in-a-market-place. *A-Terrible-Fight-Between-Two-Wolves. *Hen-that-laid-golden-eggs. *Healing-forgiveness-and-affection. *Elephants-held-by-small-ropes. *Story-of-Punyakoti-the-strength-of-truth. *What-is-the-reason? *Reply-Depends-on-the-Question. *Critical-mass-Experiment. *The-Brahman's-Wife-and-the-Mongoose. *The-Boy-Who-Cried-Wolf. *Difference-between-Heaven-and-Hell! *Freedom-and-Prison! *It's-in-Your-Eyes!
* * * * * *

Jokes.
*Please-listen-to-me. *The-Silent-Treatment! *Surgeon-Vs-Mechanic. *Manoj-Kargudri. *God's-doing-a-lot-better-job-lately.
* * * * * *

The-Bitter-Truth.
*Duty-Responsibility-Law-and-Order. *"Happy-Kingdom"-proudly-proclaimed-as-"Paradise". *Trying-to-learn-something-for-the-first-time. *Time-is-the-only-solution. *For-every-Action-there-is-an-Equal-and-Opposite-Reaction. *Logic-of-Good-and-Bad. *Manoj-Kargudri. *Duties-Responsibilities-verses-Luxuries-and-Pleasures. *Beggars!
* * * * * *

Life-is-Beautiful.
*Creating-successful-constructive-personality. *Why-God-Gave-Us-Friends? *Way-to-Paradise. *Creating-the-Beautiful-World. *Doing-the-job-of-goodness. *Life-is-Meaningful. *Manoj-Kargudri. *The-A-to-Z-for-Goodness. *Life-is-full-of-Tests. *History-Proves-That. *Love-in-different-forms. *True-to-the-heart.
* * * * * *

Prayers-To-Worship.
*Please-do-not-leave-me-ever. *Let’s-make-it-happen. *Faith-in-Patience. *The-only-one-I've-got. *Someone-somewhere. *How-I-Love-You. *Will-You? *Successful-Life. *Manoj-Kargudri. *Please-say-something. *Way-to-Paradise. *My-Everything.
* * * * * *

Friendship.
*Life-Still-Has-A-Meaning. *Heavenly-Garden. *GOD-SPEAK-TO-ME! *Why-God-Made-Friends? *I-asked-the-Lord! *A-Best-Friend! *Why-GOD-Gave-Us-Friends? *Portrait-of-a-Friend. *Friends-till-the-end. *Some-Assorted! *Forever-Friends. *What-is-a-friend!
* * * * * *

Winning-Publications.
*Significance-of-Nava-ratna (Nava-Graha). *Amazing-Constructive-Vibrations. *Manoj-Kargudri. *The-piercing-of-the-ears (karnavedha) . *Nature-Cure. *Steps-to-improve-work-place. *Steps-Involved-In-Relationships. *Some-of-the-aspects-which-materialize-the-relationships.
* * * * * *

Music-Download.
*Bhakti-Songs. *Manoj-Kargudri. *English-Songs. *Gazal-Bhajan-Hindi. *Hindi-Life. *Hindi-Love. *Hindi-Old-Kishore. *Hindi-Old-Mukesh. *Hindi-Old-Songs. *Hindi-Rock. *Hindi-Pops. *Instrumental. *Vocal-Ragas.
* * * * * *

Technology.
*READ: Why-JAVA *Manoj-Kargudri. *Start-Learning-JAVA *Why-Java's-Hot *Start-Using-LINUX *Java-Q-&-A *Question *Java-SE-6-Doc *Struts-Doc *Java-Tutorial-Index *Java-Certification *Tech. *Struts. *Manoj-Kargudri. *Servlet. *JSP. *EJB. *JNDI-JMS *SQL. *JDBC. *CORE-JAVA: *OOP *CLASS. *Manoj-Kargudri. *ABSTRACT *EXCEPTIONS *THREADS *UTILS *PACKAGES *JVM *CASTING *NETWORKING *RMI-XML
* * * * * *

MUSIC-DOWNLOAD.
*Hindi-Vocal-Raga. *Hindi-Remix-Rock. *Hindi-Old-Songs. *Hindi-Mukesh-Mohd-Rafi-Songs. *Hindi-LOVE-Songs. *Hindi-Remix-LIFE. *English-Rock-Songs. *Kannada-Janapada-Geete. *Kannada-Film-Songs. *Kannada-Devotional-Songs. *Instrumental-Music. *Manoj-Kargudri. *Hindi-Pop-Songs. *Hindi-Bhakti-Songs. *Hindi-Kishore-Kumar-SAD. *Hindi-Kishore-Kumar-JOY. *Hindi-R-D-Burman. *Hindi-Gazals. *English-Soft-Songs.
* * * * * *

Add to Google