Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-24-2007, 05:03 PM
zoe zoe is offline
Member
 
Join Date: Jul 2007
Posts: 40
zoe is on a distinguished road
Use try and catch
Hi, I want to know when should I use try and catch statement.
I have no idea of knowing whether some code has the ability to throw exceptions.
So how do I decide when to use them and when not to use them?
Thanks
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-24-2007, 06:00 PM
Member
 
Join Date: Jul 2007
Posts: 1
sandyroks is on a distinguished road
hi, A java exception is an object that describes an exceptional condition that has occured in a piece of code.When exceptional condition arises, an object representing that exception is created and 'thrown' in the method that caused the error. and the exception 'caught' and processed.

For example we go throw divide-by-zero error. when the java run-time system detects the attampt to dicide by zero, then it constructs a exception object and then throws this exception. it stops the excution bcoz exception is raised so to continue the excution we caught by an exception handler as follows.
class A{
public static void main(string s[]){
int a=0;
int b=10;
try{
int c=b/a;
System.out.println("We'll not be printed');
}catch(ArithmeticException e){
System.out.println("Divide by zero");
}
System.out.println("after the try-catch block");
}
}
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 07-25-2007, 09:50 PM
Member
 
Join Date: Jul 2007
Posts: 55
Seemster is on a distinguished road
If a call can throw an exception you'll get a compile error if it's not wrapped around a try...catch block.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
try catch!? Joe2003 Advanced Java 2 01-28-2008 09:51 PM
Try Catch issue curtis_fraser Advanced Java 2 12-14-2007 01:04 AM
Try Catch Renegade85 New To Java 4 12-03-2007 06:10 PM
when to use try...catch javaplus New To Java 2 11-18-2007 10:52 PM
try...catch block javaplus New To Java 3 11-06-2007 09:53 PM


All times are GMT +3. The time now is 11:26 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org