1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

variable exception in java

Discussion in 'Java' started by 2007160935, Oct 23, 2012.

  1. 2007160935

    2007160935 New Member

    Joined:
    Mar 6, 2009
    Messages:
    38
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    ..
    Location:
    ..
    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.");
            }
    
     
    Last edited by a moderator: Dec 22, 2017
  2. state

    state New Member

    Joined:
    Sep 14, 2011
    Messages:
    30
    Likes Received:
    0
    Trophy Points:
    0
    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().
     
  3. alia123

    alia123 New Member

    Joined:
    Jan 8, 2016
    Messages:
    65
    Likes Received:
    5
    Trophy Points:
    0
  4. priya456

    priya456 New Member

    Joined:
    Jul 7, 2017
    Messages:
    26
    Likes Received:
    2
    Trophy Points:
    3
    Gender:
    Female
    • 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.
     
    shabbir likes this.
  5. Louis Zorbas

    Louis Zorbas New Member

    Joined:
    Aug 26, 2016
    Messages:
    15
    Likes Received:
    1
    Trophy Points:
    3
    Gender:
    Male
    A Java exception is an object that describes an exceptional (that is, error) condition that has occurred in a piece of code.
     
    Mr IAN likes this.
  6. Mr IAN

    Mr IAN New Member

    Joined:
    Jan 2, 2018
    Messages:
    15
    Likes Received:
    0
    Trophy Points:
    1
    Gender:
    Male
    Very well described sir.
     
  7. meenal deshpande

    meenal deshpande New Member

    Joined:
    Oct 11, 2018
    Messages:
    20
    Likes Received:
    2
    Trophy Points:
    3
    Gender:
    Female
    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.
     
    Last edited: Nov 29, 2018
    shabbir likes this.
  8. sayalipatil

    sayalipatil New Member

    Joined:
    Dec 18, 2018
    Messages:
    9
    Likes Received:
    0
    Trophy Points:
    1
    Gender:
    Female
    Home Page:
    1. 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.

    2. (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.
     

Share This Page