Solved custom exceptions?

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

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

    xize

    Hello,

    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?

    thanks:)
     
  2. Offline

    kreashenz

    It is.

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

    Done.
     
    xize likes this.
  3. Offline

    xize

  4. Offline

    amhokies

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

    Phinary

    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

    kreashenz

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

    Phinary

    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:


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

Share This Page