Results 1 to 5 of 5
Thread: Basic exception question
- 09-15-2010, 12:19 AM #1
Member
- Join Date
- Sep 2010
- Posts
- 6
- Rep Power
- 0
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.
- 09-15-2010, 02:59 AM #2
Senior Member
- Join Date
- Feb 2010
- Location
- Ljubljana, Slovenia
- Posts
- 470
- Rep Power
- 4
If you catch exceptions, you know what went wrong and can adjust the program flow accordingly, something similar to this:
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.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(); } }Ever seen a dog chase its tail? Now that's an infinite loop.
- 09-15-2010, 03:04 AM #3
Member
- Join Date
- Sep 2010
- Posts
- 6
- Rep Power
- 0
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.
- 09-15-2010, 08:56 AM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
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.
- 09-15-2010, 09:49 AM #5
Member
- Join Date
- Sep 2010
- Posts
- 6
- Rep Power
- 0
Similar Threads
-
Basic JSP question..
By casid in forum JavaServer Pages (JSP) and JSTLReplies: 4Last Post: 12-28-2009, 06:51 PM -
Basic question on OOP.
By madthinker in forum New To JavaReplies: 7Last Post: 01-26-2009, 07:09 PM -
Basic array question
By jigglywiggly in forum New To JavaReplies: 12Last Post: 01-09-2009, 04:44 PM -
Basic question about EJB
By javaplus in forum Enterprise JavaBeans (EJB)Replies: 2Last Post: 07-15-2008, 05:44 PM -
Very basic question
By gvi in forum New To JavaReplies: 2Last Post: 10-30-2007, 06:30 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks