2

I was seaching this problem in other questions but still didn't figure out what does exactly mean Throwable. Read a few articles about it (its superclas bla bla bla) but still don't know how to implement it. Forgot to mention i am new to java. Any advice would be gratefully accepted :D

Here is the Exception class:

class Exception {
private int pDen;
private int pMes;
private int kDen;
private int kMes;

public Exception(int pDen, int pMes, int kDen, int kMes) {
    super();
    this.pDen = pDen;
    this.pMes = pMes;
    this.kDen = kDen;
    this.kMes = kMes;
}
public void message()
{
    System.out.println("Isklucok");
}
public void promena()
{
    int tmpDen = 0;
    int tmpMes = 0;
    tmpDen = pDen;
    pDen = kDen;
    kDen = tmpDen;
    tmpMes = pMes;
    pMes = kMes;
    kMes = tmpMes;
}

}

And here is the code that i run in other class where shoud i catch my exception where is thrown.

try {
        if(pMes > kMes)
        {
            throw new Exception(pDen,pMes,kDen,kMes);
        }
        else if(pMes == kMes)
            if(pDen > kDen)
            {
                throw new Exception(pDen,pMes,kDen,kMes);
            }
    }
    catch(Exception e)
    {
        e.message();
        e.promena();
    }
VL4DiMiRG
  • 67
  • 1
  • 2
  • 8

7 Answers7

6

Make your custom exception extends something from the Throwable hierarchy.

For example

// Exception here is java.lang.Exception, not the class from your example
public class MyException extends Exception {

    // ...

}
3

The class Exception and its subclasses are a form of Throwable that indicates conditions that a reasonable application might want to catch.

In most situations your exception would be one of them below:

  • Arithmetic Exception
  • ArrayIndexOutOfBoundException
  • ClassNotFoundException
  • IOException
  • NoSuchMethodException ...

An Error is a subclass of Throwable that indicates serious problems that a reasonable application should not try to catch.

Both Exception and Error are subclasses of Throwable.

If you need to not only catch Exceptions but Errors, you should use Throwable. But:

Most such errors are abnormal conditions. The ThreadDeath error, though a "normal" condition, is also a subclass of Error because most applications should not try to catch it.

If you want to catch such a thing like ThreadDeath error you would use Throwable. That might be a very rare situation.

In your example if you simply want to check the numbers, you can use something like this:

public static void check(int pDen, int pMes, int kDen, int kMes) {

    if (pMes > kMes) {
        throw new IllegalArgumentException ("Your message.....");
    } else if (pMes == kMes) {
        if (pDen > kDen) {
            throw new IllegalArgumentException ("Your message.....");
        }
    }
}
buræquete
  • 14,226
  • 4
  • 44
  • 89
Hülya
  • 3,353
  • 2
  • 12
  • 19
1

Please ignore the answers suggesting you implement your own Exception class. Java has its own: java.lang.Exception. Just use that, and extend it if needed. Cases where it makes sense to have your own Throwable, but not extend from the standard Exception are very rare.

Jorn
  • 20,612
  • 18
  • 79
  • 126
0

You'll have to create a custom Exception class containing the required Constructors as follows:

public class CustomException extends Exception {
    /* Optional: Include Serial UID */

    public CustomException(Throwable t) {
        super(t);
    }

    public CustomException(int pDen, int pMes, int kDen, int kMes) {
        /* Handle Info & Throw Exception */
    }

}

And then you can throw the exception as follows,

throw new CustomException(pDen,pMes,kDen,kMes);
user2004685
  • 9,548
  • 5
  • 37
  • 54
0

Points to avoid this common error while writing any Java program:

  1. Make sure you are not importing any package for Exception class.
  2. If you find any import for this remove it.
  3. You can implement by extending Exception class.
  4. Or by throwing an Exception from the method.
  5. Use throw when creating a custom message to be thrown along with throws keyword on method.
pappbence96
  • 1,164
  • 2
  • 12
  • 20
Nahid
  • 98
  • 7
-1

In your custom class extends Throwable. Its working

 class Exception extends Throwable {
      //your code
  }
sasikumar
  • 12,540
  • 3
  • 28
  • 48
-1

I had this exception when I hadn't imported the Exception type into the class that was catching it

simonalexander2005
  • 4,338
  • 4
  • 48
  • 92