why do you need a variable in catch exception and how do you use each it the same as the detailed exception? TemperatureException and e TooColdException and e for example: Code: try { cust.drinkCoffee(cup); System.out.println("Coffee is just right."); } catch (TemperatureException e) { // This catches TooColdException, TooHotException, // as well as TemperatureException. System.out.println("Coffee is too cold or too hot."); // Deal with an irate customer... } // THIS WON'T COMPILE, BECAUSE THIS catch clause // WILL NEVER BE REACHED. catch (TooColdException e) { System.out.println("Coffee is too cold."); }
Hi 2007160935 Whenever an exception arises it is handled by the JVM(completely or partially).Since the JVM is taking the pain to throw the error there must be something to store the error in and it is known as an Exception object.This is the why.How?override toString().
Hey, here's the link just check this " http://pages.cs.wisc.edu/~hasti/cs368/JavaTutorial/NOTES/Exceptions.html " you will get the relevant data in this . Good Luck
When an error is detected, an exception is thrown. That is, the code that caused the error stops executing immediately, and control is transferred to the catch clause for that exception of the first enclosing try block that has such a clause. The try block might be in the current function , or it might be in some function that called the current function. If no currently active function is prepared to catch the exception, an error message is printed and the program stops.
A Java exception is an object that describes an exceptional (that is, error) condition that has occurred in a piece of code.
Exceptions are the standard route in Java to demonstrate to a considering strategy that an irregular condition has happened. This article is a buddy piece to the current month's Design Techniques portion, which talks about how to utilize special cases properly in your projects and outlines. Look to this sidekick article for an instructional exercise on the stray pieces of what special cases are and how they function in the Java dialect and virtual machine. At the point when a strategy experiences an unusual condition (a special case condition) that it can't deal with itself, it might toss an exemption. Tossing an exemption resembles tossing a signaling, blazing red ball to demonstrate there is an issue that can't be taken care of where it happened. Some place, you trust, this ball will be gotten and the issue will be managed. Exemptions are gotten by handlers situated along the string's technique conjuring stack. On the off chance that the calling technique isn't set up to get the exemption, it tosses the special case up to its calling strategy, et cetera. On the off chance that one of the strings of your program tosses a special case that isn't gotten by any strategy along the technique summon stack, that string will lapse. When you program in Java, you should position catchers (the exemption handlers) deliberately, so your program will catch and handle all special cases from which you need your program to recoup. Special case classes In Java, special cases are objects. When you toss an exemption, you toss a question. You can't toss only any question as a special case, anyway - just those items whose classes plummet from Throwable. Throwable fills in as the base class for a whole group of classes, pronounced in java.lang, that your program can instantiate and toss. A little piece of this family is appeared in Figure 1. As should be obvious in Figure 1, Throwable has two direct subclasses, Exception and Error. Special cases (individuals from the Exception family) are tossed to flag unusual conditions that can frequently be dealt with by some catcher, however it's conceivable they may not be gotten and hence could result in a dead string. Mistakes (individuals from the Error family) are typically tossed for more significant issues, for example, OutOfMemoryError, that may not be so natural to deal with. When all is said in done, code you compose should toss just exemptions, not mistakes. Mistakes are typically tossed by the strategies for the Java API, or by the Java virtual machine itself. Figure 1. An incomplete perspective of the Throwable family Notwithstanding tossing articles whose classes are announced in java.lang, you can toss objects of your own outline. To make your very own class of throwable articles, you require just announce it as a subclass of some individual from the Throwable family. When all is said in done, nonetheless, the throwable classes you characterize ought to broaden class Exception. They ought to be "special cases." The thinking behind this lead will be clarified later in this article. Regardless of whether you utilize a current special case class from java.lang or make one of your own relies on the circumstance. Now and again, a class from java.lang will do fine and dandy. For instance, on the off chance that one of your techniques is summoned with an invalid contention, you could toss IllegalArgumentException, a subclass of RuntimeException in java.lang.
When an exception is thrown and the exception object gets a variable assigned to it, that object variable is only available within the "catch" section and is destroyed as soon as the catch completes. (and more importantly). You can't know where in the try block the exception was thrown. It may have been before your variable was declared. Therefore it is impossible to say what variables will be available for the catch/finally clause.