When exception occur in try block, catch block will handle it. So what is responsibility of exception class? I found in exception class in Java and only saw some function to provide infomation about exception(???). What is exception class do when exception occur . And if i want to write my own MyException extend Exception, what method i should write in exception class to handle my exception

4 Answers

talex On Best Solutions
  1. What is exception class do when exception occur

  2. if i want to write my own MyException extend Exception, what method i write in exception class to handle my exception
    You don't need to implement anything, but you may want to have constructor to set message and store some relevant information.

assylias On

When some code encounters a "problem", it may create an Exception object that describes the problem and throw it to the calling code to say "things didn't go as planned for reason X".

It is the up to the calling code to handle the exception (with a try/catch block) and act appropriately. The exception doesn't say or care about what should be done next.

If you are in a situation where you want to describe a specific issue more precisely that what a "standard" exception allows, you can create your own exception. Say you sell products that can only be sold to people between 20 and 40:

throw new AgeLimitException(clientAge, 20, 40);

And the client code:

try {
} catch (AgeLimitException e) {
  showMessage("Your age is " + e.getAge() + " but you must be between " + e.minAge()
            + " and " + e.maxAge() + " to buy this product");

As you can see, the role of the AgeLimitException is simply to give information about the problem that occurred.

sanjay sharma On

We create User-Defined-Exception class to handle the upcoming exceptions , when a exception occur then it will create the Exception class object ,where we have declared what to do when do do next ,so our program flow or process going on didn't break .

Swapnil Patil On

Exception class is a type of Throwable. It only gives you an exception(an issue that breaks your flow of execution) information.

Throwable is also a class & superclass(parent) of all Error & Exception. Only objects that are instances of this class (or one of its subclasses) are thrown by the Java Virtual Machine or can be thrown by the Java throw statement. Similarly, only this class or one of its subclasses can be the argument type in a catch clause.

Custom Exception: Java provides the facility to create our own exceptions which are basically derived classes of Exception.

A Class that represents a use-defined exception

class MyException extends Exception 
    public MyException(String s) 
        // Call constructor of parent Exception 

A Class that uses above MyException

public class Main 
  // Driver Program 
  public static void main(String args[]) 
        // Throw an object of user defined exception 
        throw new MyException("My own exception"); 
    catch (MyException ex) 

        // Print the message from MyException object