Custom Error Measage

Discussion in 'Spigot Plugin Development' started by eickmung, May 20, 2019.

  1. How to create Custom Error Message?
  2. Hello,

    Can you be more precise please? What kind of error? For what?
    You can use the logger to display any error you want.
  3. What kind of error are you talking about?
    You're able to create custom Exception in Java
    Code (Java):
    if (true) // hack the compiler
        throw new Exception("Your custom message");

    // or just extend Exception
    class CustomException extends Exception {
        // your code
    UPD: more errors
    Code (Java):
    // Just display error into standard output (console)
    System.err.printf("Devide by zero!1!!\n");

    // Send error message to player
    player.sendMessage("\u00a7cYou cannot devide by zero!");

    // Send colored error into Bukkit console
        "\u00a7cYou cannot devide by zero!"

    // or send warning to console (code from your main JavaPlugin class)
    super.getLogger().warning("You cannot devide by zero!");
    #3 NyanGuyMF, May 20, 2019
    Last edited: May 20, 2019
  4. What I want is something like an event error, NullPointer, NoSuchMethod I want to make a custom message where the class is and how many times I get an error in the line.
  5. You may throw an Exception with needed message, as I mentioned above
    Code (Java):
    throw new NullPointerException("Player not found!");
    #5 NyanGuyMF, May 20, 2019
    Last edited: May 20, 2019
  6. I think you've made yourself clear with your first reply.

    You can throw errors yourself using throw new SomeError("Some message if you wish");

    Code (Java):
    String test = "Johny!";
    if (test.contains("!")){
    throw new NullPointerException("The string you set can't have a !");
    This is purely an example and should not be copied at any case. The example usage is wrong but the idea is there.
  7. i don't need it.
  8. So what do you need? :D

    If it's possible — show us some code, where you want to place your message
  9. You can make an error message by just sending the console a message if thats what you meant, it can have colors and everything..
    Code (Java):
    Server server = Bukkit.getServer();
    ConsoleSender cs = server.getConsoleSender();

    //use this to send a message wherever you like
    cs.sendMessage(ChatColor.RED + " My Error Message Here");
    If you dont want that, you can create an exception using this
    Code (Java):
    package me.astronomize.myplugin;

    public class TestException extends Exception {

          public TestException(String message) {
    And then send it using...
    Code (Java):
    throw new TestException("Your Message Here!");
  10. You can make also exceptions with no stacktrace element (such as this).

Share This Page