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
It is. Code:java public class ManCoCrateException extends Exception {// serial numberpublic ManCoCrateException(String message){ super(message);} Done.
And wherever you want to generate, or throw, your custom exception, just use Code:java throw(new CustomExceptionClassName(message));
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
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(); }