异常从不抛出对应的try语句的正文 [英] Exception is never thrown in body of corresponding try statement

查看:1311
本文介绍了异常从不抛出对应的try语句的正文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Java中遇到异常处理问题,这是我的代码。当我尝试运行这一行时,我收到编译器错误: throw new MojException(Bledne dane); 。错误是:


异常MojException从不抛出对应的try语句的正文


这是代码:

  public class Test {
public static void (int i = 1; i< args.length; i ++){
尝试{
Integer.parseInt(args [i-1]);
}
catch(MojException e){
抛出新的MojException(Bledne dane);
}
try {
WierszTrojkataPascala a = new WierszTrojkataPascala(Integer.parseInt(args [0]));
System.out.println(args [i] +:+ a.wspolczynnik(Integer.parseInt(args [i])));
}
catch(MojException e){
throw new MojException(args [i] ++ e.getMessage());
}
}
}
}

这里是MojException的代码:

  public class MojException extends Exception {
MojException(String s){
超级
}
}

任何人都可以帮助我吗?

解决方案

try语句中的catch-block需要捕获正好 < $ c> try {} -block 可以抛出(或超级类)。

  try {
// do something throws ExceptionA,eg
抛出新的ExceptionA(我是异常Alpha!);
}
catch(ExceptionA e){
//做某事来处理异常,例如
System.out.println(Message:+ e.getMessage());
}

您要做的是这样的:

  try {
throw new ExceptionB(I am Exception Bravo!);
}
catch(ExceptionA e){
System.out.println(Message:+ e.getMessage());
}

这将导致编译器错误,因为您的java知道您正在尝试以捕获永远不会发生的异常。因此,您将得到:异常ExceptionA从不抛出对应的try语句


I have a problem with exception handling in Java, here's my code. I got compiler error when I try to run this line: throw new MojException("Bledne dane");. The error is:

exception MojException is never thrown in body of corresponding try statement

Here is the code:

public class Test {
  public static void main(String[] args) throws MojException {
    // TODO Auto-generated method stub

    for(int i=1;i<args.length;i++){
      try{
        Integer.parseInt(args[i-1]);
      }
      catch(MojException e){
        throw new MojException("Bledne dane");
      }
      try{
        WierszTrojkataPascala a = new WierszTrojkataPascala(Integer.parseInt(args[0]));
        System.out.println(args[i]+" : "+a.wspolczynnik(Integer.parseInt(args[i])));
      }
      catch(MojException e){
        throw new MojException(args[i]+" "+e.getMessage());
      }
    }
  }
}

And here is a code of MojException:

public class MojException extends Exception{
    MojException(String s){
        super(s);
    }
}

Can anyone help me with this?

解决方案

A catch-block in a try statement needs to catch exactly the exception that the code inside the try {}-block can throw (or a super class of that).

try {
    //do something that throws ExceptionA, e.g.
    throw new ExceptionA("I am Exception Alpha!");
}
catch(ExceptionA e) {
    //do something to handle the exception, e.g.
    System.out.println("Message: " + e.getMessage());
}

What you are trying to do is this:

try {
    throw new ExceptionB("I am Exception Bravo!");
}
catch(ExceptionA e) {
    System.out.println("Message: " + e.getMessage());
}

This will lead to an compiler error, because your java knows that you are trying to catch an exception that will NEVER EVER EVER occur. Thus you would get: exception ExceptionA is never thrown in body of corresponding try statement.

这篇关于异常从不抛出对应的try语句的正文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆