相同的异常类型与不同势错误消息

相同的异常类型与不同势错误消息

问题描述:

伊夫二方法是,每一个方法从不同的理由扔掉它,我包裹方法主要有尝试捕捉 ,什么是推荐的方法来解决它? 我需要与同一类型每个异常不同的短信..相同的异常类型与不同势错误消息

public static void main(String[] args) { 

try{ 

…. 


readFile(path); 

convert(file) 

} catch (IOException e) { 
    ….. 
} 


private static String readFile(String path) throws IOException { 
    //Here files requires IOException - Reason here cannot Read file 
    String lineSeparator = System.getProperty("line.separator"); 
    List<String> lines = Files.readAllLines(Paths.get(path)); 
} 


private static String convert(String file) throws IOException { 
    //Here reader requires also ioException- Reason here cannot parse file 
    ObjectMapper reader = new ObjectMapper(new YAMLFactory()); 
    Object obj = reader.readValue(file, Object.class); 

} 
+1

创建您自己的异常然后 –

+1

在单独的try/catch块中调用两个方法。 –

+0

或使用相同的块并通过equals或contains检查错误消息以确定引发了哪个异常。 – Acewin

有几种方法,你可以处理这个。一种方式,也许你需要编写的新代码中最重的代码是从每个辅助方法中抛出一个自定义异常。然后,您可以在单独的块中捕获每个特定的异常。

但我会在这里推荐的是,您只需在单独try-catch块,每个块的两个电话中的换行到您的辅助方法:

try { 
    readFile(path); 
} catch (IOException e) { 
    // handle first exception here 
} 

// more code 
try { 
    convert(file) 
} catch (IOException e) { 
    // handle second exception here 
} 

这是相当干净,不需要大量的重构。如果您一直遇到此问题,那么可以考虑为您的应用程序创建自定义例外。如果你看看很多Java库,你会发现他们经常使用自己的自定义异常。

如果您想要使用自定义例外的路线,您可以定义一个例如

public class FileReadIOException extends Exception { 
    public FileReadIOException(String message) { 
     super(message); 
    } 
} 

,然后使用它:

private static String readFile(String path) throws FileReadIOException { 
    try { 
     String lineSeparator = System.getProperty("line.separator"); 
     List<String> lines = Files.readAllLines(Paths.get(path)); 
    } 
    catch (Exception e) { 
     throw new FileReadIOException(e.getMessage()); 
    } 
} 

try { 
    readFile(path); 
    // more code 
    convert(file) 
} catch (FileReadIOException e) { 
    // handle first exception here 
} catch (SomeOtherException e) { 
    // handle second exception here 
} 

显示自定义异常上面的代码是有点做作,因为现实情况是,所有的代码抛出IOException。在您的案例中创建自定义例外不会增加太多价值,因为它们已经(正确)抛出IOException。我不确定只处理一种异常是没有意义的。更典型的是,如果您正在处理大型企业应用程序,那么您将使用自定义异常来处理在自己的自定义代码中出错的情况。

+0

你能举出第一种方法的例子吗? –

+0

还问题是,如果出现一个异常,我需要中止程序... –

+0

那么第二种方法是推荐的方法吗?我想了解最佳实践...没有简短的解决方案 –

这是我5分钱

public static void main(String[] args) throws Exception { 

    String path = "path"; 
    String path2 = "path2"; 


    try{ 
     readFile(path); 
    } catch (IOException e) { 
     throw new Exception("read file exception", e); 
    } 

    try{ 
     convert(path2); 
    } catch (IOException e) { 
     throw new Exception("convert exception", e); 
    } 


} 


private static String readFile(String path) throws IOException { 
    //Here files requires IOException - Reason here cannot Read file 
    String lineSeparator = System.getProperty("line.separator"); 
    List<String> lines = Files.readAllLines(Paths.get(path)); 
} 


private static String convert(String file) throws IOException { 
    //Here reader requires also ioException- Reason here cannot parse file 
    ObjectMapper reader = new ObjectMapper(new YAMLFactory()); 
    Object obj = reader.readValue(file, Object.class); 
} 

我的方法通常是创建自己的异常,并把它像

public class Snippet { 
    public static void main(String[] args) { 
     try { 
      String path = ""; 
      readFile(path); 
      String file = ""; 
      convert(file); 
     } catch (MyException e) { 
      // do whatever 
     } 
    } 

    private static String readFile(String path) throws MyException { 
     try { 
      String lineSeparator = System.getProperty("line.separator"); 
      List<String> lines = Files.readAllLines(Paths.get(path)); 
     } catch (Exception e) { 
      throw new MyException("Custom 'readFile' message", e); 
     } 
    } 

    private static String convert(String file) throws MyException { 
     try { 
      ObjectMapper reader = new ObjectMapper(new YAMLFactory()); 
      Object obj = reader.readValue(file, Object.class); 
     } catch (Exception e) { 
      throw new MyException("Custom 'convert' message", e); 
     } 
    } 
} 

class MyException extends Exception { 
    private static final long serialVersionUID = -3166824380774329449L; 

    public MyException(String message, Throwable cause) { 
     super(message, cause); 
     // TODO Auto-generated constructor stub 
    } 
} 

蒂姆的方法是有效的了。

@OP最好的人来解决这个问题就是你自己。 有很多方法可以解决这个问题。检查哪一个更适合你。

根据我的解决方案之一如下。这是基于IOException是许多其他异常的超类的原因。 IOException documentation

try { 
    readFile(path); 
} catch (FileNotFoundException e) { 
    // handle first exception here 
} catch (EOFException e) { 
    // handle 2nd exception here 
} 

对于上述工作,你需要知道哪种类型的IOException异常被抛出。

另一种解决方案是在知道可能收到的预期消息时检查单个异常消息。

try { 
    readFile(path); 
} catch (IOException e) { 
    if(e. getMessage().contains("TXT 1...") { 
      //Case handle #1 
    } else if(e. getMessage().contains("TXT 2...") { 
      //Case handle #2 
    } 
} 

处理异常的最好方法是不要让它们。当引发异常时,它表明应用程序的自然生命周期由于某种原因被中断。大多数例外情况都是不言自明的,并为您提供关于发生情况的准确解释,因此创建新的例外情况并重新映射那些抛出的数据几乎总是适得其反,并且可能导致比有用性更为混乱(尤其是如果您在团队中工作)。

此外,异常不需要是终端,在大多数情况下,可以设计一个方案来重试/提示不同的输入等,以确保生命周期不会中断。在异常情况下终止应用程序可能会在某些情况下产生更多问题(例如:未正确关闭文件,从而丢失处理的数据)。

现在到你的实际问题。如果你有两个或更多的组件,这会抛出不相关的异常(在这种情况下名称不是关系),最好不要将它们放在同一个try/catch结构中,因为较少的解决方法将需要取消一个部分整个事情仍然可以自行退出,甚至不需要启动。