Results 1 to 3 of 3
  1. #1
    zoe
    zoe is offline Member
    Join Date
    Jul 2007
    Posts
    40
    Rep Power
    0

    Default 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

  2. #2
    sandyroks is offline Member
    Join Date
    Jul 2007
    Posts
    1
    Rep Power
    0

    Default

    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");
    }
    }

  3. #3
    Seemster is offline Member
    Join Date
    Jul 2007
    Posts
    55
    Rep Power
    0

    Default

    If a call can throw an exception you'll get a compile error if it's not wrapped around a try...catch block.

Similar Threads

  1. try catch!?
    By Joe2003 in forum Advanced Java
    Replies: 2
    Last Post: 01-28-2008, 07:51 PM
  2. Try Catch issue
    By curtis_fraser in forum Advanced Java
    Replies: 2
    Last Post: 12-13-2007, 11:04 PM
  3. Try Catch
    By Renegade85 in forum New To Java
    Replies: 4
    Last Post: 12-03-2007, 04:10 PM
  4. when to use try...catch
    By javaplus in forum New To Java
    Replies: 2
    Last Post: 11-18-2007, 08:52 PM
  5. try...catch block
    By javaplus in forum New To Java
    Replies: 3
    Last Post: 11-06-2007, 07:53 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
  •