2017-06-03 75 views
1

好吧...所以我正在学习java中的异常,我现在处于throw语句。我抛出Exception类的异常,然后再从catch块重新抛出它以在主函数中处理它。但是,每当我把它作为Exception类抛出时,我总是会在catch块中得到一个Error(在我重新抛出它的时候会被main处理)。但是,只要我将引发和捕获的异常更改为某些特定的异常(如NullPointerException)有用!从异常类中抛出异常在java中

错误代码:

class ThrowingExceptions { 
    static boolean enable3dRendering = false; 

    public static void main(String [] com) { 

     try { 
      renderWorld(); 
     } 
     catch(Exception e) { 
      System.out.println("Rendering in 2d."); 
     } 
    } 

    static void renderWorld() { 
     try{ 
      if(!enable3dRendering) { 
       System.out.println("3d rendering is disabled. Enable 3d mode to render."); 
       throw new Exception("3d mode Disabled."); 
      } 
      else { 
       System.out.println("The World is Empty!"); 
      } 
     } 
     catch(Exception e) { 
      System.out.println("Please handle the error"); 
      throw e; // It gives me an error here 
     } 
    } 
} 

工作代码:

class ThrowingExceptions { 
    static boolean enable3dRendering = false; 

    public static void main(String [] com) { 

     try { 
      renderWorld(); 
     } 
     catch(NullPointerException e) { 
      System.out.println("Rendering in 2d."); 
     } 
    } 

    static void renderWorld() { 
     try{ 
      if(!enable3dRendering) { 
       System.out.println("3d rendering is disabled. Enable 3d mode to render."); 
       throw new NullPointerException("3d mode Disabled."); 
      } 
      else { 
       System.out.println("The World is Empty!"); 
      } 
     } 
     catch(NullPointerException e) { 
      System.out.println("Please handle the error"); 
      throw e; 
     } 
    } 
} 

为什么它不与Exception类的工作,并与它的子类的工作?

注: - 我在错误代码得到的错误是未处理的异常类型异常

+0

当你在这里提出一个问题,请随时包括你所看到的到底是什么错误。 – nhouser9

+0

在我看来,您需要阅读有关已检查和未经检查的例外情况。 –

回答

1
  1. 运行时异常延长RuntimeException。他们不必被处理或宣布。 它们可以由程序员或JVM引发。

  2. 检查异常在其层次结构中有Exception,但不包含RuntimeException。他们 必须处理或宣布。它们可以由程序员或JVM引发。

  3. Errors扩展Error类。它们由JVM抛出,不应该被处理或者声明为 。

当方法抛出检查异常(1)时,您应该处理或重新发出它。

当方法抛出未经检查的异常(2)时,您可以处理或重新抛出它,但它不是强制性的。

但每当我把它作为Exception类,我总是在 错误catch块(其中我重新把它扔在主处理)

这意味着,你的方法是抛出应该处理或重新抛出的检查异常。

处理:

public class Main { 

    public static void main(String[] args) { 
     try { 
      throw new Exception(); 
     } catch (Exception e) { 
      try { 
       throw new Exception(); 
      } catch (Exception e1) { 
       e1.printStackTrace(); 
      } 
     } 
    } 
} 

重新抛出:

public class Main { 

    public static void main(String[] args) throws Exception { 
     try { 
      throw new Exception(); 
     } catch (Exception e) { 
      throw new Exception(); 
     } 
    } 
} 

你的情况:

// You declaring that the caller should handle exception 
static void renderWorld() throws Exception { 
     try { 
      if(!enable3dRendering) { 
       System.out.println("3d rendering is disabled. Enable 3d mode to render."); 
       throw new Exception("3d mode Disabled."); 
      } else { 
       System.out.println("The World is Empty!"); 
      } 
     } catch(Exception e) { 
      System.out.println("Please handle the error"); 
      // You cannot just throw uncheked exception here 
      // You should handle it yourself or a caller should do it 
      throw e; 
     } 
} 
0

更改

static void renderWorld() { ... } 

static void renderWorld() throws Exception { ... } 

应该解决这个问题。这是因为运行时异常是未经检查的异常。

建议您阅读有关Checked和Unchecked例外的详细信息 - Java: checked vs unchecked exception explanation

0

这是因为它们是几个Exception类继承自类异常。每一个都可以扔,逮住但他们分为两组:

经过选中例外:

  1. 一个未经检查的异常并不需要处理和 NullPointerException异常你尝试是来自该组,所以你不需要在技术上关心它。
  2. A 检查异常需要每次处理或者不会 编译,这些异常类似IOException

由于基座异常对象可以选中和未选中以及编译器关心它应处理每次。

如果你给它一个尝试和改变NullPointerException异常IOException异常它不会编译要么导致它是一个的Checked Exception。所以它只是随机的,你正好找到一种类型的异常你的代码可以在没有编译错误的情况下工作。

欲了解更多信息请访问我的博客文章吧: http://www.zoltanraffai.com/blog/?p=93