Results 1 to 4 of 4

Thread: Explain Try Catch Block in java

  1. #1
    Join Date
    Nov 2009
    Posts
    446

    Explain Try Catch Block in java

    Hi,
    I am new to java, not really a fresher. I have done I/O programs in java. Now I any trying to learn the try and catch block in java. Can any one explain me the concept of try and catch block with some examples. It will be highly appreciated if you help me. Thanks in advance. Waiting for the reply.

  2. #2
    Join Date
    Apr 2008
    Posts
    1,948

    Re: Explain Try Catch Block in java

    Hi,
    The try/catch statement encloses some code and is used to handle errors and exceptions that might occur in that code. The general syntax of an try and catch block is as follows:
    Code:
      try {
            body-code
        } catch (exception-classname variable-name) {
            handler-code
        }
    For more particle example see the code below.
    Code:
     String fn = "/nosuchdir/myfn";
        try {
            // Create the file
            new File(fn).createNewFile();
        } catch (IOException e) {
            // Print out the exception that occurred
            System.out.println("Unable to create "+fn+": "+e.getMessage());
        }
        // Execution continues here after the IOException handler is executed
    This may help you understand the basis of try and catch block

  3. #3
    Join Date
    May 2008
    Posts
    2,297

    Re: Explain Try Catch Block in java

    Hi
    The above posted example is perfect to understand. Correctly explained with the syntax. Thanks for posting. I am posting an example please go through that, may be it will help you.
    Code:
    try{}
    catch(IOException e){}
    catch(Exception e){}
    //now IO exceptions will be caught by the first catch block and all other exceptions handled by the superclass Exception.
    //I will now use a runnable example trying to output 1 divided by 0.
    
    public class Exceptions{
    public static void main(String args[]){
    try{
    System.out.println(1/0);
    }
    catch(java.lang.ArithmeticException e){
    System.out.println(”The operation is not possible.”);
    /*Note I used ArithmeticException here, but i could have just as easily used Exception */
    }
    }
    }

  4. #4
    Join Date
    Feb 2008
    Posts
    1,852

    Re: Explain Try Catch Block in java

    Hi
    To increase you knowledge to your database I am posting another example. Just go through the example. Best of Luck for further programs in java.
    Code:
    public class exceptions{
        public static void main(String Args[]){
            int[] ar = new int[3];
            try{
                for(int i=0;i<4;++i){
                    ar[i] = i;
                }
                System.out.println(ar);
            }
            catch(ArrayIndexOutOfBoundsException e){
                //printed just to inform that we have entered the catch block
                System.out.println("Oops, we went to far, better go back to 0!");
            }
        }
    }

Similar Threads

  1. Can we have multiple try and Catch block in our program ?
    By Solaris in forum Software Development
    Replies: 4
    Last Post: 29-01-2011, 11:52 PM
  2. How to catch an Exception in Java?
    By SKREECH in forum Software Development
    Replies: 4
    Last Post: 18-02-2010, 04:50 AM
  3. How to prevent from duplicating catch blocks in java?
    By Kasper in forum Software Development
    Replies: 5
    Last Post: 16-02-2010, 08:09 PM
  4. How does C sharp support try-catch-finally block?
    By Trance Maniac in forum Software Development
    Replies: 5
    Last Post: 21-01-2010, 09:02 AM
  5. Using Try Catch Block In SQL Server 2005
    By Aaryn in forum Software Development
    Replies: 3
    Last Post: 06-06-2009, 08:01 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,711,699,416.16199 seconds with 16 queries