Results 1 to 5 of 5
  1. #1
    wulfgarpro is offline Member
    Join Date
    Sep 2010
    Posts
    6
    Rep Power
    0

    Default Basic exception question

    Hi,

    Please excuse my lack of understanding. This is a question I need to ask.

    Most of the applications I currently build are very basic. They never really exceed the core thread. Exceptions are generally of checked type and are caught either in the class that throws the exception or a class higher up the stack. Generally I output a message within my caught exception.

    If the purpose of exceptions and exception handling is to ensure an application can handle an exceptional event and continue to run.. what should I do within my caught exceptions? How do I continue execution? Is this only really possible when using multiple threads of execution?

    Kind Regards,

    wulfgarpro.

  2. #2
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    If you catch exceptions, you know what went wrong and can adjust the program flow accordingly, something similar to this:
    Java Code:
    int age;
    public void getAge() {
      Scanner sc = new Scanner(System.in);
      System.out.println("Input age");
      try {
        age = sc.nextInt();
      }
      catch(InputMismatchException ime) {
        System.out.println("Please enter a number");
        getAge();
      }
    }
    If you tryed to get the input without the try catch block, if someone was to input a string instead of an integer, the program would quit, this way, you inform the user what went wrong and try again.
    Ever seen a dog chase its tail? Now that's an infinite loop.

  3. #3
    wulfgarpro is offline Member
    Join Date
    Sep 2010
    Posts
    6
    Rep Power
    0

    Default

    Ok.

    So in an application that utilises only the core thread, recalling the method from the catch (form of recursion) is the preferred way to continue execution? If this proposed application implemented multiple threads of execution, each thread could handle exceptions that may occur in a similar fashion? This would ensure persistence of execution while the core thread happily processes its own functionality.

    Just clarifying. If I am on the wrong track please explain!

    wulfgarpro.
    Last edited by wulfgarpro; 09-15-2010 at 03:08 AM.

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

    Default

    That was just an example of what could be done.

    What happens with an exception is entirely down to what you want to happen, which involves some analysis. Can you recover and still process things in a meaningful way? If not, then presumably you should be failing gracefully and informing the user.

  5. #5
    wulfgarpro is offline Member
    Join Date
    Sep 2010
    Posts
    6
    Rep Power
    0

    Default

    Thanks Tolls. Your response is what I was looking for. I guess it also has to do with the environment an application runs within - whether thats desktop, cli, web ..

    wulfgarpro.

Similar Threads

  1. Basic JSP question..
    By casid in forum JavaServer Pages (JSP) and JSTL
    Replies: 4
    Last Post: 12-28-2009, 06:51 PM
  2. Basic question on OOP.
    By madthinker in forum New To Java
    Replies: 7
    Last Post: 01-26-2009, 07:09 PM
  3. Basic array question
    By jigglywiggly in forum New To Java
    Replies: 12
    Last Post: 01-09-2009, 04:44 PM
  4. Basic question about EJB
    By javaplus in forum Enterprise JavaBeans (EJB)
    Replies: 2
    Last Post: 07-15-2008, 05:44 PM
  5. Very basic question
    By gvi in forum New To Java
    Replies: 2
    Last Post: 10-30-2007, 06:30 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
  •