小编Thu*_*wat的帖子

区分相同类型的异常

例如,类型的异常:java.net.BindException可以抛出"已在使用的地址"(尝试绑定另一个程序使用的端口)或"权限被拒绝"(您没有root权限来打开此端口号).我不拥有抛出BindException的类.

那么,区分具有相同类型的这些"不同"异常的最佳实践是什么?

我这样做,但我不知道是否是最佳做法:

try {
   //...some scary stuffs here  
 }
catch (BindException e){
            if (e.getMessage().contentEquals("Permission denied")){
                System.out.println("ERROR**You must be ROOT to bind that port address TCP:"+defaultPort);
            }
            else if (e.getMessage().contentEquals("Address already in use")){
                System.out.println("ERROR**Port TCP:"+defaultPort+" already in use by onother application");
            }
            e.printStackTrace();
        }
Run Code Online (Sandbox Code Playgroud)

java exception

7
推荐指数
1
解决办法
423
查看次数

如何在指定的语言环境中使用 Calendar.getInstance

我正在尝试Calendar.getInstance(Locale l)与指定一起使用但Locale无法正常工作。我无法弄清楚我做错了什么。

Java 文档。说:

getInstance public static Calendar getInstance(Locale aLocale) 获取使用默认时区和指定区域设置的日历。返回的日历基于具有给定语言环境的默认时区中的当前时间。 参数: aLocale - 周数据的区域设置 返回: 日历。

我的代码:

 public static void main (String[] args){

     Locale local = new Locale("pt", "BR");

     Calendar c = Calendar.getInstance(local); // here I am using the method
     System.out.println(c.getTime()); // and here, I cannot figure out why is not working


     DateFormat dt = DateFormat.getDateInstance(DateFormat.LONG, local);
     String s = dt.format(c.getTime());
     System.out.println(s); // here just a example in portuguese Brasil
 }
Run Code Online (Sandbox Code Playgroud)

输出:

2015 年 …

java calendar

4
推荐指数
1
解决办法
1万
查看次数

标签 统计

java ×2

calendar ×1

exception ×1