我正在尝试抛出自定义异常.
自定义异常类的实现是:
case class customException(smth:String) extends Exception
Run Code Online (Sandbox Code Playgroud)
在我的代码中,我包装了一段代码,我肯定会抛出一个异常,使用try/catch来抛出我的customException.
try{
val stateCapitals = Map(
"Alabama" -> "Montgomery",
"Alaska" -> "Juneau",
"Wyoming" -> "Cheyenne")
println("Alabama: " + stateCapitals.get("AlabamaA").get)
}
catch{
case x:Exception=>throw classOf[CustomException]
}
Run Code Online (Sandbox Code Playgroud)
我收到一个编译错误,说:
found : java.lang.Class[CustomException]
[INFO] required: java.lang.Throwable
[INFO] case x:Exception=>throw classOf[CustomException]
Run Code Online (Sandbox Code Playgroud)
我怎么能在这种情况下抛出我自己的自定义异常?后来我正在检查抛出的异常是否是类型[x]来执行特定的操作.
我有一个日期变量
var date: Date = new Date()
Run Code Online (Sandbox Code Playgroud)
然后我将这个日期转换为String:
var dateStr = date.toString()
Run Code Online (Sandbox Code Playgroud)
现在我需要将此String转换回日期.我试过了两个:
1:
var stringToDate: Date = date2Str.asInstanceOf[Date]
Run Code Online (Sandbox Code Playgroud)
和2:
stringToDate: Date = new SimpleDateFormat("dd.MM.yyyy").parse(dateStr);
Run Code Online (Sandbox Code Playgroud)
但在这两种情况下我都收到了错误:
java.lang.ClassCastException:
java.lang.String cannot be cast to java.util.Date
Run Code Online (Sandbox Code Playgroud) 我有一个自定义异常类,如下所示:
case class CustomException(errorMsg:String) extends Exception(error:String)
Run Code Online (Sandbox Code Playgroud)
当我捕获异常时,我需要的是抛出我的自定义异常并将我的错误消息传递给自定义异常.我期望从CustomException构造函数调用super(errMsg)但是,这不是现在的情况,我得到了一个编译错误.
catch {
case s: Exception => throw CustomException("This is a custom message")
}
Run Code Online (Sandbox Code Playgroud)
我怎么能调用超级构造函数:
super(errorMessage)
Run Code Online (Sandbox Code Playgroud) 我有以下.proto文件:
enum Enum1{
X=0;
Y=1;
}
message SomeClass{
required Enum1 enum1=1;
required Enum2 enum2=2;
}
enum Enum2{
X=0;
Z=1;
}
Run Code Online (Sandbox Code Playgroud)
当我尝试使用protoc来编译它时,我收到以下错误:
proto.proto:19:5:"X"已定义为proto.proto:19:5:请注意,枚举值使用C++作用域规则,这意味着枚举值是其类型的兄弟,而不是它的子类.因此,"X"必须是唯一的,而不仅仅是"Enum2".
我有办法克服这个问题!
我有JSON喜欢:
{
"_id" : "1",
"_class" : "com.model.Test",
"itemList" : [
{
"itemID" : "1",
"itemName" : "Foo",
"resources" : [
{
"resourceID" : "1",
"resourceName" : "Foo Test1"
}, {
"resourceID" : "2",
"resourceName" : "Foo Test2"
}
]
}
]
}
Run Code Online (Sandbox Code Playgroud)
我需要能够删除itemList的记录之一.我做了以下事情:
public void removeItemByID(String docID, String itemID) throws Exception {
MongoOperations mongoOperations = mongoConfiguration.getMongoTemplate();
Query query = new Query(where("_id").is(docID).and("itemList.itemID").is(itemID));
mongoOperations.remove(query, Item.class);
Run Code Online (Sandbox Code Playgroud)
}
这种方法不起作用.但是,当我使用带有$ pull方法的BasicDBObject时,它工作正常!这些方法有什么区别!
我需要实现一个带有元组的泛型方法并返回一个Map示例:
val tuple=((1,2),(("A","B"),("C",3)),4)
Run Code Online (Sandbox Code Playgroud)
我一直试图将这个元组分解为一个列表:
val list=tuple.productIterator.toList
Scala>list: List[Any] = List((1,2), ((A,B),(C,3)), 4)
Run Code Online (Sandbox Code Playgroud)
但这种方式会返回List [Any].
我现在正在尝试找出如何迭代以下元组,例如:
((1,2),(("A","B"),("C",3)),4)
Run Code Online (Sandbox Code Playgroud)
为了遍历每个元素1,2,"A",B",...等.我怎么能在元组上进行这种迭代
我试着写一个原型文件,它有一个Date字段,它没有被定义为Protocol buffer的类型.
我已阅读以下文章,但我无法找到适合我的正确解决方案: 使用协议缓冲区的小数和日期时间的最佳方法是什么?.
我正在尝试将proto文件转换为java.
我使用Spring 3,基于java的配置,使用BootStrap.
我已经下载了bootstrap并将css和js放在resources目录下.
我不能在freemarker页面中使用这些.css的问题.但是我进口了它们.当我使用基于java的配置时,我添加了"addResourceHandler",如下所示:
WebAppConfig:
@Configuration
@EnableWebMvc
@ComponentScan("com.springway")
public class WebConfig implements WebApplicationInitializer {
@Override
public void onStartup(final ServletContext servletContext) throws ServletException {
final AnnotationConfigWebApplicationContext root = new AnnotationConfigWebApplicationContext();
root.setServletContext(servletContext);
root.scan("com.springway");
root.refresh();
final ServletRegistration.Dynamic servlet = servletContext.addServlet("spring", new DispatcherServlet(root));
servlet.setLoadOnStartup(1);
servlet.addMapping("/*");
}
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
}
Run Code Online (Sandbox Code Playgroud)
Tomcat日志说:
"警告:没有找到带URI的HTTP请求的映射
名为'spring'的DispatcherServlet中的[/springway/resources/css/bootstrap-responsive.css]
目录 :
-SpringWay
> -src
> - main
> -webapp
> -resources
-WEB-INF
-welcome.ftl
-springway.ftl
Run Code Online (Sandbox Code Playgroud)
welcome.ftl:
[#ftl /]
[#include "springway.ftl" /]
<ul class="breadcrumb">
<li>
<a href="[@spring.url '/test'/]">Test</a> <span …Run Code Online (Sandbox Code Playgroud) 我hava JSON对象:
[#1={id:"2012-05-04", title:"Scheduled", start:(new Date(1336096800000)), source:{events:[#1#], className:[]}, _id:"2012-05-04", _start:(new Date(1336089600000)), end:null, _end:null, allDay:true, className:[]}]
Run Code Online (Sandbox Code Playgroud)
我尝试将其字符串化:
var test = JSON.stringify(resourceVacation, censor(resourceVacation));
function censor(censor) {
return (function() {
var i = 0;
return function(key, value) {
if (i !== 0 && typeof(censor) === 'object' && typeof(value) == 'object' && censor == value)
return '[Circular]';
++i; // so we know we aren't using the original object anymore
return value;
}
})(censor);
}
Run Code Online (Sandbox Code Playgroud)
我使用此处提到的审查:Chrome sendrequest错误:TypeError:将循环结构转换为JSON n
但是我通过浏览器获得以下异常:
未捕获的TypeError:将循环结构转换为JSON
这是Java Script对象:

我在Mozilla浏览器中使用toSource()获得了以前的JSON对象.知道怎么解决它!
============================ …
我有List[(String,String)],我需要按第二个值对它们进行排序并返回一个地图
我做了以下事情:
val newMap = list.sortBy(_._2).foldLeft(Map.empty[String, String]) {
(map, key) ? map + (key._1 ? key._2)
}
Run Code Online (Sandbox Code Playgroud)
列表是一个 List[(String,String)]
但是,返回的地图没有排序!!