首页 文章

从java中的Exception类中抛出异常

提问于
浏览
1

好的...所以我正在学习java中的异常,我目前正在使用throw语句 . 抛出Exception类的异常,然后再次从catch块中重新抛出它以在main函数中处理它 . 但每当我把它作为Exception类抛出时,我总是在catch块中得到一个错误(在那里我重新抛出它以便在main中处理) . 但是一旦我改变了抛出并捕获了一些特殊的异常,如NullPointerException,有用!

错误代码:

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类一起使用并使用它的子类?

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

3 回答

  • 0
    • 运行时异常扩展 RuntimeException . 它们不必处理或声明 . 它们可以由程序员或JVM抛出 .

    • 已检查的异常在其层次结构中有 Exception 但不是RuntimeException . 必须处理或声明它们 . 它们可以由程序员或JVM抛出 .

    • Errors 扩展Error类 . 它们由JVM抛出,不应处理或声明 .

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

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

    但每当我把它作为Exception类抛出时,我总是在catch块中得到一个错误(我将它重新抛出以便在main中处理)

    这意味着您的方法抛出了应该处理或重新抛出的已检查异常 .

    处理:

    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 .

  • 1

    这是因为它们是几个Exception类,它们继承自Exception类 . 每一个都可以被抛弃,但它们分成两组:

    已检查和未检查的例外情况:

    • 不需要处理未经检查的异常,并且您尝试的NullPointerException来自该组,因此您不需要在技术上关注它 .

    • 每次都需要处理一个已检查的异常,否则它将无法编译,这些异常就像IOException一样 .

    由于可以检查和取消选中基本异常对象,因此编译器关心每次都应该处理它 .

    如果你试一试并将NullPointerException更改为IOException,它将无法编译,因为它是一个Checked Exception . 所以它只是随机的,你确切地找到了一种类型的Exception,你的代码可以在没有编译错误的情况下工作 .

    有关更多信息,请访问我的博客文章:http://www.zoltanraffai.com/blog/?p=93

相关问题