Need to enclose MQTTCLient Instance in try catch block

0 votes

I have recently started learning to work on various communication protocols for IoT. Given below is the line of code I can't get my head around.I try to run the code:

MqttClient Client = new MqttClient(Broker, ClientId, Persistence);

But shows an "Unhandled exception" error. So I fixed it by enclosing it with a try/catch.

 try {
        MqttClient Client = new MqttClient(Broker, ClientId, Persistence);
    } catch (MqttException e) {
        e.printStackTrace();
    }

It actually works but can't understand why the need of this. Thanks in advance.

Aug 21, 2018 in IoT (Internet of Things) by Matt
• 2,270 points
714 views

1 answer to this question.

0 votes

The instance doesn't need to be surrounded by try/catch, but the constructor invocationdoes, or else needs to be within a method that is declared to throw that exception.

Hope this helps !!

answered Aug 21, 2018 by anonymous2
• 4,280 points

Related Questions In IoT (Internet of Things)

0 votes
0 answers
0 votes
1 answer

Publishing commands to device in IBM IoT using MQTT in Java

If you are publishing from an application, ...READ MORE

answered Jul 27, 2018 in IoT (Internet of Things) by anonymous2
• 4,280 points
1,024 views
0 votes
1 answer
0 votes
1 answer

How to read data from MQTT in Eclipse Paho?

You don't read data from a MQTT ...READ MORE

answered Aug 9, 2018 in IoT (Internet of Things) by anonymous2
• 4,280 points
455 views
0 votes
1 answer

MQTT broker for Google App Engine

You had a hard time finding anything ...READ MORE

answered Aug 1, 2018 in IoT (Internet of Things) by nirvana
• 3,130 points
915 views
0 votes
1 answer

Eclipse Paho: read data from MQTT Broker

From what I could analyze, you've been ...READ MORE

answered Aug 9, 2018 in IoT (Internet of Things) by DataKing99
• 8,240 points
1,747 views
0 votes
1 answer

MQTT : Connection lost on MQTT subscriber to Internet of Things Server

An invalid topic "matteo" seems to be causing the ...READ MORE

answered Oct 3, 2018 in IoT (Internet of Things) by Annie97
• 2,160 points
3,265 views
0 votes
1 answer

Invalid MQTT publish topic on Google Cloud IoT

It's pretty simple. You have the incorrect ...READ MORE

answered Feb 27, 2019 in IoT (Internet of Things) by Upasana
• 8,620 points
1,618 views
0 votes
1 answer

How to get Unicast, Dns and Gateway Address in UWP?

Try this code Snippet I found here: https://social.msdn.microsoft.com/Forums/en-US/27a8b7a8-8071-4bc1-bbd4-e7c1fc2bd8d7/windows-10-iot-core-how-do-you-create-a-tcp-server-and-client?forum=WindowsIoT ...READ MORE

answered Jul 17, 2018 in IoT (Internet of Things) by anonymous2
• 4,280 points
575 views
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP