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,260 points
73 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,260 points

Related Questions In IoT (Internet of Things)

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,260 points
412 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,260 points
74 views
0 votes
1 answer

Using FIWARE to start Cygnus as a Service in IoT Solution

Cygnus is supposed to create /var/run/cygnus/ when started. You ...READ MORE

answered Aug 13, 2018 in IoT (Internet of Things) by anonymous2
• 4,260 points
77 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,060 points
206 views
0 votes
1 answer

Eclipse Paho: read data from MQTT Broker

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

answered Aug 8, 2018 in IoT (Internet of Things) by DataKing99
• 8,130 points
600 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,190 points
434 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 26 in IoT (Internet of Things) by Upasana
• 8,530 points
191 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,260 points
135 views