java throwable_Java Throwable getLocalizedMessage()用法及代码示例

Throwable类的getLocalizedMessage()方法用于在发生异常时获取Throwable对象的locale-specific描述。它有助于我们根据本地特定消息修改Throwable对象的描述。对于不覆盖此方法的子类,此方法的默认实现返回与getMessage()相同的结果。

用法:

public String getLocalizedMessage()

返回值:发生异常时,此方法返回Throwable对象的locale-specific描述。

下面的程序演示了Throwable类的getLocalizedMessage()方法:

示例1:

// Java program to demonstrate

// the getLocalizedMessage() Method.

import java.io.*;

class GFG {

// Main Method

public static void main(String[] args)

throws Exception

{

try {

// add the numbers

addPositiveNumbers(2, -1);

}

catch (Exception e) {

System.out.println("LocalizedMessage = "

+ e.getLocalizedMessage());

}

}

// method which adds two positive number

public static void addPositiveNumbers(int a, int b)

throws Exception

{

if (a < 0 || b < 0) {

throw new Exception("Numbers are not Positive");

}

else {

System.out.println(a + b);

}

}

}

输出:

LocalizedMessage = Numbers are not Positive

示例2:

// Java program to demonstrate

// the ensureCapacity() Method.

import java.io.*;

class GFG {

// Main Method

public static void main(String[] args)

throws Exception

{

try {

testException();

}

catch (Throwable e) {

System.out.println("LocalizedMessage of Exception : "

+ e.getLocalizedMessage());

}

}

// method which throws IndexOutOfBoundsException

public static void testException()

throws IndexOutOfBoundsException

{

throw new IndexOutOfBoundsException(

"Forcefully Generated Exception");

}

}

输出:

LocalizedMessage of Exception : Forcefully Generated Exception

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java MQTT示例代码可以使用Eclipse Paho客户端库来实现,以下是一个简单的示例: ```java import org.eclipse.paho.client.mqttv3.*; import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence; public class MqttSubscriber { public static void main(String[] args) { String topic = "test/topic"; int qos = 2; String broker = "tcp://iot.eclipse.org:1883"; String clientId = "JavaSampleSubscriber"; MemoryPersistence persistence = new MemoryPersistence(); try { MqttClient mqttClient = new MqttClient(broker, clientId, persistence); mqttClient.setCallback(new MqttCallback() { public void messageArrived(String topic, MqttMessage message) throws Exception { System.out.println("Received message: " + new String(message.getPayload())); } public void deliveryComplete(IMqttDeliveryToken token) { // not used in this example } public void connectionLost(Throwable cause) { System.out.println("Connection lost: " + cause.getMessage()); } }); mqttClient.connect(); System.out.println("Connected to broker: " + broker); mqttClient.subscribe(topic, qos); System.out.println("Subscribed to topic: " + topic); } catch (MqttException me) { System.out.println("reason: " + me.getReasonCode()); System.out.println("msg: " + me.getMessage()); System.out.println("loc: " + me.getLocalizedMessage()); System.out.println("cause: " + me.getCause()); System.out.println("excep: " + me); } } } ``` 此示例订阅了一个主题并打印接收到的消息。您需要确保在运行代码之前已经安装Eclipse Paho客户端库。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值