Results 1 to 3 of 3
  1. #1
    gnarkill10 is offline Member
    Join Date
    Aug 2012
    Location
    Tamworth, Australia
    Posts
    42
    Rep Power
    0

    Default Question about exception handling - Help needed

    Hi,

    Could someone please breifly explain to me how I can handle exceptions from multiple classes, I cant find anything on the Internet that uses multiple classes.

    This is my catch statement to handle the exception within my Main program (below), however I want to handle an exception from another class. I understand I could use the .getMessage, but I have three classes and Im thinking this method will only work with one?

    Could someone please explain this to me?


    Catch within my Main program:
    Java Code:
    catch (Exception e) 
    {
      System.err.println("Error: This is the Exception");
    }

    Thanks,
    Ben

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    5

    Default Re: Question about exception handling - Help needed

    I'm not sure what exactly do you want to do and what`s your question
    You can catch several exceptions and you can treat them differently
    Java Code:
    catch ( SQLException e )
    {
    ...
    }
    catch ( IOException e )
    {
    ....
    }
    In some cases, you want to do the same (e.g. logging the exception) in both cases, since Java 7 there is a multiple catch
    Java Code:
    catch ( SQLException | IOException e )
    {
    ....
    }
    If this doesn`t answer your question, maybe this helps: Lesson: Exceptions (The Java™ Tutorials > Essential Classes)

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,006
    Rep Power
    20

    Default Re: Question about exception handling - Help needed

    Also, you want to printStackTrace() for the exception otherwise you won't have a clue where it was thrown from.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Exception handling question
    By BenH in forum Advanced Java
    Replies: 1
    Last Post: 05-21-2012, 11:13 PM
  2. Question about Exception handling
    By bigsonny in forum New To Java
    Replies: 26
    Last Post: 08-05-2011, 12:28 AM
  3. Exception Handling
    By link6790 in forum New To Java
    Replies: 16
    Last Post: 05-19-2011, 06:57 PM
  4. Handling long strings - Advice needed
    By Codercode in forum Forum Lobby
    Replies: 0
    Last Post: 01-02-2011, 12:43 AM
  5. Handling Exception - elementary question
    By kracer in forum New To Java
    Replies: 2
    Last Post: 05-06-2010, 11:47 PM

Tags for this Thread

Posting Permissions

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