0

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

NAM
  • 107
  • 3
  • 10
  • 1
    To create your own exception you need to invoke the constructor of `Exception`. See [How to define custom exception class in Java, the easiest way?](https://stackoverflow.com/questions/3776327/how-to-define-custom-exception-class-in-java-the-easiest-way) – Sudhir Ojha May 10 '19 at 10:09
  • In the ideal word we log exception information somewhere using any logging framework so we can use them to troubleshoot some issues. – Dhruv Kapatel May 10 '19 at 15:25

4 Answers4

2
  1. What is exception class do when exception occur
    Nothing.

  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.

talex
  • 17,973
  • 3
  • 29
  • 66
  • so with FileNotFoundException and ParseException, if i exchange each other, program would working properly (except info it provides), right? – NAM May 10 '19 at 10:17
  • What do you mean by exchange? If you throw one instead of another? You will break the code because if it catches one exception, doesn't mean it catch another. – talex May 10 '19 at 10:19
  • in catch(FileNotFoundException e){ //some code...}, i change to catch( ParseException e){ //some code...} with some code the same in catch. how it would? – NAM May 10 '19 at 10:24
  • You don't understand what `catch` clause do? It catch specific exception, if you change exception it stops catching old one and starts catching new. – talex May 10 '19 at 10:26
1

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 {
  buyProduct();
} 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.

assylias
  • 321,522
  • 82
  • 660
  • 783
0

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 .

0

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 
        super(s); 
    } 
} 

A Class that uses above MyException

public class Main 
{ 
  // Driver Program 
  public static void main(String args[]) 
  { 
    try
    { 
        // Throw an object of user defined exception 
        throw new MyException("My own exception"); 
    } 
    catch (MyException ex) 
    { 
        System.out.println("Caught"); 

        // Print the message from MyException object 
        System.out.println(ex.getMessage()); 
    } 
  } 
} 
Swapnil Patil
  • 613
  • 6
  • 23