0 votes
155 views
in Java by
What is an Exception in java? Explain by an example.

2 Answers

0 votes
by (4.4k points)

Exception:- Exception is an error event that can occur during the execution of a program and it  disrupts the normal flow of program. (e.g. divided by zero, array access out of bound, etc.). In Java, an exception is an object that wraps in error event and it occurred within a method and contains information about the error including its type.

The runtime system searches the call stack for a method. It contains a block of code that can handle the exception. This block of code is called an exception handler.

There are following reasons to occured an exception:-

  • Users can entered invalid data.
  • A network connection has been lost in the middle of communications or the JVM has run out of memory.
  • To open file but file can not be found.

Exception handling class hierarchy:-

Exception class hierarchy

0 votes
by (4.4k points)

See the following example for java exception (Runtime Exception):-

package exceptions;

public class ExcepTest {

    /**
     * @param args
     */
    public static void main(String[] args) {
        try{
            int num = 10/0;
        }catch(RuntimeException ue){
            ue.printStackTrace();
        }
    }

}
Output:-

java.lang.ArithmeticException: / by zero
    at exceptions.ExcepTest.main(ExcepTest.java:10)

Share:- Whatsapp Facebook Facebook


Welcome to Developerhelpway Q&A, where you can ask questions and receive answers from other members of the community.

Categories

...