我不知道为什么抛出异常,这是工作代码:
DateTime.Parse("1/12/2012 12:00:00 AM")
Run Code Online (Sandbox Code Playgroud)
这是抛出异常的那个:
DateTime.Parse("1/13/2012 12:00:00 AM")
Run Code Online (Sandbox Code Playgroud)
抛出的异常是"FormatException",包含此消息: "字符串未被识别为有效的DateTime".
这是CurrentCulture值:
System.Globalization.CultureInfo.CurrentCulture
{en-MY}
Calendar: {System.Globalization.GregorianCalendar}
CompareInfo: {CompareInfo - en-MY}
CultureTypes: SpecificCultures | InstalledWin32Cultures
DateTimeFormat: {System.Globalization.DateTimeFormatInfo}
DisplayName: "English (Malaysia)"
EnglishName: "English (Malaysia)"
IetfLanguageTag: "en-MY"
IsNeutralCulture: false
IsReadOnly: true
KeyboardLayoutId: 17417
LCID: 17417
Name: "en-MY"
NativeName: "English (Malaysia)"
NumberFormat: {System.Globalization.NumberFormatInfo}
OptionalCalendars: {System.Globalization.Calendar[2]}
Parent: {en}
TextInfo: {TextInfo - en-MY}
ThreeLetterISOLanguageName: "eng"
ThreeLetterWindowsLanguageName: "ENM"
TwoLetterISOLanguageName: "en"
UseUserOverride: true
Run Code Online (Sandbox Code Playgroud)
有谁知道这里发生了什么?
我收到System.FormatException:运行此方法时输入字符串的格式不正确.
字段的值:
arrayName = "requester";
fieldList = "\"name\" : \"shimshon\""; // "name" : "shimshon"
public override string ToString()
{
var val = string.Format("\"{0}\" : { {1} }", arrayName, fieldList);
return val;
}
Run Code Online (Sandbox Code Playgroud)
该方法的期望结果是
"requester" : { "name" : "shimshon" }
Run Code Online (Sandbox Code Playgroud)
这种格式有什么问题?
我正在尝试设置我的Font Family组合框的选定值,该组合框已填充以下XAML:
<ComboBox ItemsSource="{x:Static Fonts.SystemFontFamilies}" Name="cboFont">
<ComboBox.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel MinWidth="256" />
</ItemsPanelTemplate>
</ComboBox.ItemsPanel>
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Margin="2" Text="{Binding}" FontFamily="{Binding}"/>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
Run Code Online (Sandbox Code Playgroud)
我必须将组合框设置为字符串,但这会导致FormatExceptions.任何人都可以快速告诉我组合框将期待什么类,以及如何将字符串例如"Arial"转换为该格式?
我在c#中的代码下面,我将我的字符串类型格式日期转换为datetime,但是给出了错误.
if (!string.IsNullOrEmpty(SessionDictionary.GetValue("UserDetails", "ExpiryDate")))
{
DateTime ExpiryDate = DateTime.ParseExact(SessionDictionary.GetValue("UserDetails", "ExpiryDate"), "dd mmm yy", null);
strDate = sitedata.FormatDate(ExpiryDate, TridionDateFormat.ShortDate);
}
else
{
strDate = "-";
}
Run Code Online (Sandbox Code Playgroud)
我SessionDictionary.GetValue("UserDetails", "ExpiryDate")的字符串类型数据返回"31/01/2011 00:00:00"格式日期,在我使用DateTime.ParseExact它的上面的代码中给我System.FormatException: String was not recognized as a valid DateTime.错误.
请说明出了什么问题.
谢谢.
为什么这段代码会抛出异常?
var dateTime = "2012-03-21_15.12";
var format = new DateTimeFormatInfo()
{
FullDateTimePattern = "yyyy-MM-dd-HH_mm.ss"
};
// FormatException: String was not recognized as a valid DateTime.
var parse = DateTime.Parse(dateTime, format);
Run Code Online (Sandbox Code Playgroud) 我已经创建了一个程序,并对它进行了大量测试,我收到一条错误消息"FormatException未处理,输入字符串格式不正确".当我将任一文本框留空并按下"完成"按钮但如果我输入低于0或高于59的任何值 - 这是我想要允许的数字范围,它会正常工作.我怎么办?当盒子空白时,我没有收到这个错误?这是'btnFinished'背后的代码:
private void btnFinished_Click(object sender, EventArgs e)
{
if (lstCyclists.SelectedIndex >= 0)
{
Cyclists currentCyc = (Cyclists)lstCyclists.SelectedItem;
//Decalre the minsEntered and secsEntered variables for txtMins and textSecs
int minsEntered = int.Parse(txtMins.Text);
int secsEntered = int.Parse(txtSecs.Text);
try
{
//If the status of a cyclist is already set to Finished, show an error
if (currentCyc.Finished.ToString() == "Finished")
{
MessageBox.Show("A time has already been entered for this cyclist");
}
else
{
//if a minute lower than 0 or greater than 59 …Run Code Online (Sandbox Code Playgroud) class Program
{
static void Main(string[] args)
{
string str = "0.898";
double dbl = Double.Parse(str);
dbl++;
Console.WriteLine(dbl);
Console.ReadLine();
}
}
Run Code Online (Sandbox Code Playgroud)
我需要工作的所有其他格式,如"100"工作.但是只要我添加一个"点",我就会FormatException出错.
嗨,我正在尝试制作简单的计算器,因为我是 C# 新手并且不熟悉,无论如何,当我尝试通过 try catch 处理格式异常时,当我输入一个值而不是数字时,我没有工作,它总是抛出异常不执行 catch 块
try
{
Val1 = double.Parse(opr1.Text);
Val2 = double.Parse(opr2.Text);
double sum = Val1 + Val2;
label1.Text = sum.ToString();
}
catch(Exception ex //or FormatException)
{
label1.Text = "Please enter the proper data type";
}
Run Code Online (Sandbox Code Playgroud)
在不执行 catch 块的情况下尝试转换错误的值时总是会出错
错误:mscorlib.dll中出现未处理的"System.FormatException"类型异常
附加信息:索引(从零开始)必须大于或等于零且小于参数列表的大小.
namespace Inheritance
{
abstract class Airlines
{
public int Aid;
protected string Aname, DOD, ToAdd, ToFrom, FromAdd;
protected float Cost;
public void Accept()
{
Console.WriteLine("Enter the Aid,Aname");
Aid = Convert.ToInt32(Console.ReadLine());
Aname = Console.ReadLine();
Console.WriteLine("Enter DOD,ToAdd,FromAdd");
DOD = Console.ReadLine();
ToAdd = Console.ReadLine();
FromAdd = Console.ReadLine();
Console.WriteLine("Enter the Cost");
Cost = Convert.ToSingle(Console.ReadLine());
}
public abstract void DisplayInfo();
public abstract void facility();
}
abstract class SpiceJet : Airlines
{
string PaymentMode;
public void AcceptData()
{
//Console.WriteLine("Welcome");
Accept();
}
public void payment() …Run Code Online (Sandbox Code Playgroud) I use the following line to convert the datarow value into double.
double.parse(Convert.ToString(datarow));
Run Code Online (Sandbox Code Playgroud)
If the datarow is DBNULL, I am getting the following exception:
'double.Parse(Convert.ToString(data))' threw an exception of type 'System.FormatException'
How to handle this one without using tryparse.