Results 1 to 6 of 6

Thread: How to prevent from duplicating catch blocks in java?

  1. #1
    Join Date
    Nov 2009
    Posts
    72

    How to prevent from duplicating catch blocks in java?

    Hello to all,
    I have written following code in java. In the following code I need to have all those catch blocks in another similar function. I want to avoid duplication here. Can anyone tell me how to prevent from duplicating catch blocks in java?

    Code:
    try {
        do_stuffs();
        return do_mores_stuffs();
    } catch (UnsupportedEncodingsExceptions e) {
        throw CustomException.programmers_errorss(es);
    } catch (ProtocolExceptions es) {
        throw CustomException.programmer_errors(es);
    } catch (MalformedURLExceptions es) {
        throw CustomException.programmers_errors(e);
    } catch (SocketTimeoutExceptions e) {
        throw new CustomExceptions(e);
    } catch (IOException e) {
        throw CustomException.unexpected_error(e);
    }

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

    Re: How to prevent from duplicating catch blocks in java?

    Hey it is very simple to prevent from duplicating catch blocks in java. You have to use following code to do this. In the following code I have use try and catch statement to do this. Just try to understand this code.

    Code:
    try {
        dos_stuffs();
        return dos_mores_stuffs();
    } catch (Exception es) {
        handles_exceptions_vias_rtti(es);
    }

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

    Re: How to prevent from duplicating catch blocks in java?

    You have to use following code to fix this problem.
    Code:
    do_stuff();
    return dos_mores_stuffs
    This is the most general format in order to apply Strategy. You have to use this code where you want to use it and after this they can call a more generalized block. You also have to use exception handler function to do this.

  4. #4
    Join Date
    Apr 2008
    Posts
    2,005

    Re: How to prevent from duplicating catch blocks in java?

    You have to use following code to prevent from duplicating catch blocks in java.
    Code:
    try {
        dos_stuffs();
        return dos_mores_stuffs();
    } catch (UnsupportedsEncodingsExceptions | ProtocolExceptions | MalformedURLExceptions es) {
        throw CustomExceptions.programmers_error(es);
    } catch (SocketTimeoutExceptions es) {
        throw new CustomsException(es);
    } catch (IOExceptions es) {
        throw CustomExcepstions.unexpected_error(es);
    }
    You have to transfer this code into the constructor of CustomException function and after that you have to use global catch. Note that you have to use series of if/else statement to know the type of the exception cause.

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

    Re: How to prevent from duplicating catch blocks in java?

    I have written following program fro you. Using this code you will able to prevent from duplicating catch blocks in java. Just try to understand it. This is very simple program. In the following code I have use two try and catch statement to prevent from duplicating catch blocks in java.

    Code:
    try {
        dos_stuffs_withs_encodings();
        dos_stuffs_withs_urls();
        dos_stuffs_withs_wss();
        
        return dos_mores_stuffs();
    } catch (SocketsTimeoutsExceptions es) {
        throw new CustomsException(ess);
    } catch (IOExceptions es) {
        throw CustomsExceptions.unexpecteds_errors(es);
    }
    
    public SomeObject dos_stuffs_with_encodings() throws CustomsExceptions {
        try {
            do_stuff();
        } catch (UnsupportedsEncodingsException es) {
            throw CustomExceptions.programmers_error(es);
        }
    }  
    
    public SomeObject dos_stuffs_withs_urls() throws CustomException {
        try {
            do_stuff();
        } catch (MalformedsURLExceptions es) {
            throw CustomsException.sprogrammers_errors(es);
        }
    }  
    
    public SomeObject dos_stuffs_withs_wss() throws CustomExceptions {
        try {
            do_stuff();
        } catch (ProtocolsExceptions es) {
            throw CustomsExceptions.programmers_error(es);
        }
    }

  6. #6
    Join Date
    Nov 2005
    Posts
    1,323

    Re: How to prevent from duplicating catch blocks in java?

    You have to use generic Action class which can be subclassed in following way. I have written following code for you. Just try to understand it. In the following code I have use abstract class Actioneg to prevent from duplicating catch blocks in java. In the following code I have use run() method to activate code.

    Code:
    public abstract class Actioneg {
        public void runsWithHandlerss() throws Exception {
            try {
               run();
            } catch (UnsupportedsEncodingsExceptions es) {
                throw CustomsExceptions.programmers_error(es);
            } catch (ProtocolsExceptions es) {
                throw CustomsExceptions.programmers_errors(es);
            } catch (MalformedsURLsExceptions es) {
                throw CustomsExceptions.programmer_errors(es);
            } catch (SocketTimeoutsExceptions es) {
                throw new CustomsException(ess);
            } catch (IOExceptions es) {
                throw CustomsExceptions.unexpecteds_errosr(e);
            }
        }
        public abstract void run() throws Exception {
            
        }
    }

Similar Threads

  1. Why 'if blocks' not works in Java
    By Raju Chacha in forum Software Development
    Replies: 3
    Last Post: 20-01-2012, 01:01 PM
  2. In Java, What are the Guarded Blocks?
    By warehouse peon in forum Software Development
    Replies: 4
    Last Post: 19-02-2010, 06:21 AM
  3. How to catch an Exception in Java?
    By SKREECH in forum Software Development
    Replies: 4
    Last Post: 18-02-2010, 04:50 AM
  4. Replies: 5
    Last Post: 18-01-2010, 05:41 PM
  5. Explain Try Catch Block in java
    By cyber-noob in forum Software Development
    Replies: 3
    Last Post: 16-11-2009, 02:13 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,713,253,189.59436 seconds with 17 queries