111,102
社区成员




以下代码可以编译通过,运行程序时却出现了一个提示框,显示:出现了一个问题,导致程序停止正常工作,请关闭该程序。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using PanGu;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
Segment.Init();
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string Original = richTextBox1.Text;// "盘古分词demo2";
Segment segment = new Segment();
ICollection < WordInfo > words = segment.DoSegment(Original);
string strs = "";
foreach (WordInfo v in words)
{
strs += v.ToString() + "\r\n";
}
richTextBox2.Text = strs;
}
}
}
PanGu.xml存储于应用程序同一个目录中,Dict.dct也存储于这个目录中,并已经在PanGu.xml里修改了字典的路径。
PanGu.xml中Dict.dct的路径已经修改过了。
如果将button1_Click(object sender, EventArgs e)括号内的代码全部注销,仍然会出现这个问题,如果再将下面的语句注销:
Segment.Init();
就不会出现这个问题了。改成下面这样也不行,估计问题是出在了初始化阶段。
public Form1()
{
Segment.Init(@"E:\net中文分词\测试盘古分词\WindowsFormsApp1\WindowsFormsApp1\bin\Debug\pangu.xml");
InitializeComponent();
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using PanGu;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
Segment.Init();
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
try
{
string Original = richTextBox1.Text;// "盘古分词demo2";
Segment segment = new Segment();
ICollection<WordInfo> words = segment.DoSegment(Original);
string strs = "";
foreach (WordInfo v in words)
{
strs += v.ToString() + "\r\n";
}
richTextBox2.Text = strs;
}
catch (Exception ex)
{
MessageBox.Show(ex); //这一行通不过编译
}
}
}
}
加个try catch看下哪个地方异常了
private void button1_Click(object sender, EventArgs e)
{
try{
string Original = richTextBox1.Text;// "盘古分词demo2";
Segment segment = new Segment();
ICollection < WordInfo > words = segment.DoSegment(Original);
string strs = "";
foreach (WordInfo v in words)
{
strs += v.ToString() + "\r\n";
}
richTextBox2.Text = strs;
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
}