Jump to content

Try / Except vs Throw


Schepto

Recommended Posts

Hello, I am a noob in Java and am wondering what the point is for OSBot scripting to use things like throw instead of try / catch.

I have yet to find a use case for throwing an interruptedexception but catching nullpointers is very useful (because im a noob). What is the difference in this context?

Edited by Schepto
Link to comment
Share on other sites

1 hour ago, Schepto said:

Hello, I am a noob in Java and am wondering what the point is for OSBot scripting to use things like throw instead of try / catch.

I have yet to find a use case for throwing an interruptedexception but catching nullpointers is very useful (because im a noob). What is the difference in this context?

You want to prevent the null pointer exception from happening, not allowing it to happen and then catching it. This is a very bad practice you are learning. In the case of an InterruptedException, for now you can just try/catch those since you have no control over it. For null objects, you can always check if the object is null using an if statement.

  • Heart 1
Link to comment
Share on other sites

18 minutes ago, Alek said:

You want to prevent the null pointer exception from happening, not allowing it to happen and then catching it. This is a very bad practice you are learning. In the case of an InterruptedException, for now you can just try/catch those since you have no control over it. For null objects, you can always check if the object is null using an if statement.

I really disagree with that. There are all sorts of reasons why exceptions can occur, and it's perfectly fine to use a try/catch to handle them--that's what try/catch is for!

 

OP, you use 'throws' when you want the calling function to do the try/catch. There is still a try/catch somewhere, it's just a matter of where.

 

I know that isn't very specific advice, but without looking at the specific situation, I cannot be more specific.

  • Heart 1
Link to comment
Share on other sites

8 minutes ago, Gundo said:

I really disagree with that. There are all sorts of reasons why exceptions can occur, and it's perfectly fine to use a try/catch to handle them--that's what try/catch is for!

 

OP, you use 'throws' when you want the calling function to do the try/catch. There is still a try/catch somewhere, it's just a matter of where.

 

I know that isn't very specific advice, but without looking at the specific situation, I cannot be more specific.

You're advising him to try catch null pointers instead of checking if the object is null? This is the problem with high level programming, encourages people to be lazy and create inefficient code.

  • Like 1
  • Heart 1
Link to comment
Share on other sites

48 minutes ago, Alek said:

You're advising him to try catch null pointers instead of checking if the object is null? This is the problem with high level programming, encourages people to be lazy and create inefficient code.

All null pointers I get are generally Noob mistakes. I still don't understand this exchangecontent nonsense and why I need to do it.... it really seems like a hack to me. But forgetting to do that is the real reason I have found to catch a nullpointer. Other then possibly a mob not being near but != null seems better for this.

That being said what is the point of InterruptedException? I know what a nullpointer is but wat is diz??? *confused*

Edited by Schepto
Link to comment
Share on other sites

1 hour ago, Alek said:

You want to prevent the null pointer exception from happening, not allowing it to happen and then catching it. This is a very bad practice you are learning. In the case of an InterruptedException, for now you can just try/catch those since you have no control over it. For null objects, you can always check if the object is null using an if statement.

Why put this inside an if statement EVERY TIME...? instead of try / catching onLoop and then passing my methods into that? It seems to catch them just fine even if I mess up as long as I extend to my Lib which i think is initialized into the Main Script onStart and checked from that...(Uses deprecated exchangecontext.(getbot) at least thats how I think it works ( Super noob in Java ). 

Edited by Schepto
Link to comment
Share on other sites

1 hour ago, Schepto said:

Why put this inside an if statement EVERY TIME...? instead of try / catching onLoop and then passing my methods into that? It seems to catch them just fine even if I mess up as long as I extend to my Lib which i think is initialized into the Main Script onStart and checked from that...(Uses deprecated exchangecontext.(getbot) at least thats how I think it works ( Super noob in Java ). 

Yeah, it works - but its inefficient. This is literally the worst thing you can do. If it was good under all circumstances then Java would just have it built-in.

Link to comment
Share on other sites

2 hours ago, Alek said:

Yeah, it works - but its inefficient. This is literally the worst thing you can do. If it was good under all circumstances then Java would just have it built-in.

Hmm... I still dont get it and I have been with OOP for a fair bit.... Could you please explain why this is important...?

Is Java really that bad? Its a high level language... it should not need low level mastery to perform well.... Sure the points you make are important but if a high level lang dev needs to know all these intricacies then Java seems like a really bad language which I just refuse to believe due to its popularity. I know its messy but that just seems like bad practice more then messy language.

The developers of java are not dumb and I fail to believe they will not cater to noobs like me. Are you sure this is not practice you picked up in legacy code?

because It just does not click to me and seems like old af like objective C from back in the early 2000's

Is this really as bad as you say it is now... or is this just legacy practice hanging on...

Edited by Schepto
Link to comment
Share on other sites

  • Alek locked this topic
12 hours ago, Schepto said:

Hmm... I still dont get it and I have been with OOP for a fair bit.... Could you please explain why this is important...?

Is Java really that bad? Its a high level language... it should not need low level mastery to perform well.... Sure the points you make are important but if a high level lang dev needs to know all these intricacies then Java seems like a really bad language which I just refuse to believe due to its popularity. I know its messy but that just seems like bad practice more then messy language.

The developers of java are not dumb and I fail to believe they will not cater to noobs like me. Are you sure this is not practice you picked up in legacy code?

because It just does not click to me and seems like old af like objective C from back in the early 2000's

Is this really as bad as you say it is now... or is this just legacy practice hanging on...

I'm banning you, I'm very certain you are a troll. Please learn how to program at a different community.

  • Like 5
Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...