Exception template code and template code

Source: Internet
Author: User

Exception template code and template code

Read this article: http://tutorials.jenkov.com/java-exception-handling/exception-handling-templates.html

Record:

After an exception is caughtfinallyWhen an exception is thrown, it overwrites the previous exception information. Therefore, you need to clearly identify each possible exception, so that no overwrite exception is missed. If the code is complex and exception determination is a one-click redundancy task, the template is used to simplify the work and reduce errors.

It is actually about how a common code can be more elegantly shared.

 Input       input            = null;    IOException processException = null;    try{        input = new FileInputStream(fileName);        //...process input stream...    } catch (IOException e) {        processException = e;    } finally {       if(input != null){          try {             input.close();          } catch(IOException e){             if(processException != null){                throw new MyException(processException, e,                  "Error message..." +                  fileName);             } else {                throw new MyException(e,                    "Error closing InputStream for file " +                    fileName;             }          }       }       if(processException != null){          throw new MyException(processException,            "Error processing InputStream for file " +                fileName;    }

 

Template mode:

public abstract class InputStreamProcessingTemplate {    public void process(String fileName){        IOException processException = null;        InputStream input = null;        try{            input = new FileInputStream(fileName);            doProcess(input);        } catch (IOException e) {            processException = e;        } finally {           if(input != null){              try {                 input.close();              } catch(IOException e){                 if(processException != null){                    throw new MyException(processException, e,                      "Error message..." +                      fileName);                 } else {                    throw new MyException(e,                        "Error closing InputStream for file " +                        fileName;                 }              }           }           if(processException != null){              throw new MyException(processException,                "Error processing InputStream for file " +                    fileName;        }    }    //override this method in a subclass, to process the stream.    public abstract void doProcess(InputStream input) throws IOException;}

Call:

  new InputStreamProcessingTemplate(){        public void doProcess(InputStream input) throws IOException{            int inChar = input.read();            while(inChar !- -1){                //do something with the chars...            }        }    }.process("someFile.txt");

 

Interface implementation:

public interface InputStreamProcessor {    public void process(InputStream input) throws IOException;}public class InputStreamProcessingTemplate {    public void process(String fileName, InputStreamProcessor processor){        IOException processException = null;        InputStream input = null;        try{            input = new FileInputStream(fileName);            processor.process(input);        } catch (IOException e) {            processException = e;        } finally {           if(input != null){              try {                 input.close();              } catch(IOException e){                 if(processException != null){                    throw new MyException(processException, e,                      "Error message..." +                      fileName;                 } else {                    throw new MyException(e,                        "Error closing InputStream for file " +                        fileName);                 }              }           }           if(processException != null){              throw new MyException(processException,                "Error processing InputStream for file " +                    fileName;        }    }}

Call:

new InputStreamProcessingTemplate()        .process("someFile.txt", new InputStreamProcessor(){            public void process(InputStream input) throws IOException{                int inChar = input.read();                while(inChar !- -1){                    //do something with the chars...                }            }        });

 

Static template:

public class InputStreamProcessingTemplate {    public static void process(String fileName,    InputStreamProcessor processor){        IOException processException = null;        InputStream input = null;        try{            input = new FileInputStream(fileName);            processor.process(input);        } catch (IOException e) {            processException = e;        } finally {           if(input != null){              try {                 input.close();              } catch(IOException e){                 if(processException != null){                    throw new MyException(processException, e,                      "Error message..." +                      fileName);                 } else {                    throw new MyException(e,                        "Error closing InputStream for file " +                        fileName;                 }              }           }           if(processException != null){              throw new MyException(processException,                "Error processing InputStream for file " +                    fileName;        }    }}

 

Call:

 InputStreamProcessingTemplate.process("someFile.txt",        new InputStreamProcessor(){            public void process(InputStream input) throws IOException{                int inChar = input.read();                while(inChar !- -1){                    //do something with the chars...                }            }        });

 


An error occurred while defining the member functions outside the template class. The source code is as follows:

I have run your code without the error you mentioned, however, there are a lot of errors in your code, which cannot be correctly sorted. below is what I changed for you to run on my computer. You can ask me if you can't.
# Include <iostream>
Using namespace std;
Const int maxsize = 10;
Template <typename T>
Class search
{
Private:
T * array;

Public:
Search ();
Search (T a []);
~ Search ();
Void dubble_sort ();
Void insert_sort ();
Void binary_sort ();
Void sort_choice ();
Void display ();

};

Template <typename T>
Search <T>: search ()
{
Array = new T [maxsize];

}

Template <typename T>
Search <T>: search (T a [])
{
Array = new T [maxsize];
For (int I = 0; I <maxsize; I ++)
{
Array [I] = a [I];

}

}

Template <typename T>
Search <T> ::~ Search ()
{
Delete [] array;
}

Template <typename T>
Void search <T >:: dubble_sort ()
{

For (int I = 1; I <maxsize; I ++)
{
For (int j = 0; j <= maxsize-I; j ++)
If (array [j]> array [j + 1])
{
T temp;
Temp = array [j];
Array [j] = array [j + 1];
Array [j + 1] = temp;

}

}

}

Template <typename T>
Void search <T >:: insert_sort ()
{
For (int I = 0; I <maxsize; I ++)
{
Int k = I;
For (int j = I-1; j> = 0; j --)
{
If (array [k] <array [j])
{
T temp;
Temp = array [j];
Array [j] = array [k];
Array [k] = temp;

}

}

}

}

Template <typename T>
Void search <T >:: binary_sort ()
{
For (int I = 0; I <maxsize; I ++)
{Int k = I + 1;
Int left = 0, right = I, mid = (left + right)/2;
If (array [k]> array [mid])
{
Left = mid;
Mid = (left + r... the remaining full text>

How can I change the template call error in the background of zhimeng? My code is too small to mention the detailed steps.

First, you need to know where your template is.
By default, the root directory/templet/default/xxx.htm is used.
These are templates, and you are the first core website topic management in your background. You create a new topic and there will be several tabs, General tabs, advanced tabs, and topic content.
On the General tab, We need to configure the column storage path, default file name, and column attribute.
The key is column attributes,
Including: final list columns, channel covers, external connections (this is generally useless)
This is related to whether the cover template or list template is called for this topic in the Advanced tab.
If you select the channel cover to generate HTML, you call the cover template.
The final list column calls the list template.

Next
Select the advanced option card to configure the template to be used.
Three template configurations are available here.
Cover template, list template, article template,
Select these templates from the initial template location
In general, the official dream
The seal Template Name Is index_article.htm,
The life cycle Template Name Is list_article.htm,
The Template Name Is article_article.htm.

The template specified here must exist and only applies to the column you created. If other columns are created, you need to reconfigure these templates, of course, you can select different templates for different columns.

Finally, it is the homepage template. The homepage template is relatively simple and can be directly generated. You can configure the path and generated place of the homepage template in the "generate Homepage" option.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.