📜  处理检查异常的Java程序

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

处理检查异常的Java程序

受检异常是 Exception 类的子类。这些类型的异常发生在程序编译期间。这些异常可以由 try-catch 块处理,否则程序将给出编译错误。


Java 中的异常

I/O 异常:该程序抛出 I/O 异常,因为 FileNotFoundException 是Java中的已检查异常。任何时候,我们想从文件系统中读取文件, Java都会强制我们处理文件不在给定位置的错误情况。

考虑 myfile.txt 文件不存在。

Java
// Java Program to Handle Checked Exception
import java.io.*;
class GFG {
    public static void main(String args[])
    {
        FileInputStream GFG
            = new FileInputStream("/home/mayur/GFG.txt");
        // this file does not exist in the location
        /* This constructor FileInputStream
         * throws FileNotFoundException which
         * is a checked exception
         */
    }
}


Java
// Java Program to Handle Checked Exception
import java.io.*;
import java.util.*;
class GFG {
    public static void main(String[] args)
        throws FileNotFoundException
    {
        FileInputStream GFG = null;
        try {
            GFG = new FileInputStream(
                "/home/mayur/GFG.txt");
        }
        catch (FileNotFoundException e) {
            System.out.println("File does not exist");
        }
    }
}


Java
// Java Program to Handle Checked Exception
import java.io.*;
class GFG {
    public static void main(String[] args)
    {
        Class temp = Class.forName("gfg");
        // Calling the clas gfg which is not present in the
        // current class temp instance of calling class it
        // will throw ClassNotFoundException;
    }
}


Java
// Java Program to Handle Checked Exception
import java.io.*;
class GFG {
  
    public static void main(String[] args)
        throws ClassNotFoundException
    {
  
        try {
            Class temp = Class.forName(
                "gfg"); // calling the gfg class
        }
        catch (ClassNotFoundException e) {
            // block executes when mention exception occur
            System.out.println(
                "Class does not exist check the name of the class");
        }
    }
}


输出:

处理 FileNotFoundException:

可以在 try-catch 块的帮助下处理此异常

  • 声明函数使用 throw关键字以避免编译错误。
  • 所有异常都在发生时抛出对象 try 语句允许您定义要测试错误的代码块,并且 catch 块捕获给定的异常对象并执行所需的操作。
  • 将显示使用 try-catch 块定义的输出。

Java

// Java Program to Handle Checked Exception
import java.io.*;
import java.util.*;
class GFG {
    public static void main(String[] args)
        throws FileNotFoundException
    {
        FileInputStream GFG = null;
        try {
            GFG = new FileInputStream(
                "/home/mayur/GFG.txt");
        }
        catch (FileNotFoundException e) {
            System.out.println("File does not exist");
        }
    }
}

输出:

类未找到异常:

当 Class.forName() 和 LoadClass Method 等方法无法找到给定的类名作为参数时,会发生此异常。

Java

// Java Program to Handle Checked Exception
import java.io.*;
class GFG {
    public static void main(String[] args)
    {
        Class temp = Class.forName("gfg");
        // Calling the clas gfg which is not present in the
        // current class temp instance of calling class it
        // will throw ClassNotFoundException;
    }
}

输出:

处理 ClassNotFoundException:使用 try-Catch 块

Java

// Java Program to Handle Checked Exception
import java.io.*;
class GFG {
  
    public static void main(String[] args)
        throws ClassNotFoundException
    {
  
        try {
            Class temp = Class.forName(
                "gfg"); // calling the gfg class
        }
        catch (ClassNotFoundException e) {
            // block executes when mention exception occur
            System.out.println(
                "Class does not exist check the name of the class");
        }
    }
}

输出: