Results 1 to 6 of 6
  1. #1
    ShadowWalker is offline Senior Member
    Join Date
    Mar 2013
    Location
    Greece
    Posts
    112
    Rep Power
    0

    Default Question About Exceptions

    I would like to know why we have so many Exceptions...

    for example i read a book right now and explaine some staff about Exceptions and RuntimeExceptions
    but i am not sure why all this are useful at structure of a program...

    let's make an example.. below i will write 2 codes :


    Java Code:
    public void methodNo1(int x){
    	try{
    		//some code
    	}catch(Exception e){
    		e.printStackTrace()
    		// or your own System.out.println("with infos")
    	}
    }
    
    public void methodNo1(int x){
    	try{
    		// some code
    	}catch(NullPointerException e){
    		// some code
    	}catch(onotherException e){
    		// some code
    	}
    	...
    }
    my point is why we need (or it's more efficient) to check the specific exceptions when we can just do as the first code and check for "Exception e" and then to put a message like "incorrect input we need bla bla bla"

    thx in advance and sorry if the question is not important

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,354
    Blog Entries
    7
    Rep Power
    20

    Default Re: Question About Exceptions

    If all that can be thrown were an Exception, the exception catching block has to figure out what actually went wrong in order to fix the correct stuff; and please note that your catching code doesn't have to catch all exceptions, i.e. it can let certain exceptions 'pass through' if it doesn't know how to handle the exception.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    zFollette is offline ▼ dafuq did I do?
    Join Date
    Dec 2013
    Posts
    203
    Rep Power
    0

    Default Re: Question About Exceptions

    Quote Originally Posted by JosAH View Post
    If all that can be thrown were an Exception, the exception catching block has to figure out what actually went wrong in order to fix the correct stuff; and please note that your catching code doesn't have to catch all exceptions, i.e. it can let certain exceptions 'pass through' if it doesn't know how to handle the exception.

    kind regards,

    Jos
    He is asking WHY it's more efficient. I think OP knows that it is more efficient and he should use multiple exceptions. I don't know too much about how this works but I'll take a swing at it. Say you catch 'Exception' and your code throws an 'XPathException', your try/catch block now has to search through all of the exceptions:

    AclNotFoundException, ActivationException, AlreadyBoundException, ApplicationException, AWTException, BackingStoreException, BadAttributeValueExpException, BadBinaryOpValueExpException, BadLocationException, BadStringOperationException, BrokenBarrierException, CertificateException, CloneNotSupportedException, DataFormatException, DatatypeConfigurationException, DestroyFailedException, ExecutionException, ExpandVetoException, FontFormatException, GeneralSecurityException, GSSException, IllegalClassFormatException, InterruptedException, IntrospectionException, InvalidApplicationException, InvalidMidiDataException, InvalidPreferencesFormatException, InvalidTargetObjectTypeException, IOException, JAXBException, JMException, KeySelectorException, LastOwnerException, LineUnavailableException, MarshalException, MidiUnavailableException, MimeTypeParseException, MimeTypeParseException, NamingException, NoninvertibleTransformException, NotBoundException, NotOwnerException, ParseException, ParserConfigurationException, PrinterException, PrintException, PrivilegedActionException, PropertyVetoException, ReflectiveOperationException, RefreshFailedException, RemarshalException, RuntimeException, SAXException, ScriptException, ServerNotActiveException, SOAPException, SQLException, TimeoutException, TooManyListenersException, TransformerException, TransformException, UnmodifiableClassException, UnsupportedAudioFileException, UnsupportedCallbackException, UnsupportedFlavorException, UnsupportedLookAndFeelException, URIReferenceException, URISyntaxException, UserException, XAException, XMLParseException, XMLSignatureException, XMLStreamException, XPathException

    (List taken from the Exception class in the javadocs)

    Until it finds the specific exception that needs to be caught.
    1000011 1100001 1101110 100000 1111001 1101111 1110101 100000 1110010 1100101 1100001 1100100 100000 1000010 1101001 1101110 1100001 1110010 1111001 111111

  4. #4
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: Question About Exceptions

    The clearest explanation I have ever seen of when and why to use different types of exceptions is in this book: Effective Java (2nd Edition): Joshua Bloch: 9780321356680: Amazon.com: Books
    Get in the habit of using standard Java naming conventions!

  5. #5
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,392
    Rep Power
    5

    Default Re: Question About Exceptions

    That is a great book! It is really a best practices book which everyone should have in their library.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  6. #6
    ShadowWalker is offline Senior Member
    Join Date
    Mar 2013
    Location
    Greece
    Posts
    112
    Rep Power
    0

    Default Re: Question About Exceptions

    Thx all for the informations..i am trying to understand how to make my code more efficient and more clear and first i should learn why some staff are useful or not.. about the book "effective java" hmmm maybe i should make an order ;)

Similar Threads

  1. Exceptions Question
    By Clown in forum New To Java
    Replies: 2
    Last Post: 02-19-2013, 09:42 AM
  2. Catching Exceptions (quick question)
    By Danieldcc in forum New To Java
    Replies: 3
    Last Post: 02-19-2012, 11:40 PM
  3. Exceptions
    By Nerijus in forum New To Java
    Replies: 8
    Last Post: 05-18-2010, 01:44 PM
  4. question regarding exceptions..
    By SCS17 in forum New To Java
    Replies: 3
    Last Post: 11-17-2007, 09:31 AM
  5. Question on Exceptions
    By yelllow4u in forum New To Java
    Replies: 6
    Last Post: 07-27-2007, 01:41 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •