Win a copy of Rust Web Development this week in the Other Languages 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Can some one explain the answer in detail

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Which can appropriately be thrown by a programmer using Java SE technology to create
a desktop application
A. ClassCastException
B. NullPointerException
C. NoClassDefFoundError
D. NumberFormatException
E. ArrayIndexOutOfBoundsException
Answer: D
Ref : Sun 310-065: Practice Exam
 
Ranch Hand
Posts: 331
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The rest are JVM thrown exceptions. You'd never programatically throw a ArrayIndexOutOfBoundException etc.

But NumberFormatException is a programatically thrown exception. For example,
if the input number is not in the format that you expect you can throw the exception as such:



HTH,
Vishwa
 
Ranch Hand
Posts: 155
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
NumberFormatException is thrown to indicate that the application has attempted to convert a string to one of the numeric types, but that the string does not have the appropriate format.

Following code throws NumberFormatException at line 3:


This is because String "javaranch" is not in the appropriate number format (like say "75")
 
Trivikram Kamat
Ranch Hand
Posts: 155
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Actually we can throw any Exception in the above code (there's no restriction)
The line3 can be replaced by:
  • throw new ClassCastException();
  • throw new NullPointerException();
  • throw new NoClassDefFoundError();
  • throw new ArrayIndexOutOfBoundsException();
  •  
    Ranch Hand
    Posts: 46
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator


    Actually we can throw any Exception in the above code (there's no restriction)



    Although there is no restriction on throwing exceptions but a programmer would rather not go into throwing exceptions that extend the Error class and the Runtime Exception Class, collectively known as unchecked exceptions.

    As one(Error) indicates serious problems in the code, that are mostly detected by the JVM(as such information is only available to the JVM), and are thrown only by the JVM, e.g NoClassDefFoundError and should not be caught.

    While the other(RuntimeException) indicates problems relating to the incorrect usage of a Java construct(Array,Division By Zero, Casting), which are detected at runtime by the JVM anyway(so why do the extra work ) and should not be caught as that would forgo any advantage got by detecting such exceptions as ClassCastException, NullPointerException, ArrayIndexOutOfBoundsException, etc.


     
    Rahul Saple
    Ranch Hand
    Posts: 46
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Although I did give an answer to what I thought it should be, I still think the correct answer is a bit questionable as NumberFormatException is also a RuntimeException.

    And it is just as appropriate and as possible to throw a NullPointerException as it is to throw a NumberFormatException.

    Say, checking whether a reference has a null value, and throwing a NullPointerException if such a reference is made use of to refer to any member of its associated class.

    Same can be said about the ArrayIndexOutOfBoundsException which can be thrown if the index value is manually checked and is known to exceed the bounds.

    All these are programmatically thrown exceptions and usually thrown by the JVM, saving the programmer some time and effort.
     
    Greenhorn
    Posts: 2
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Question is : Which can appropriately be thrown by a programmer using Java SE technology to create a desktop application?

    Solution can be obtained by analyzing the existing classes.

    1. NoClassDefFoundError
    Any errors which are subclasses of Error should not be thrown programmatically. They are handled by JVM and Exceptions can only be handled by a programmer .

    2. NullPointerException
    Ex: consider the code
    ArrayList<Integer> a=null;
    a.add(1); // throws NullPointerException

    If we analyze the code present in ArrayList.add, we don't find the method throwing NullPointerException. Similarly we don't find ClassCastException and ArrayIndexOutOfBoundsException exceptions are also not thrown explicitly in the existing Oracle JDK classes. Reason is JVM takes care of throwing the appropriate exception in such scenarios.

    3. ArrayIndexOutOfBoundsException
    String[] s=new String[1];
    s[2]="abc"; // throws ArrayIndexOutOfBoundsException, But there is no declaration in String class


    4.ClassCastException
    ArrayList<Integer> a=new ArrayList<Integer>();
    a.add(1);
    List<Integer> list=a;
    LinkedList<Integer> l=(LinkedList<Integer>)list; // throws ClassCastException, But no declaration in LinkedList class


    5. NumberFormatException
    double val = Double.parseDouble("abc");
    Double.parseDouble explicitly throw NumberFormatException if the format of the string is unexpected.

    //Following is the syntax of Double.parseDouble method
    public static double parseDouble(String s) throws NumberFormatException {
    return FloatingDecimal.parseDouble(s);
    }


    So the answer is NumberFormatException.
     
    Screaming fools! It's nothing more than a tiny ad:
    Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
    https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    reply
      Bookmark Topic Watch Topic
    • New Topic