如何在我的代码中执行异常处理 [英] How to do exception handling in my code

查看:241
本文介绍了如何在我的代码中执行异常处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚了解了异常处理。所以,我还在使用它。我知道如何进行基本异常处理,如输入正确的值,并输入DividebyZeroException。



但是我需要帮助以下一些帮助:



如果



创建一个异常处理来抛出错误p> 1)每行中的整数个数彼此不相等,例如:Matrix = [3 4; 9 8 1] 应该是:matrix = [3 4 2; 9 8 1]



2)分号;我使用分隔符替换为无效字符,如:matrix = [3 4 2#9 8 1]



这是我的代码: / strong>



这是从我的主要创建字符串。

  string text =A = [8 5 5 4; 2 6 5 3; 8 5 2 6]; 

这是我的课程

  public string [,] Matrix(string text)
{
try
{
char [] splitOne = {'[',']'} ;
char [] splitTwo = {';'};
char [] splitThree = {''};
words = text.Split(splitOne)[1]
.Split(splitTwo,StringSplitOptions.RemoveEmptyEntries)
.Select(x => x.Split(splitThree,StringSplitOptions.RemoveEmptyEntries))
.ToArray();
}
catch
{
Console.WriteLine(尝试输入正确的字符串);

}


string [,] matrix = new string [words.Length,words [0] .Length]; (int i = 0; i< words.Length; ++ i)
{
for(int j = 0; j< words [i] .Length; ++ j)
{
matrix [i,j] = words [i] [j]; (int i = 0; i< matrix.GetLength(0); i ++)
{
for(int j)
}
}

= 0; j< matrix.GetLength(1); j ++)
{
Console.Write({0},matrix [i,j]);
}
Console.WriteLine();
}
返回矩阵;
}

我如何让尝试和抓住工作?这是一个基本的总体捕捉,但是如何使其与以下要求一起工作。现在没有输出我的消息。

解决方案

你的误会异常,您要使用异常作为数据结构的规则。相反,异常是在方法中出现错误时,因为您在数学运算中用 0 分隔。这是一个关键的错误,代码不知道该怎么做。



对于这个具体的例子,你应该做 if 语句。因为你想应用规则并执行它,而异常不适合这些用例,如果你在哪里实现它,你将需要 if 语句,以检查您要应用规则的字符串,在您的标准之后形成良好,之后抛出异常。如果您正在创建一个图书馆,那么Moreso就是有意义的。



我的代码应该是什么样的解决方案。

  String text =A = [8 5 3 4; 2 6 5 3; 8 5 2 3]; 

var startIndex = text.IndexOf('[')+ 1;
var length = text.IndexOf(']') - startIndex;
text = text.Substring(startIndex,length);

if(!text.Contains(;))
{
Console.WriteLine(malformed seperator);

return;
}

var test = text.Split(';')。选择((k,i)=> new {Key = i,Value = k.Replace( )。长度});


if(!test.All(x => x.Value == test.First()。Value))
{
Console.WriteLine 不平衡阵列);
返回;
}

但是,我认为你正在使自己的工作更加努力,那么你应该设计意味着在字符串创建过程中,很多东西都是正确的,并且使用已经建立的矩阵库可以节省大量的工作。



编辑 / p>

更新了解决方案,但我仍然认为字符串矩阵的设计强制代码在许多方向是无用的。


I just learned about exception handling. So, I am still getting use to it. I know how to do the basic exception handling like entering in a correct value for division and having it throw a DividebyZeroException if a incorrect value is entered.

But I need help some help on doing the following:

Create an exception handling to throw an error if

1) The number of integer in each row are not equal to each other for example: Matrix = [ 3 4; 9 8 1] should be: matrix = [ 3 4 2; 9 8 1]

2) the semicolon ";" that I use as a separator is replace by an invalid character such as: matrix = [ 3 4 2 # 9 8 1]

This is my code:

this is from my main in which I create the string.

string text = "A = [8 5 5 4; 2 6 5 3; 8 5 2 6]";

this is my class

public string[,] Matrix(string text)
{
    try
    {
        char[] splitOne = { '[', ']' };
        char[] splitTwo = { ';' };
        char[] splitThree = { ' ' };
        words = text.Split(splitOne)[1]
                               .Split(splitTwo, StringSplitOptions.RemoveEmptyEntries)
                               .Select(x => x.Split(splitThree, StringSplitOptions.RemoveEmptyEntries))
                               .ToArray();
    }
    catch
    {
        Console.WriteLine("Try entering a correct string");

    }


    string[,] matrix = new string[words.Length, words[0].Length];
    for (int i = 0; i < words.Length; ++i)
    {
        for (int j = 0; j < words[i].Length; ++j)
        {
            matrix[i, j] = words[i][j];
        }
    }

    for (int i = 0; i < matrix.GetLength(0); i++)
    {
        for (int j = 0; j < matrix.GetLength(1); j++)
        {
            Console.Write("{0} ", matrix[i, j]);
        }
        Console.WriteLine();
    }
    return matrix;
}

How would I make the try and catch work? This is a basic general catch all but how would I make it work with my following requirements as well. Right now it is not outputting my message.

解决方案

You misunderstanding Exceptions, you want to use Exceptions as rules for your data structure. Instead Exceptions is when an error occour in a method, as you are dividing with 0 in a math operation. This is a critical error, where the code does not know what to do.

For this concrete example, you should do if statements. Because you want to apply rules and enforce it and Exceptions does not fit these use cases well, if you where to implement it, you would need if statements to check if the string you want to apply the rules to, was well formed after your standards and after that throw an Exception. Moreso this makes sense if you are creating a library.

My solution to what the code should look like.

String text = "A = [8 5 3 4; 2 6 5 3; 8 5 2 3]";

var startIndex = text.IndexOf('[') + 1;
var length = text.IndexOf(']') - startIndex;
text = text.Substring(startIndex, length);

if(!text.Contains(";"))
{
    Console.WriteLine("malformed seperator");

    return;
}

var test = text.Split(';').Select((k, i) => new {Key = i, Value = k.Replace(" ", "").Length});


if(!test.All(x => x.Value == test.First().Value))
{
    Console.WriteLine("unbalanced array");
    return;
}

But i think you are making your self work harder then you should, your design implies that a lot of things goes right in the string creation and using already build matrix libraries would save a lot of work here.

EDIT

Updated the solution to work, but i still think the design of the string matrix forces the code in a lot of directions that is unecesary.

这篇关于如何在我的代码中执行异常处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆