这是一个介绍Reactive Framework的简单程序.但我想通过修改程序来尝试错误处理程序:
var cookiePieces = Observable.Range(1, 10);
cookiePieces.Subscribe(x =>
{
Console.WriteLine("{0}! {0} pieces of cookie!", x);
throw new Exception(); // newly added by myself
},
ex => Console.WriteLine("the exception message..."),
() => Console.WriteLine("Ah! Ah! Ah! Ah!"));
Console.ReadLine();
Run Code Online (Sandbox Code Playgroud)
在此示例中,使用了以下过载.
public static IDisposable Subscribe<TSource>(
this IObservable<TSource> source,
Action<TSource> onNext,
Action<Exception> onError,
Action onCompleted);
Run Code Online (Sandbox Code Playgroud)
我希望我会看到打印的异常消息,但控制台应用程序崩溃了.是什么原因?
我一直收到这个错误:
System.NullReferenceException was unhandled by user code
Message=[Arg_NullReferenceException]
Arguments:
Debugging resource strings are unavailable. Often the key and arguments provide sufficient information to diagnose the problem. See http://go.microsoft.com/fwlink/?linkid=106663&Version=5.0.60401.00&File=mscorlib.dll&Key=Arg_NullReferenceException
StackTrace:
at Jantire.DoHomeworkView.TextAlignment_combobox_SelectionChanged(Object sender, SelectionChangedEventArgs e)
at System.Windows.Controls.Primitives.Selector.OnSelectionChanged(SelectionChangedEventArgs e)
at System.Windows.Controls.Primitives.Selector.InvokeSelectionChanged(List`1 unselectedItems, List`1 selectedItems)
at System.Windows.Controls.Primitives.Selector.SelectionChanger.End()
at System.Windows.Controls.Primitives.Selector.OnItemsChanged(NotifyCollectionChangedEventArgs e)
at System.Windows.Controls.ItemsControl.OnItemCollectionChanged(Object sender, NotifyCollectionChangedEventArgs e)
at System.Collections.Specialized.NotifyCollectionChangedEventHandler.Invoke(Object sender, NotifyCollectionChangedEventArgs e)
at System.Windows.Controls.ItemCollection.NotifyCollectionChanged(NotifyCollectionChangedEventArgs e)
at System.Windows.Controls.ItemCollection.NotifyCollectionReady()
at System.Windows.Controls.ItemsControl.NotifyAllItemsAdded(IntPtr nativeItemsControl)
InnerException:
Run Code Online (Sandbox Code Playgroud)
在代码:
private void TextAlignment_combobox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
//This next line is where error …Run Code Online (Sandbox Code Playgroud) c# visual-studio-2010 nullreferenceexception silverlight-4.0
这里我检索目录中文本文件的最小大小.但它给出0了最小大小.但是该目录中没有0 kb文件.
var queryList3Only= (from i in di.GetFiles("*.txt", SearchOption.AllDirectories)
select i.Length / 1024).Min();
dest.WriteLine(queryList3Only.ToString()+" Kb");
Run Code Online (Sandbox Code Playgroud)
有什么建议吗?
我有一个asp:带有asp:TextExpressionValidator的TextBox来验证它是否是一个数字.显然,onchange事件将在渲染时附加到此文本框.我还在$(document).ready中添加了一个更改事件,以便在更改值时进行一些计算.
<asp:TextBox id="myText" runat="server" />
<asp:regularexpressionvalidator id="myRev" ControlToValidate="myText" runat="server">*</asp:regularexpressionvalidator>
$(document).ready(function(){
$('[id$=myText]').bind('change',function(){
//do something
}).change(); //force the change event at the very beginning
});
Run Code Online (Sandbox Code Playgroud)
由于寄存器时间的原因,我的函数将比.net生成的js执行得晚.但是asp.net js会抛出一个错误.我在js中追踪:
function ValidatorOnChange(event) {
...
}
Run Code Online (Sandbox Code Playgroud)
并发现event.fromElement,event.toElement,event.srcElement的所有内容都为null,从而导致异常.我做错什么了吗?有解决方案吗 谢谢.
编辑
它被证明是一个MS bug,在ASP.NET 4 vs2010中运行良好.
菜鸟问题:
我在mvc2应用程序中遇到了一个小错误.我能够追溯到这段代码:
List<Stream2FieldTypes> Stream2FieldTypes = new List<Stream2FieldTypes>();
foreach (var item in stream.Stream2FieldTypes)
{
Stream2FieldTypes.Add(item);
}
Run Code Online (Sandbox Code Playgroud)
我遇到的问题是,当我实例化新列表时,它的计数为1.我想这可能是因为我使用了构造函数.所以我尝试了这个:
List<Stream2FieldTypes> Stream2FieldTypes;
foreach (var item in stream.Stream2FieldTypes)
{
Stream2FieldTypes.Add(item);
}
Run Code Online (Sandbox Code Playgroud)
但是,当然由于错误而无法编译Stream2FieldTypes.Add(item);.有没有办法可以创建List<Stream2FieldTypes>并确保计数为零?
我初始化了一个dataAdapter:
string sql = "SELECT * From localitati";
da1 = new System.Data.SqlClient.SqlDataAdapter(sql, con);
da1.Fill(ds1, "localitati");
Run Code Online (Sandbox Code Playgroud)
这很好用.问题是当我尝试删除记录并更新数据库时.我从数据集中删除了一条记录:
ds1.Tables["localitati"].Rows.Remove(dRow);
Run Code Online (Sandbox Code Playgroud)
这也很好(验证).
问题是当我更新DataAdapter时,DataBase不会被修改:
con.Open()
da1.Update(ds1, "localitati");
con.Close();
Run Code Online (Sandbox Code Playgroud)
可能是什么问题呢 ?
在Visual Studio社区2015 Update 3中,当您的项目中不存在类型时,VS将搜索包源并提供"add package xxx"建议,如下所示:

但是在VS2017社区,这个功能已经消失了.有没有选择打开它?还是完全删除了?为什么?
.net c# visual-studio visual-studio-community visual-studio-2017
我的程序调用将errno在发生故障时设置的方法,我抛出errno异常并捕获它:
try
{
if (-1 == truncate("/foo/bar.txt", 0))
{
throw errno;
}
}
catch (const int errno)
{
//log
}
Run Code Online (Sandbox Code Playgroud)
在这里,我不想讨论异常处理最佳实践主题.事实是在上面的代码中,catch当catch括号中的变量名称时,块不会被命中errno.此问题可以简化为:
try
{
throw 1999;
}
catch (const int errno) //renaming "errno" to "e" works!!!
{
//unreachable code here
}
Run Code Online (Sandbox Code Playgroud)
我知道这errno是一个"特殊"名称,但我认为C++可以正确处理在不同作用域中定义的相同变量名称.
//test.h
int my_number = 99;
//test.cpp
#include "test.h"
int main()
{
try
{
throw 1999;
}
catch(int my_number)
{
std::cout << "in catch: " << my_number << std::endl; //prints …Run Code Online (Sandbox Code Playgroud) 假设有一个3TB的TXT文件,其中每一行都是一个字符串,如何在其中找到那些重复的字符串?这是我朋友的采访问题.在接下来的一次采访中,我们最好让这些问题足够清楚.
PS:如果我是受访者,我会告诉采访者:你们怎么能在TXT文件中存储这么多字符串?这真是个坏主意!
我会尝试快速,因为我已经在这个主题上进行了详尽的搜索,我只找到了将bool转换为int的相关主题.
我构建了一个类来处理稀疏矩阵.它们可以用double,int或bool或任何其他值类型填充.
为了在位置i获得一些矩阵元素,j:
public T getElementValueAt(int i, int j)
{
int ind = this.doesElemExist(i, j);
// returns the element index if it exists, or -1
return (ind == -1 ? (T)(object)0 : this.elem[ind].value );
}
Run Code Online (Sandbox Code Playgroud)
在继续之前,有一个突出显示上面的错误做法,另一个关于稀疏矩阵:
我一直在寻找并发现这(T)(object)0不是一个好习惯,但我不明白为什么我不能在这里使用它,因为无论T是int还是double,它都可以工作;
如果我想要获取的元素不在列表中this.elem,我应该根据其他元素类型正确返回0.
因此,归结为将int值(在本例中为0)转换为某个类型T,这是一个类型参数.
有什么好办法吗?有关这种方法的任何评论?
任何帮助,将不胜感激!
提前致谢!
c# ×9
.net ×2
algorithm ×1
asp.net ×1
boolean ×1
c++ ×1
casting ×1
dataadapter ×1
errno ×1
int ×1
java ×1
javascript ×1
jquery ×1
linq ×1
sql-server ×1