Why does the catch block generate a compiler error?

I have the following contrived code:

class Exception
{
    public static void main(String args[])
    {

        int x = 10;
        int y = 0;

        int result;

        try{
            result = x / y;
        }
        catch(ArithmeticException e){
            System.out.println("Throwing the exception");
            throw new ArithmeticException();
        }
        catch(Exception ae){
            System.out.println("Caught the rethrown exception");
        }
    }
}

The first catch block restores the exception that was detected. However, the compiler says "Incompatible types are required" and "Throwable". Why did this error occur?

+4
source share
4 answers

Just change the class name from Exception to Exception1.

The conflict seems to conflict with the name.

The strategy for your naming convention is determined by the Java Identifier Definition Rule.

So you cannot give a name that is already set by the library. here, the name of the class.

+10
source

Exception java.lang.Exception, . , java.lang.Exception.

Exception java.lang.Exception

catch(java.lang.Exception ae)

, java.lang.Exception

+3

Exception .

,

Java, .

, Java. , .

0

Source: https://habr.com/ru/post/1617559/


All Articles