Solved custom exceptions?

Discussion in 'Plugin Development' started by xize, Nov 1, 2013.

Thread Status:
Not open for further replies.
  1. Offline



    I whas wondering is it possible to create custom exceptions?
    since I'm working on a small api for my plugin ManCo supply crates I like to make a custom exception when the falling block falls on a slab its not really I want to show a stacktrace though but more likely inform the player why the crate is not falling.

    but is this possible?

  2. Offline


    It is.

    1. public class ManCoCrateException extends Exception {
    2. // serial number
    3. public ManCoCrateException(String message){
    4. super(message);
    5. }

    xize likes this.
  3. Offline


  4. Offline


    And wherever you want to generate, or throw, your custom exception, just use
    1. throw(new CustomExceptionClassName(message));
  5. Offline


    Just create a class that extends Exception. Then you can just add "throw new MyException();" and catch it wherever the method is being called. You can also extend RuntimeException which is an extension of Exception that is not required to be caught using a try and a catch.

    Edit: Looks like I took to long :'(
  6. Offline


    Isn't it
    throw new ManCoCrateException("ERROR MESSAGE");?
    method() throws ManCoCrateException {
  7. Offline


    You need both. You have to show that the method has a potential to throw an error so that when it is being executed you can catch it, but you also need to throw the exception. On the other hand, if you are using a RuntimeException you do not need to include the "throws RuntimeException".

    I should note though that throw new Exception doesn't have to be called inside the method itself. It can be called through another method:

    public void testMethod() throws Exception {
    public void otherMethod() throws Exception {
      throw new Exception();
    kreashenz likes this.
Thread Status:
Not open for further replies.

Share This Page