📜  处理未检查异常的Java程序

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

处理未检查异常的Java程序

异常是在运行时出现的问题,导致程序工作的突然流程。记住异常永远不会在编译时抛出,而总是在运行时抛出,无论是任何类型。编译时不会抛出异常。 Throwable 也是所有异常和错误的超类。现在迫切需要处理它们,在Java语言中定义了一个概念,称为“异常处理技术”

有两种类型的异常定义如下

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

真实世界的插图:例外

方法:现在,为了处理异常,提出的概念是异常处理技术。直接潜入 未检查异常的概念。

未经检查的异常

这些类型的异常发生在程序运行期间。这些是编译器在编译时未检查的异常。在Java中 Error 和 Runtime Exception 类下的异常是未经检查的异常,此异常是由于错误的编程而发生的。

  1. Errors 类StackOverflow、OutOfMemoryError 异常等异常难以处理
  2. 可以在 try-Catch 块的帮助下处理 IndexoutOfBoundException、Nullpointer Exception 等运行时异常

程序员通常会遇到两种主要的未检查异常,即下面将在示例实现的帮助下讨论它们,并讨论如何处理它们。 两种主要方法都提出如下:

  1. 索引出界异常
  2. 空指针异常

情况 1: (Array)IndexoutOfBoundException 由于访问的索引大于等于数组长度的大小而发生此异常。发生此异常后,程序将自动终止。简而言之,就是试图访问当前数据结构本身没有保存的内存。这里这个异常是在数据结构上定义的,即“数组”。

Java
// Importing Classes/Files
import java.io.*;
  
class GFG {
  
    // Main Driver Function
    public static void main(String[] args)
    {
        // Array containing 4 elements
        int a[] = { 1, 2, 3, 4 };
  
        // Try to access elements greater than
        // index size of the array
        System.out.println(a[5]);
    }
}


Java
// Importing Classes/Files
import java.io.*;
  
public class GFG {
  
    // Main Driver Method
    public static void main(String[] args)
    {
        // Inserting elements into Array
        int a[] = { 1, 2, 3, 4, 5 };
  
        // Try block for exceptions
        try {
  
            // Forcefully trying to access and print
            // element/s beyond indexes of the array
  
            System.out.println(a[5]);
        }
  
        // Catch block for catching exceptions
        catch (ArrayIndexOutOfBoundsException e) {
  
            // Printing display message when index not
            // present in a array is accessed
            System.out.println(
                "Out of index  please check your code");
        }
    }
}


Java
// Importing Classes/Files
import java.io.*;
  
public class GFG {
    // Main Driver Method
    public static void main(String[] args)
    {
  
        // Instance of string a has null value
        String a = null;
  
        // Comparing null value with the string value
        // throw exception and Print
        System.out.println(a.equals("GFG"));
    }
}


Java
// Importing Files/Classes
import java.io.*;
  
public class GFG {
  
    // Driver Main Method
    public static void main(String[] args)
    {
        // Assigning NULL to string
        String m = null;
  
        // Try-Catch Block
        try {
            
            // Checking the null value with GFG string
            // and throw exception
            if (m.equals("GFG")) {
                // Print String
                System.out.println("YES");
            }
        }
  
        // Try-Catch Block
        catch (NullPointerException e) {
  
            // Handles the exception
            System.out.println(
                "Object reference cannot be null");
        }
    }
}


输出:

处理 ArrayIndexoutOfBoundException : Try-catch Block 我们可以处理这个异常try 语句允许您定义要测试错误的代码块,并且catch 块捕获给定的异常对象并执行所需的操作程序不会终止。

Java

// Importing Classes/Files
import java.io.*;
  
public class GFG {
  
    // Main Driver Method
    public static void main(String[] args)
    {
        // Inserting elements into Array
        int a[] = { 1, 2, 3, 4, 5 };
  
        // Try block for exceptions
        try {
  
            // Forcefully trying to access and print
            // element/s beyond indexes of the array
  
            System.out.println(a[5]);
        }
  
        // Catch block for catching exceptions
        catch (ArrayIndexOutOfBoundsException e) {
  
            // Printing display message when index not
            // present in a array is accessed
            System.out.println(
                "Out of index  please check your code");
        }
    }
}

输出:

情况 2:空指针异常: 尝试访问具有空值的对象引用时会发生此异常。

Java

// Importing Classes/Files
import java.io.*;
  
public class GFG {
    // Main Driver Method
    public static void main(String[] args)
    {
  
        // Instance of string a has null value
        String a = null;
  
        // Comparing null value with the string value
        // throw exception and Print
        System.out.println(a.equals("GFG"));
    }
}

输出:

NullPointerException的处理技术

Java

// Importing Files/Classes
import java.io.*;
  
public class GFG {
  
    // Driver Main Method
    public static void main(String[] args)
    {
        // Assigning NULL to string
        String m = null;
  
        // Try-Catch Block
        try {
            
            // Checking the null value with GFG string
            // and throw exception
            if (m.equals("GFG")) {
                // Print String
                System.out.println("YES");
            }
        }
  
        // Try-Catch Block
        catch (NullPointerException e) {
  
            // Handles the exception
            System.out.println(
                "Object reference cannot be null");
        }
    }
}

输出: