1

I want to create user-defined exception with addition but I get a compilation error.

class Test{

    public static void main(String[] args){

        int a=9, b=67, result;
        result = a+b;
        throw new Add("Addition has been performed");
    }
}
class Add extends Throwable{
    Add(){
        printStackTrace();
    }
    Add(String message){
        System.out.println(message);
        printStackTrace();  
    }
}

The error is:

Practice.java:8: error: unreported exception Add; must be caught or declared to be thrown
                throw new Add("Addition has been performed");
Stephen C
  • 698,415
  • 94
  • 811
  • 1,216

6 Answers6

1

I would point out that irrespective of the compilation error, and the possible fixes, it is a bad idea to extend Throwable.

  • It is unnecessary. You can / should extend Exception or RuntimeException or any of their subclasses.

  • There is a lot of code that assumes that all exceptions are Exception or Error or their subtypes, not some "random" subtype of Throwable.

  • The generic meaning of a random subclass of Throwable is unclear; e.g. see Which subclass of Throwable should be caught and which shouldn't?

See also the discussion in this Q&A:


It is also a bad idea for an exception's constructors to be printing or logging error messages and stacktraces. It should be up to the code that catches the exception to decide what to do with them.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
0

To make your exception unchecked declare it in the following way

class Add extends RuntimeException {
} 
talex
  • 17,973
  • 3
  • 29
  • 66
0

Either catch/rethrow the Add throwable or extend it from RuntimeException - they are not mandatory to be caught.

class Add extends RuntimeException
lotor
  • 1,060
  • 7
  • 18
0

If you do not want to make your exception unchecked you need either to declare that your method might throw this exception

public static void main(String[] args) throws Add {

or surround it with try/catch statements

try {
    throw new Add("Addition has been performed");
} catch (Add add) {
    add.printStackTrace();
}

Overall I would suggest you to use an IDE they are really good at helping you with these kind of problems.

Dimitris
  • 560
  • 3
  • 17
0

You have Successfully created and throw the Exception but use Try and Catch block to handle it.

Shaik Bajivali
  • 167
  • 1
  • 16
0

First, in Java there are 2 types of exception. Checked and unchecked. Checked are exceptions thrown that you have to handle by either catching them ( surrounding the block, portion of code, containing methods that would throw an exception like yours) or declaring that the parent method calling this method throwing an exception.

Throwable is base class of all errors in Java. There are multiple types of errors, a sub type Is exceptions.

Important to not as well is that you seem to throw an exception on success which isn’t the best to do because exceptions are costly in terms of resources. Better use events in this case to broadcast that addition happened.

Ali Ben Zarrouk
  • 1,891
  • 16
  • 24