0

I am making a Discord JDA bot that can when a user sends the message: Prefix("$") + hastebin + their code, the bot will create a request to hastebin and paste their code, after that he will take the paste URL and print it to the console(I will send it as a message after I solve the problem).

This is my HastebinCommand class:

package events;
import main.Hastebin;

import Info.Info;
import net.dv8tion.jda.core.events.message.guild.GuildMessageReceivedEvent;
import net.dv8tion.jda.core.hooks.ListenerAdapter;

import java.lang.*;

public class HastebinCommand extends ListenerAdapter
{
    Info info;
    Hastebin hastebin;
    @Override
    public void onGuildMessageReceived(GuildMessageReceivedEvent event)
    {
        String[] message = event.getMessage().getContentRaw().split(" ");

    if (message[0].equalsIgnoreCase(info.prefix + "hastebin") || message[0].equalsIgnoreCase(info.prefix + "haste"))
    {
        if (message.length == 1)
        {
            //Send an error message
        }
        else
        {
            String code = "";
            for (int i = 1; i < message.length; i++)
            {
                code = code + "" + message[i];
            }
            System.out.println(hastebin.paste(code));
        }
    }
}
}

This is my Hastebin request class:

package main;

import org.json.JSONObject;

import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;


public class Hastebin
{

    public static String paste(String content) throws Exception{
    final HttpClient client = HttpClient.newHttpClient();
    final HttpRequest request = HttpRequest.newBuilder(URI.create("https://hastebin.com/documents"))
            .POST(HttpRequest.BodyPublishers.ofString(content)).build();

    final HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());

    final String responseContent = response.body();
    final JSONObject responseJson = new JSONObject(responseContent);
    final String key = responseJson.getString("key");
    return "https://hastebin.com/" + key;
}
}

My error:

C:\Users\user\Documents\Java\Java Projects\DiscordJDA\SpoonfeedingBot\src\events\HastebinCommand.java Error:(32, 50) java: unreported exception java.lang.Exception; must be caught or declared to be thrown

I would really appreciate getting help after trying to solve my problem for so long.

Minn
  • 5,688
  • 2
  • 15
  • 42
Tal Moshel
  • 232
  • 3
  • 18
  • 2
    `throws java.lang.Exception` indicates that `parse` throws a **checked exception** which must be handled by a `try/catch` when calling `parse`. See https://stackoverflow.com/questions/6115896/java-checked-vs-unchecked-exception-explanation – Minn Dec 04 '18 at 18:47
  • 2
    Possible duplicate of [Java: checked vs unchecked exception explanation](https://stackoverflow.com/questions/6115896/java-checked-vs-unchecked-exception-explanation) – Minn Dec 04 '18 at 18:47

1 Answers1

0

Some function you're using is throwing an exception that must be dealt with (checked exception). Whenever you have a problem with an exception you should google the exception first. For example, in this case, you could paste "unreported exception java.lang.Exception; must be caught or declared to be thrown" in google search engine and you'd get your explanation.

Here's a tutorial on Exceptions: https://www.tutorialspoint.com/java/java_exceptions.htm

Also, your StackOverflow post should be about the error itself, in other words "unreported exception java.lang.Exception".

KON
  • 91
  • 12