Processing of Java Exceptions __java

Source: Internet
Author: User
Problem 1, inheritance and runtimeexception because it is a uncheck type of error, you can reduce the amount of code in the future program and ease of use. And exception is a must check the type of error, the program must add try,catch processing, more cumbersome.

About log4j because the catalog in each class is not the same. And there is no need to distinguish multiple users of information, should be able to meet the current requirements.

The following other:

Solve the exception overwrite problem by customizing the exception.

And you can also implement custom exception information

Some points of attention in Java exception processing are explained

Custom Exception classes:

By overloading the GetMessage method, the output of abnormal information is rewritten in multiple exception cases. And the method can also implement the binding of exception code and exception information: can be read from the configuration file or the database, the exception encoding to the exception of the information generated output

Package com.gever.jcw.test;

Import java.util.List;
Import java.util.ArrayList;


public class MyException extends exception{
List List;
Public MyException () {
Super ();
List=new ArrayList ();
}
Public myexception (exceptionmsg msg) {
Super ();
List=new ArrayList ();
List.add (msg);
}
public void Addexception (Exceptionmsg msg) {
List.add (msg);
}
Public List getexceptionlist () {
return list;
}
Public String GetMessage () {
StringBuffer sb=new StringBuffer ();
if (list==null) {
Sb.append ("Nothing exception!/n");
}else{
if (List.size () >0) {
for (int i=0;i exceptionmsg msg= (exceptionmsg) list.get (i);
Sb.append (Msg.getclassname ());
Sb.append (".");
Sb.append (Msg.getmethodname ());
Sb.append ("");
Sb.append (Msg.getlinenumber ());
Sb.append ("line,");
Sb.append ("wrong number");
Sb.append (Msg.geterrnumber ());
Sb.append (":");
Switch (Msg.geterrnumber ()) {
Case 101:
Sb.append ("This is 0 wrong");
Break
Case 102:
Sb.append ("This is 1 wrong");
Break
Case 103:
Sb.append ("This is 1 wrong");
Break
Default
Sb.append ("Unknow error");
Break
}
Sb.append ("n");
}
}
}
return sb.tostring ();
}
public void print () {
System.out.println (This.getmessage ());
}
}

Exception Information class:

Package com.gever.jcw.test;

public class Exceptionmsg {
private int errnumber=0;
Private String classname= "";
Private String methodname= "";
private int linenumber=0;

Public exceptionmsg (int errnumber,string classname,string methodname,int linenumber) {
This.errnumber=errnumber;
This.classname=classname;
This.methodname=methodname;
This.linenumber=linenumber;
}
public int Geterrnumber () {
return errnumber;
}

public void Seterrnumber (int errnumber) {
This.errnumber = Errnumber;
}

Public String GetClassName () {
return classname;
}

public void Setclassname (String classname) {
This.classname = classname;
}

Public String Getmethodname () {
Return methodname;
}

public void Setmethodname (String methodname) {
This.methodname = methodname;
}

public int Getlinenumber () {
return linenumber;
}

public void Setlinenumber (int linenumber) {
This.linenumber = linenumber;
}
}

The test classes are as follows:

Package com.gever.jcw.test;
public class Testt {
public void Test () throws myexception{
MyException myex=new myexception ();
try{
file://myex.addException ();
            throw new MyException (new Exceptionmsg 101, This.getclass (). toString (), "test", 14);
       }catch (Exception e) {
             myex.addexception (New Exceptionmsg (102,this.getclass (). toString (), "test", 16));
       }finally{
             myex.addexception (New Exceptionmsg (103,this.getclass (). toString (), "test", 18));
            throw Myex;
       }

}
public static void Main (string[] args) {
Testt test=new Testt ();
try{
Test.test ();
}catch (MyException e) {
E.print ();
E.printstacktrace ();
}
}
}
Another note: Finally, it will be executed before return. If there is a return in a try or a catch statement, and there is finally, then it will not execute.

Several points of attention in Java exception handling:

1 use finally to prevent resource leaks

2 The return statement is not executed from the try block. If the block has a finally block after it, the finally block will first execute the

3 The try and catch blocks are placed outside the loop body, because the execution efficiency of the two is significantly different without the JIT compiler

4 only in exceptional circumstances to use the exception. Can not be expected to call the exception, otherwise should be judged by the statement to check

5 throws an exception in the constructor. This avoids the construction of an incorrect object

6 before throwing an exception, restore the valid state of the object, if possible.

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.