现在的位置: 首页 > 综合 > 正文

java 自定义异常

2013年10月27日 ⁄ 综合 ⁄ 共 4829字 ⁄ 字号 评论关闭

自己抛出异常,调用者捕获异常,

    class DefineException  
    {  
        public int shang(int x,int y)
        {  int m=0;
            try{
                m=x/y;             
                throw new DataParseException("11111111");
            }catch(DataParseException e){
                   e.printStackTrace();         
            }
         return m;
        }  
    } 
上面会执行  e.printStackTrace()语句,同时也不能缺少 throw new DataParseException("11111111"),但是即有这话它也不会打印11111111,如果没有throw new DataParseException("111111111")这一句将会出现Unreachable catch block for DataParseException. This exception is never thrown from the try statement body
也说是说你这执行语块中根本没有抛出任何异常,不可能有捕获异常的语句,

    class Numbertest   
    {  
        public int shang(int x,int y) throws DataParseException  
        {
            int m=0;
            if(m==1)
                {
                    System.out.println("finally!")    ;
                }
            else{
                throw new DataParseException();
                }
         return m;
        }  
    } 

或是这样

    class Numbertest   
    {  
        public int shang(int x,int y)   
        {
            int m=0;
            try{
                m=6;
            }catch(DataParseException e){
                throw new DataParseException();
            }
         return m;
        }  
    } 

调用的时候:     这样即可,不需要在调用处的这个方法后面加上throws DataParseExcepton    

public ovid pumethod{

Numbertest n=new Numbertest();
        try {
            n.shang(2, -3);
        } catch (DataParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

}

上面如果没有throws DataParseException  会报错,我也不知道原因,还请高人指教

最近在项目中需用到自定义异常,在网上粗略的查了一下这类的信息,其中有很多博客给我们讲解了此类的文章 :

http://www.189works.com/article-42157-1.html

自定义异常 : 步骤: ­
一:创建一个类继承于Throwable或其子类; ­
二:添加构造方法; ­
三:在一个方法中使用throw抛出异常 ­
四:在另一个方法中捕获并处理异常 ­
示例:
一、继承Exception类 

Java代码  
  1. public class MyFirstException extends Exception {  
  2. public MyFirstException() {  
  3. super();  
  4. }  
  5. public MyFirstException(String msg) {  
  6. super(msg);  
  7. }  
  8. public MyFirstException(String msg, Throwable cause) {  
  9. super(msg, cause);  
  10. }  
  11. public MyFirstException(Throwable cause) {  
  12. super(cause);  
  13. }  
  14. //自定义异常类的主要作用是区分异常发生的位置,当用户遇到异常时,  
  15. //根据异常名就可以知道哪里有异常,根据异常提示信息进行修改。  
  16. }  

二、 继承Throwable 类 

Java代码  
  1. public class MySecondException extends Throwable {  
  2. public MySecondException() {  
  3. super();  
  4. }  
  5. public MySecondException(String msg) {  
  6. super(msg);  
  7. }  
  8. public MySecondException(String msg, Throwable cause) {  
  9. super(msg, cause);  
  10. }  
  11. public MySecondException(Throwable cause) {  
  12. super(cause);  
  13. }  
  14. }  

三、测试 

Java代码  
  1. /** 
  2. * 自定义异常类的使用 
  3. * @author new 
  4. * 
  5. */  
  6. public class TestMyException {  
  7. public static void firstException() throws MyFirstException{  
  8. throw new MyFirstException("\"firstException()\" method occurs an exception!");  
  9. }  
  10.   
  11. public static void secondException() throws MySecondException{  
  12. throw new MySecondException("\"secondException()\" method occurs an exception!");  
  13. }  
  14. public static void main(String[] args) {  
  15. try {  
  16. TestMyException.firstException();  
  17. TestMyException.secondException();  
  18. catch (MyFirstException e1){  
  19. System.out.println("Exception: " + e1.getMessage());  
  20. e1.printStackTrace();  
  21. catch (MySecondException e2){  
  22. System.out.println("Exception: " + e2.getMessage());  
  23. e2.printStackTrace();  
  24. }  
  25. //当一个try块后面跟着多个catch块时,如果发生的异常匹配第一个catch块的参数,便将异常处理权利交给第一个catch块。  
  26. //如果发生的异常与第一个catch块不匹配,便看是否与第二个catch块匹配,依次下去,如果到最后依然无法匹配该异常,  
  27. //便需要在方法声明中添加一条throw语句,将该异常抛出。  
  28. //因此,在有多个catch块,而且每次处理的异常类型具有继承关系时,应该首先catch子类异常,再catch父类异常。  
  29. //比如,如果MySecondException继承MyFirstException,那么最好将catch(MySecondException e2)放在前面  
  30. //把catch (MyFirstException e1)放在后面。  

http://blog.csdn.net/stellaah/article/details/6738424

/*下面做了归纳总结,欢迎批评指正*/

/*自定义异常*/
class ChushulingException extends Exception
{
    public ChushulingException(String msg)
    {
        super(msg);
    }
}

class ChushufuException extends Exception
{
    public ChushufuException(String msg)
    {
        super(msg);
    }
}

/*自定义异常 End*/

class Numbertest
{
    public int shang(int x,int y) throws ChushulingException,ChushufuException
    {
        if(y<0)
        {
            throw new ChushufuException("您输入的是"+y+",规定除数不能为负数!");//抛出异常
        }
        if(y==0)
        {
            throw new ChushulingException("您输入的是"+y+",除数不能为0!");
        }
    
        int m=x/y;
        return m;
    }
}

class Rt001
{
    public static void main(String[]args)
    {
        Numbertest n=new Numbertest();

        //捕获异常
        try
        {
            System.out.println("商="+n.shang(1,-3));
        }
        catch(ChushulingException yc)
        {
            System.out.println(yc.getMessage());
            yc.printStackTrace();
        }
        catch(ChushufuException yx)
        {
            System.out.println(yx.getMessage());
            yx.printStackTrace();
        }
        catch(Exception y)
        {
            System.out.println(y.getMessage());
            y.printStackTrace();
        }
    
    finally{ System.out.println("finally!");} ////finally不管发没发生异常都会被执行    

    }
}
/*
[总结]

1.自定义异常:

class 异常类名 extends Exception
{
    public 异常类名(String msg)
    {
        super(msg);
    }
}

2.标识可能抛出的异常:

throws 异常类名1,异常类名2

3.捕获异常:
try{}
catch(异常类名 y){}
catch(异常类名 y){}

4.方法解释
getMessage() //输出异常的信息
printStackTrace() //输出导致异常更为详细的信息

*/

对java自定义异常还不是很了解的朋友,这两篇文章值得我们去学习,

抱歉!评论已关闭.