Exception in java

krishbcs

krishbcs

@krishbcs-5tG26c Oct 22, 2024
Exception has two types. Unchecked Exception and Checked Exception.

IOException
ClassNotFoundException
CloneNotSupportedException
IllegalAccessException
InterruptedException
NoSuchFieldException
NoSuchMethodException

These are Checked Exception.

Can you explain each exception with simple example(Error program and correction program)?

Replies

Welcome, guest

Join CrazyEngineers to reply, ask questions, and participate in conversations.

CrazyEngineers powered by Jatra Community Platform

  • Anoop Kumar

    Anoop Kumar

    @anoop-kumar-GDGRCn Feb 22, 2012

    Checked Exception: You must have to handle those exceptions by yourself using try-catch or throws clause
    Unckecked: JRE will take care of these things

    Why Checked Exception: In case of IOException, for example you wrote a program to read a file. How can java know if file is there to read? that is why it insist you to catch this exception by yourself.
    Java is telling you that somewhere it could be wrong.

    Rest, explaining each exception will be a theory , I would suggest you to write programs in which these exception can occur. If will give you live practical example.
  • krishbcs

    krishbcs

    @krishbcs-5tG26c Feb 23, 2012

    ianoop
    Checked Exception: You must have to handle those exceptions by yourself using try-catch or throws clause
    Unckecked: JRE will take care of these things

    Why Checked Exception: In case of IOException, for example you wrote a program to read a file. How can java know if file is there to read? that is why it insist you to catch this exception by yourself.
    Java is telling you that somewhere it could be wrong.

    Rest, explaining each exception will be a theory , I would suggest you to write programs in which these exception can occur. If will give you live practical example.
    thanks but i need detail each exception with examples.
  • krishbcs

    krishbcs

    @krishbcs-5tG26c Feb 23, 2012

    for example:


    ArithmeticException:

    Error program:

    class ArithException
    {
    public static void main(String arg[])
    {
    System.out.println(2/0);
    }
    }

    Output: ArithmeticException Error.


    Correction Program:

    class ArithException
    {
    public static void main(String arg[])
    {
    try{
    System.out.println(2/0);
    }catch(ArithmeticException)
    { System.out.println("divide by zero is not valued");
    }
    }

    Ouptut: Divide by zero is not Valued.


    I need like this for every exception.

    would you help me?