|
-
Nov 17th, 2005, 02:59 AM
#1
Thread Starter
Hyperactive Member
try/catch Problem
I have to give kudos to my Java book but this is something that wasn't explained really well and now I'm on an exercise that requires me to use the try/catch statements and I'm totally lost.
Can someone explain what the try/catch statement is and how to use it in as much detail as possible? Any help is most graciously appreciated.
Last edited by GamerMax5; Nov 17th, 2005 at 04:19 PM.
-
Nov 17th, 2005, 06:08 AM
#2
New Member
Re: try/catch Problem
Try/Catch is a flow control construct used to deal with exceptions. Think of an exception like an emergency inside your code. It is something you the programmer must prepare for and must deal with should it occur but they are not necessarily fatal.
When something out of the ordinary occurs in a procedure an exception is typically thrown - an exception is a special object, which can be thrown. The act of throwing an exception immediately stops execution of the current procedure. The exception then propagates up through the call stack until it reaches the Java runtime where it will be displayed as an uncaught exception. In this case, the program will terminate because and uncaught exception, is an error and it is fatal.
Enter here, try/catch. The try/catch construct allows you to catch an exception and deal with it before it reaches the JRE; simply enclose the code you want to try and execute inside a try block. Should an exception be thrown anywhere in that block, the program will immediately jump to the catch block for that particular exception. Remember exceptions are object and they are typically generalized into different types of exception - all exceptions however are derived from the Exception class.
Consider the following code:
Code:
public static int ask(String msg, String errorMsg) throws IOException
{
int ret;
while(true) {
System.out.print(msg);
try {
return readInt();
} catch (NumberFormatException e) {
System.out.println("Enter a proper integer please.");
}
}
}
public static int readInt() throws IOException
{
return new Integer(kb.readLine()).intValue();
}
Here we have a procedure which prints a message and attempts to read an integer from the console. The following points should be noted:
- The readLine() function of the kb object will throw an IOException if there is an error reading from the console. This is an exception which must be prepared for hence we must either enclose the offending line in a try block or declare that the function can throw an IOException in the throws clause of the function definition.
- The ask() function uses a try block to catch the a NumberFormatException. The number format exception is a RuntimeException they occur during normal program flow so we cannot declare them to be thrown in the function definition.
- The actual source of the NumberFormatException, should the user type a non integer value is the constructor for the Integer class. When the exception is thrown, it propagates from the readInt() function, up to the ask() function and into the catch block where it is handled.
- Once an exception has been caught, it no longer propagates up the call stack and execution continues at the point immediately after the try/catch block.
In effect, exception handling is a very advanced form of error handling. It gives the programmer much finer control in dealing with all kinds of errors.
I am the real Santa Clause 
-
Nov 17th, 2005, 06:42 AM
#3
Frenzied Member
Re: try/catch Problem
I would say handle the exception unless you really want to pass the problem up.
Code:
try
{
return readInt();
}
catch (NumberFormatException nfe)
{
System.out.println("Enter a proper integer please.");
}
catch(IOException ioe)
{
ioe.printStackTrace();
}
But that's just my personal preference.
-
Nov 17th, 2005, 03:52 PM
#4
Thread Starter
Hyperactive Member
Re: try/catch Problem
Thanks a lot. It makes a little more sense now.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|