📜  Java中的错误 V/s 异常

📅  最后修改于: 2022-05-13 01:55:46.787000             🧑  作者: Mango

Java中的错误 V/s 异常

在Java中,Errors 和 Exceptions 都是Java.lang.Throwable 类的子类。错误是指用户进行的非法操作,导致程序无法正常运行。在程序编译或执行之前,编程错误通常不会被发现。一些错误会阻止程序被编译或执行。因此,应该在编译和执行之前删除错误。它分为三种类型:

  • 编译时
  • 运行
  • 逻辑的

而Java中的异常是指在程序执行期间(即在运行时)发生的不希望或意外事件,它破坏了程序指令的正常流程。

现在让我们讨论各种类型的错误,以便更好地理解数组。正如标题中所讨论的,错误表示合理的应用程序不应尝试捕获的严重问题。错误是任何处理技术都无法恢复的情况。它肯定会导致程序异常终止。错误属于未经检查的类型,并且大多发生在运行时。一些错误示例是内存不足错误或系统崩溃错误。

示例 1编译时错误

Java
// Java Program to Illustrate Error
// Stack overflow error via infinite recursion
 
// Class 1
class StackOverflow {
 
    // method of this class
    public static void test(int i)
    {
        // No correct as base condition leads to
        // non-stop recursion.
        if (i == 0)
            return;
        else {
            test(i++);
        }
    }
}
 
// Class 2
// Main class
public class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
        // Testing for error by passing
        // custom integer as an argument
        StackOverflow.test(5);
    }
}


Java
// Java Program to Illustrate Run-time Errors
 
// Main class
class GFG {
 
    // Main driver method
    public static void main(String args[])
    {
 
        // Declaring and initializing numbers
        int a = 2, b = 8, c = 6;
 
        if (a > b && a > c)
            System.out.println(a
                               + " is the largest Number");
        else if (b > a && b > c)
            System.out.println(b
                               + " is the smallest Number");
 
        // The correct message should have been
        // System.out.println
        // (b+" is the largest Number"); to make logic
        else
            System.out.println(c
                               + " is the largest Number");
    }
}


Java
// Java program illustrating exception thrown
// by AritmeticExcpetion class
 
// Main class
class GFG {
 
    // main driver method
    public static void main(String[] args)
    {
        int a = 5, b = 0;
 
        // Try-catch block to check and handle exceptions
        try {
 
            // Attempting to divide by zero
            int c = a / b;
        }
        catch (ArithmeticException e) {
 
            // Displaying line number where exception occured
            // using printStackTrace() method
            e.printStackTrace();
        }
    }
}


输出:

示例 2

Java

// Java Program to Illustrate Run-time Errors
 
// Main class
class GFG {
 
    // Main driver method
    public static void main(String args[])
    {
 
        // Declaring and initializing numbers
        int a = 2, b = 8, c = 6;
 
        if (a > b && a > c)
            System.out.println(a
                               + " is the largest Number");
        else if (b > a && b > c)
            System.out.println(b
                               + " is the smallest Number");
 
        // The correct message should have been
        // System.out.println
        // (b+" is the largest Number"); to make logic
        else
            System.out.println(c
                               + " is the largest Number");
    }
}
输出
8 is the smallest Number

现在让我们详细讨论例外情况 表示合理的应用程序可能想要捕获的条件。异常是在运行时发生的情况,可能会导致程序终止。但是它们可以使用 try、catch 和 throw 关键字来恢复。异常分为两类:

  • 已检查的异常
  • 未经检查的异常

编译器在编译时已知的已检查异常(如 IOException),而编译器在运行时已知未检查异常(如 ArrayIndexOutOfBoundException)。它主要是由程序员编写的程序引起的。

示例异常

Java

// Java program illustrating exception thrown
// by AritmeticExcpetion class
 
// Main class
class GFG {
 
    // main driver method
    public static void main(String[] args)
    {
        int a = 5, b = 0;
 
        // Try-catch block to check and handle exceptions
        try {
 
            // Attempting to divide by zero
            int c = a / b;
        }
        catch (ArithmeticException e) {
 
            // Displaying line number where exception occured
            // using printStackTrace() method
            e.printStackTrace();
        }
    }
}

输出:

最后,现在通过以表格格式绘制它们之间的差异来结束本文,如下所示:

ErrorsExceptions
Recovering from Error is not possible.We can recover from exceptions by either using try-catch block or throwing exceptions back to the caller.
All errors in java are unchecked type.Exceptions include both checked as well as unchecked type.
Errors are mostly caused by the environment in which program is running.Program itself is responsible for causing exceptions.

Errors can occur at compile time as well as run time. Compile Time: eg Syntax Error

Run Time: Logical Error.

All exceptions occurs at runtime but checked exceptions are known to the compiler while unchecked are not.
They are defined in java.lang.Error package.They are defined in java.lang.Exception package
Examples : java.lang.StackOverflowError, java.lang.OutOfMemoryErrorExamples : Checked Exceptions : SQLException, IOException Unchecked Exceptions : ArrayIndexOutOfBoundException, NullPointerException, ArithmeticException.