Try Catch or If statement?

2020-02-10 02:30发布

if you think there is a possibility of getting a null pointer exception, should you use an if statement to make sure the variable is not null, or should you just catch the exception?

I don't see any difference as you can put your logic to deal with the null pointer in the if statement, or in the catch block, so which one is best practise?

10条回答
相关推荐>>
2楼-- · 2020-02-10 03:01

The main Question is if it is a good idea to have methods returning Null at all, personally i do not have any problem with this, but as soon as you try to access modifiers of an object returned from this method and you forget to check if it is assigned this becomes an issue.

Ken has a good answer about this:

If you are always expecting to find a value then throw the exception if it is missing. The exception would mean that there was a problem.

If the value can be missing or present and both are valid for the application logic then return a null.

See this disscussion abou tthis issue:

Returning null is usually the best idea if you intend to indicate that no data is available.

An empty object implies data has been returned, whereas returning null clearly indicates that nothing has been returned.

Additionally, returning a null will result in a null exception if you attempt to access members in the object, which can be useful for highlighting buggy code - attempting to access a member of nothing makes no sense. Accessing members of an empty object will not fail meaning bugs can go undiscovered.

Some further reading:

查看更多
迷人小祖宗
3楼-- · 2020-02-10 03:02

Well. Exceptions are just that. Exceptions. They are thrown when something unforseen has happened and should not be part of the normal program flow.

And that's what is happening here. You expected the argument to be specified when it's not. That is unexpected and you should therefore throw your own exception informing the user of that. If you want to get bonus points you can also include the reason to WHY the argument must be specified (if it's not obvious).

I've written a series of posts about exceptions: http://blog.gauffin.org/2013/04/what-is-exceptions/

查看更多
\"骚年 ilove
4楼-- · 2020-02-10 03:04

using try catch for the statements is not an good idea. because when you use try catch them it seems that if some error comes the code will not turninate the application. but if you are sure about what kind of error can come you can tap the error at that point. that will not produce any unknown errors. for example.

string name = null;

here i am going to use the name variable and i am sure that this will throw Null Refrance Error .

try
{
Console.writeLine("Name ={0}",name);
}
catch (NullRefranceException nex)
{
//handle the error 
}
catch(Exception ex)
{
 // handle error to prevent application being crashed. 
}

This is not a good practice while you can handle this kind of error and make your code more readable. like.

if(name !=null)
    Console.writeLine("Name ={0}",name);
查看更多
男人必须洒脱
5楼-- · 2020-02-10 03:06

Its always better to use Try Catch other than if else Here Exceptions are two types namely handled and UN-handled exceptions Even if u want to handle some function when the Exception u can handle it...

Handled exception always allows you to write some implementations inside the Catch block Eg. An Alert Message, A new Function to handle when such exception occurs.

查看更多
登录 后发表回答