C#抛出异常错误.. [英] C# throwing exception error..

查看:179
本文介绍了C#抛出异常错误..的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public static int getLatestCustomerID()
        {
            int latestCustomerID = 0;

            //query to get the latest customer ID
            const string getLatestCustomerIDQuery = @"select max(CustomerID) from Customers as 'latest'";

            try
            {
                //connection to get the latest customer ID
                using (SQLiteConnection getLatestCustomerIDConnection = new SQLiteConnection(conString))
                {
                    getLatestCustomerIDConnection.Open();

                    //command to get the latest customer ID
                    using (SQLiteCommand getLatestCustomerIDCommand = getLatestCustomerIDConnection.CreateCommand())
                    {
                        object obj_latestCustomerID = getLatestCustomerIDCommand.ExecuteScalar();

                        string str_latestCustomerID = obj_latestCustomerID.ToString();
                        if (str_latestCustomerID.Length == 0)
                            latestCustomerID = 0;
                        else
                            latestCustomerID = int.Parse(str_latestCustomerID);
                    }
                }
            }
            catch (Exception ex) { throw ex; }

            return latestCustomerID;
        }





这是我的代码,它停止并显示此错误...

< a href =http://tinypic.com/r/28t821y/8> http://tinypic.com/r/28t821y/8 [ ^ ]



这是什么意思?

我甚至没有创建一个null异常..

这是异常抛出sqlite库我再次投掷..

为什么会发生这种情况?

我的代码出了什么问题?



提前谢谢..



Here's my code and it stops and shows this error...
http://tinypic.com/r/28t821y/8[^]

what's the meaning of this ?
Im haven't even created a null exception..
It's that exception what throws sqlite library Im throwing again..
How come this happens ?
What's the wrong with my code ?

Thanks in advance..

推荐答案

所以它不应该再次抛出异常而不是在类中显示错误吗?



你已经抛出了异常catch处理程序 - 但是树没有进一步处理它的处理程序 - 因此系统会停止您的应用程序并让您知道存在问题。在制作中,它会崩溃。



因此,您可以自行确定导致错误的原因。在方法的开头放置一个断点,然后再次运行app。当您点击断点时,逐步查看明显的问题。如果你得到了捕捉,那么仔细看看你以前的线路!



但我的猜测是你没有创建一个实际的SQL命令:您在连接上调用CreateCommand,但在调用ExecuteScalar之前不会对其执行任何其他操作。你应该考虑设置CommandText属性......
"So shouldn't it throws the exception again instead of showing the error in the class ?"

You have thrown the exception in the catch handler - but there is no handler further up the "tree" to handle it - so the system stops your application and lets you know there is a problem. In production, it would just crash.

So it's up to you to work out exactly what is causing the error in the first place. Place a breakpoint at the start of the method, and run you app again. When you hit the breakpoint, step through looking obvious problems. If you get to teh catch, then look very closely at the line you were previously on!

But my guess is that you haven't created an actual SQL command: You call CreateCommand on the connection, but you don't do anything else with it before you call ExecuteScalar. You should probably consider setting the CommandText property...


你好..!



我认为



应该有问题

Hi..!

As I think

there should be problem
object obj_latestCustomerID = getLatestCustomerIDCommand.ExecuteScalar();

string str_latestCustomerID = obj_latestCustomerID.ToString();





由于ToString()方法,它没有处理空值。所以在使用之前检查对象是否为空。







Due to ToString() Method it not handled null value. So before using it check the object is null or not.


object obj_latestCustomerID = getLatestCustomerIDCommand.ExecuteScalar();

string str_latestCustomerID = ""; // Any default value when null value occure
if(obj_latestCustomerID != null)
{
  str_latestCustomerID = obj_latestCustomerID.ToString()
}









: - )





:-)


您好,



通过发布的代码我没有看到SQL Query(getLatestCustomerIDQuery)传递给命令对象。请添加代码以将SQL查询传递给命令对象;



例如:getLatestCustomerIDCommand.CommandText = getLatestCustomerIDQuery;





Hello,

Going through your code posted I don't see the SQL Query ( getLatestCustomerIDQuery) passed to the command object. Please add code to pass the SQL Query to the command object ;

eg : getLatestCustomerIDCommand.CommandText = getLatestCustomerIDQuery;


public static int getLatestCustomerID()
        {
            int latestCustomerID = 0;

            //query to get the latest customer ID
            const string getLatestCustomerIDQuery = @"select max(CustomerID) from Customers as 'latest'";

            try
            {
                //connection to get the latest customer ID
                using (SQLiteConnection getLatestCustomerIDConnection = new SQLiteConnection(conString))
                {
                    getLatestCustomerIDConnection.Open();

                    //command to get the latest customer ID
                    using (SQLiteCommand getLatestCustomerIDCommand = getLatestCustomerIDConnection.CreateCommand())
                    {
                        //pass the SQL Query as command text
                        getLatestCustomerIDCommand.CommandText = getLatestCustomerIDQuery;
                        object obj_latestCustomerID = getLatestCustomerIDCommand.ExecuteScalar();

                        string str_latestCustomerID = obj_latestCustomerID.ToString();
                        if (str_latestCustomerID.Length == 0)
                            latestCustomerID = 0;
                        else
                            latestCustomerID = int.Parse(str_latestCustomerID);
                    }
                }
            }
            catch (Exception ex) { throw ex; }

            return latestCustomerID;
        }


这篇关于C#抛出异常错误..的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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