2012-10-11 106 views
0

我在找上如何处理Java异常的情况下的建议,其中Java自定义异常处理

Class A.test() 
{ 
    ClassB.test1() 
} 


test1() 
{ 
    Map listOfExceptions = new HashMap(); 
    for() 
    { 
     try{ 
     } 
     catch(custException e){ 
     // I want the for loop to continue even if there is an exception 
     //So I am not throughing now rather adding the exception message to map with 
     //the field name for which for loop is running as key and message as value 
     { 
    } 

// at some point if map has >0 records I want to throw an exception to classA. 
} 

所以这里的问题是我怎么可以在地图数据添加到抛出的异常?基本上我需要将异常列表发送到调用方法,作为异常处理的一部分。 有没有办法可以做到这一点? 如果答案是众所周知的,这可能是一个非常愚蠢的问题,但我力求在任何地方找到确切的答案。

+0

是这个java代码? – Prasanth

+0

此Java代码不会编译。 – 2012-10-11 08:19:23

+3

我认为(公平)这是Java-ish伪代码 –

回答

7

为什么不在迭代时只生成List<Exception>?然后在完成时抛出CustomException(源自Exception),如果该列表不是空的,则在CustomException中提供List<Exception>作为字段。

您可能不想存储List<Exception>,而是List<Info>,其中Info表示导致异常的数据。这可能会更轻。

更好的是,创建一个ErrorCollator对象,您可以将其添加到Exceptions。完成后,请致电ErrorCollator.throwExceptionIfReqd(),并且该对象本身可以确定是否抛出异常(简单地说,如果List不为空)。这样你就有了一个可重复使用的组件/模式,你可以持续使用。也许值得努力,如果不立即做。

+0

谢谢布赖恩,这有帮助。 – Swagatika

0

创建一个自定义异常类像

Class CustomException extends Exception{ 

Map listOfExceptions; 


CustomException (Map m){ 
    ListofExceptions=m; 
} 
} 

现在抛出这个客户异常,当记录> 0或任何其它条件。

throw new CustomException(listOfExceptions); 
0

退房的,你怎么能一个特例到一个HashMap下面的代码

public class ClassA { 

    HashMap hm = new HashMap(); //HashMap Object that holds Exceptions 

    public ClassA(){ 
     try { 
      runA(); 
     } catch (Exception ex) { 
      hm.put("a", ex); //put Exception in HashMap 
     } 
     try { 
      runB(); 
     } catch (Exception ex) { 
      hm.put("b", ex); //put Exception in HashMap 
     } 
    } 

    private void runA() throws Exception { 
     throw new Exception("a"); //Generate exception a 
    } 

    private void runB() throws Exception { 
     throw new Exception("b"); //Generate exception b 
    } 
} 

然后你就可以通过提供一些吸气功能,让您的HashMap对象,并使用该循环扩展此类(迭代器)可以迭代它:

// Get a set of the entries 
Set set = hm.entrySet(); 
// Get an iterator 
Iterator i = set.iterator(); 
// Display elements 
while(i.hasNext()) { 
    Map.Entry me = (Map.Entry)i.next(); 
    System.out.print(me.getKey() + ": "); 
    System.out.println(me.getValue()); 
} 
0

请尝试下面的代码。在我的示例代码中,我将一个String解析为int。

直到数组结束,解析仍在继续。如果发生异常,则将其放到 图上。该映射包含for循环正在以 键和异常消息作为值运行的字段名称。

在数组末尾,检查映射是否需要抛出异常调用类 。

public class ExceptionHadlingTest { 

    public static void main(String[] args) { 
     A1 a1 = new A1(); 
     a1.test(); 
    } 
} 
class A1 { 
    public void test() { 

     B1 b1= new B1(); 
     try { 
      b1.test1(); 
     } catch (Exception e) { 
     } 
    } 
} 

class B1 { 
    public void test1() throws Exception { 
     Map<String, String> exceptions = new HashMap<String, String>(); 
     String[] array = {"1","a","2","b","6"}; 
     for (int i=0; i< array.length ; i++) { 
      try { 
       int a = Integer.parseInt(array[i]); 
      } catch (Exception e) { 
       exceptions.put(array[i], e.getMessage()); 
      } 
     } 
     if(exceptions.size() > 0) { 
      System.out.println("Total Number of exception " + exceptions.size()); 
      for (Entry<String, String> entry : exceptions.entrySet()) { 
       System.out.println(entry.getKey() + " " + entry.getValue()); 
      } 
      throw new Exception(); 
     } 
    } 
    private int parse(String str) { 
     int a = Integer.parseInt(str); 
     return a; 
    } 
} 

您也可以使用里面customException的。