Exceptions.

We have the same code. When i type 4/3 my program crashes with the error (picture). When my colleague types 4/3 he gets it written to the output and the program continues. Why?
No description
No description
72 Replies
SG97
SG97β€’3mo ago
how are you handling that exception assuming you colleague is handling that and prints the message to console
Merineth πŸ‡ΈπŸ‡ͺ
I have no idea how if i'm being honest. My understanding was that when the exception encounters a throw statement the program should crash (which it does for me) Especially since ZZZZZZZ covered this for me yesterday and explained it
Pobiega
Pobiegaβ€’3mo ago
exception encounters a throw statement
your exception doesnt encounter anything its just an object
Merineth πŸ‡ΈπŸ‡ͺ
Right but he is throwing an exception which to me says (oh an error CRASH THE PROGRAM) Considering it's unhandled
Pobiega
Pobiegaβ€’3mo ago
well, it should tell you "oh no something bad happened, I cant solve this!" and if noone can, thats unhandled -> crash program
Merineth πŸ‡ΈπŸ‡ͺ
Yeah but he isn't handling it anywhere He is just throwing the exception
Pobiega
Pobiegaβ€’3mo ago
I do know you have quite a bit of number parsing in your program thou thats all culture dependant
Merineth πŸ‡ΈπŸ‡ͺ
Yes else if(decimal.TryParse(token, out decimal number) == true) { throw new InvalidOperationException(); } But shouldn't this just crash the program?
Pobiega
Pobiegaβ€’3mo ago
Β―\_(ツ)_/Β― we cant tell from that point you might have a try catch later on
Merineth πŸ‡ΈπŸ‡ͺ
We don't
Pobiega
Pobiegaβ€’3mo ago
you are not running the exact same code then he deffo has a try/catch or an unhandled exception handler remember, the try/catch can be anywhere above in the callstack breakpoints are your friend
FusedQyou
FusedQyouβ€’3mo ago
Because your if statement should check if the result is false, I guess? I don't see the logic in throwing if the parse succeeded
else if(!decimal.TryParse(token, out decimal number))
{
throw new InvalidOperationException();
}
else if(!decimal.TryParse(token, out decimal number))
{
throw new InvalidOperationException();
}
Also, if you're going to throw you might as well use decimal.Parse unless you want to be more specific
Merineth πŸ‡ΈπŸ‡ͺ
I haven't written any of that code, so i have no idea
FusedQyou
FusedQyouβ€’3mo ago
Oh, this is identical code on both sides?
Merineth πŸ‡ΈπŸ‡ͺ
I'm just trying to figure out why he gets it written to console and the program continues while mine crashes becuase an unhandled error. Yes
FusedQyou
FusedQyouβ€’3mo ago
Can you share the full code? Preferably also from the other person
Merineth πŸ‡ΈπŸ‡ͺ
How?
FusedQyou
FusedQyouβ€’3mo ago
Upload to https://gdl.space/ or format with the Discord formatting
FusedQyou
FusedQyouβ€’3mo ago
No description
FusedQyou
FusedQyouβ€’3mo ago
Then just paste the code
Angius
Angiusβ€’3mo ago
Or a Github repo
Merineth πŸ‡ΈπŸ‡ͺ
OK seems he hasn't posted his changes
Merineth πŸ‡ΈπŸ‡ͺ
Our fork have different files for some reason
FusedQyou
FusedQyouβ€’3mo ago
Probably a good idea to merge those changes then
Merineth πŸ‡ΈπŸ‡ͺ
How?
FusedQyou
FusedQyouβ€’3mo ago
I assume this is a fork of another project you don't have control over, and that one has new changes? Or what is a fork here?
Merineth πŸ‡ΈπŸ‡ͺ
I own the fork It's my project He has pushed changes which i don't see in the software fork
FusedQyou
FusedQyouβ€’3mo ago
And you work on the same branch, being main? Or at least, I don't see other branches here
Merineth πŸ‡ΈπŸ‡ͺ
I'm unsure what branch mean But i assusme so? We work on the same project
FusedQyou
FusedQyouβ€’3mo ago
You seem to be on the main branch together A branch is part of your project and it's a version of it basically You can work in a branch and make changes, and these changes exist on this branch
Merineth πŸ‡ΈπŸ‡ͺ
In that case, yeah
FusedQyou
FusedQyouβ€’3mo ago
You can make more branches, and these will base off another branch as it is at that moment So the main branch always exists Then you make one called Merineth The other person makes changes to main, but these don't appear in Merineth When you merge main to Merineth, you merge its current state with your branch I suggest you look up how these work Anyway, the main issue here is conflicts Right now you work on the same branch, on the same code The issue here is that one change from somebody else might conflict with yours since you work on the same branch Resolving this is annoying Instead, what you should do it create a branch for each person
Merineth πŸ‡ΈπŸ‡ͺ
Ahhh
FusedQyou
FusedQyouβ€’3mo ago
Depending on the company they might tell you to make one for each distinct feature, but it's up to you Generally, a project has the main branch, and then a dev branch that was made from it
SG97
SG97β€’3mo ago
I think this is for school
FusedQyou
FusedQyouβ€’3mo ago
Then this dev branch has a bunch of sub branches for each person Again this depends entirely on what you want. People also often make a staging branch for example
SG97
SG97β€’3mo ago
but to add to this, using decent commit messages would also do you guys good
FusedQyou
FusedQyouβ€’3mo ago
So what you should probably do is look into making two new branches from main, and work in those instead Then, when you're done, you merge these with main
Merineth πŸ‡ΈπŸ‡ͺ
Yeah i made them
Merineth πŸ‡ΈπŸ‡ͺ
But when we push
FusedQyou
FusedQyouβ€’3mo ago
Once that is done, the other person can merge main into their branch Conflicts will be way easier to resolve then, and you won't detatch your own branch constantly because it was modified elsewhere
Merineth πŸ‡ΈπŸ‡ͺ
I push from breanch merineth to origin/main and he pushed from him to main?
FusedQyou
FusedQyouβ€’3mo ago
I know this doesn't resolve your main issue, but it's so much easier to work on your project if you use branches properly Merge requests (or pull requests if you use github) also very cleanly show you what was changed
Merineth πŸ‡ΈπŸ‡ͺ
Ok i made the two branches
No description
Merineth πŸ‡ΈπŸ‡ͺ
He does not see them yet How does he see them? Fetch?
FusedQyou
FusedQyouβ€’3mo ago
Yes If you are on a branch and you fetch, it will look for new commits on that branch Then, you pull You can also just pull immediatley Difference is that fetch doesnt apply the changes immediatley Considering you seem to work on the same files I suggest you try to pull and merge as much as possible to avoid possible conflicts If you don't then it's not as necessary, but Git can complain quickly
Merineth πŸ‡ΈπŸ‡ͺ
Yeah i'm just waiting for him to respond.. He clicked "fetch" and says he doesn't see them
FusedQyou
FusedQyouβ€’3mo ago
Make sure the correct branch is currently chosen
Merineth πŸ‡ΈπŸ‡ͺ
What's the difference between branch and remote?
Merineth πŸ‡ΈπŸ‡ͺ
you mean this?
Merineth πŸ‡ΈπŸ‡ͺ
Like we got it almost to work but i still dont understand what the difference is between branches and remotes Are branches copies of the project ? And then we merge them to main?
HtmlCompiler
HtmlCompilerβ€’3mo ago
remote branch is just the branch in the remote repository
FusedQyou
FusedQyouβ€’3mo ago
Yeah, if something is specified as remote then it's state state as it is in GitHub in your case
Merineth πŸ‡ΈπŸ‡ͺ
Ok
Merineth πŸ‡ΈπŸ‡ͺ
So currently i've pushed to main? Because i specifically choose my branch. We can see "Merineth" and Yannick" very far down I'm desperate for some help at this point :catHeyHello: :sadcat:
Pobiega
Pobiegaβ€’3mo ago
Whats the current state?
Merineth πŸ‡ΈπŸ‡ͺ
I just want to understand the fork
Pobiega
Pobiegaβ€’3mo ago
"understand the fork"?
Merineth πŸ‡ΈπŸ‡ͺ
I dont understand branches, remotes
Pobiega
Pobiegaβ€’3mo ago
oh, git okay so a remote in git is a remotely connected repository usually things like github, gitlab, bitbucket etc so for example, your project has a remote, which is your github repository.
Merineth πŸ‡ΈπŸ‡ͺ
makes sense
Pobiega
Pobiegaβ€’3mo ago
git works entirely offline locally so when you "push", you actually push your local commits to the remote repository a branch is... well, okay, now we need to talk about how git works and i cba writing, wanna go on a call?
Merineth πŸ‡ΈπŸ‡ͺ
ok
try
{
decimal output = calculationHandler.Run(top);
string stringoutput = output.ToString("F2");
outputlines.Add(stringoutput);
}
catch (Exception ex)
{
outputlines.Add($"Exception: {ex.Message}");
}
try
{
decimal output = calculationHandler.Run(top);
string stringoutput = output.ToString("F2");
outputlines.Add(stringoutput);
}
catch (Exception ex)
{
outputlines.Add($"Exception: {ex.Message}");
}
If i want to output a custom message. Any advice on how that is achieved?
Merineth πŸ‡ΈπŸ‡ͺ
I want to basically achieve this Would i have to create a user defined exception for InvalidOperationException and InvalidtokenException?
Pobiega
Pobiegaβ€’3mo ago
Yes Or pass a message in when you throw it, if the exception allows it
Want results from more Discord servers?
Add your server