Kiel
Kiel
CC#
Created by Olipro on 11/14/2024 in #help
IDisposable ownership semantics
to answer your question bluntly, no C# does not have first class support for...whatever it is you're trying to express. We have try/finally, try/catch, and the various forms of using. But it seems like you're pretty much asking for a way to create an object, do work with it, and return it, and dispose it ONLY if an exception is thrown. If you're re-using this logic lots, you could probably use the above suggestion as a wrapper, but otherwise yeah the way you're doing it is probably about as clean as it's gonna get without re-throwing
28 replies
CC#
Created by Olipro on 11/14/2024 in #help
IDisposable ownership semantics
what exactly are you trying to accomplish here then, if not what I'm suggesting?
28 replies
CC#
Created by Olipro on 11/14/2024 in #help
IDisposable ownership semantics
your original example was a little weird re-assigning result to a new variable so it threw me off
28 replies
CC#
Created by Olipro on 11/14/2024 in #help
IDisposable ownership semantics
otherwise, return it
28 replies
CC#
Created by Olipro on 11/14/2024 in #help
IDisposable ownership semantics
if I'm reading your messages right you want result disposed ONLY if an exception is thrown
28 replies
CC#
Created by Olipro on 11/14/2024 in #help
IDisposable ownership semantics
but keep in mind this approach would swallow exceptions, you should still probably log or whatever your system does to handle them
28 replies
CC#
Created by Olipro on 11/14/2024 in #help
IDisposable ownership semantics
var result = new SomeDisposable();

try
{
var ret = result.PossiblyThrows();
return ret;
}
catch
{
result.Dispose();
return // whatever you want
}
var result = new SomeDisposable();

try
{
var ret = result.PossiblyThrows();
return ret;
}
catch
{
result.Dispose();
return // whatever you want
}
Is this closer to what you're looking for? You could use try/catch instead of try/finally
28 replies
CC#
Created by Olipro on 11/14/2024 in #help
IDisposable ownership semantics
So you only want result disposed if an exception is thrown?
28 replies
CC#
Created by Olipro on 11/14/2024 in #help
IDisposable ownership semantics
if you prefer nesting and/or your true method is larger than this, you can also use a using expression instead:
using (var result = new SomeDisposable())
{
var ret = result.PossiblyThrows();
return ret;
}
using (var result = new SomeDisposable())
{
var ret = result.PossiblyThrows();
return ret;
}
28 replies
CC#
Created by Olipro on 11/14/2024 in #help
IDisposable ownership semantics
If you are using only try { } finally { }, perhaps consider using a, well...using statement or expression?
public SomeDisposable GetDisposable()
{
using var result = new SomeDisposable();
var ret = result.PossiblyThrows();
return ret;
}
public SomeDisposable GetDisposable()
{
using var result = new SomeDisposable();
var ret = result.PossiblyThrows();
return ret;
}
I'm not sure what the point of setting result to null is in your original code, but what I just posted above is syntactically equivalent to
public SomeDisposable GetDisposable()
{
var result = new SomeDisposable();
try
{
var ret = result.PossiblyThrows();
return ret;
}
finally
{
result.Dispose();
}
}
public SomeDisposable GetDisposable()
{
var result = new SomeDisposable();
try
{
var ret = result.PossiblyThrows();
return ret;
}
finally
{
result.Dispose();
}
}
28 replies
CC#
Created by Daiko Games on 9/30/2024 in #help
How to convert a whole json file to C# classes with Newtonsoft.Json?
Personally in the past I used https://quicktype.io/ to make a quick and dirty concept and then I'd refine it from there to my liking
43 replies
CC#
Created by SpookyToad on 9/30/2024 in #help
✅ Is it possible to clean up all this in a vs release folder?
It looks like you're using Humanizer, it might help to install Humanizer.Core if you JUST want english-language features - it won't produce just an exe file like above mentioned but it will at least keep all those extra folders from being generated
7 replies
CC#
Created by Merineth 🇸🇪 on 9/28/2024 in #help
✅ Exception handling
keep in mind that in a catch block, thrown exceptions are not caught - you would need ANOTHER try { } catch { } block inside it lol
31 replies
CC#
Created by Merineth 🇸🇪 on 9/28/2024 in #help
✅ Exception handling
There's nothing stopping you from throwing your own exception in a catch block, but generally you should have a good reason for doing so. A proper real-world usecase could be if you have your own generic ErrorException type which wraps other exceptions so you can display more detailed information than a plain old DivideByZeroException can
31 replies
CC#
Created by Merineth 🇸🇪 on 9/28/2024 in #help
✅ Exception handling
(even just new Exception(...) too)
31 replies
CC#
Created by Merineth 🇸🇪 on 9/28/2024 in #help
✅ Exception handling
You can make your own exception class, or you can throw any of the base exceptions
31 replies
CC#
Created by Merineth 🇸🇪 on 9/28/2024 in #help
✅ Exception handling
Correct, you can throw an exception at any time if you so choose
31 replies
CC#
Created by Merineth 🇸🇪 on 9/28/2024 in #help
✅ Exception handling
Ignoring the fact I'm using int.Parse instead of int.TryParse, here's an example of handling multiple exceptions in different ways
31 replies
CC#
Created by Merineth 🇸🇪 on 9/28/2024 in #help
✅ Exception handling
try
{
var input = Console.ReadLine();
var number = int.Parse(input);
var division = 5 / number;
}
catch (NullReferenceException)
{
// input was null!
}
catch (FormatException)
{
// number failed to parse (int.Parse failed)
}
catch (DivideByZeroException)
{
// input was 0
}
try
{
var input = Console.ReadLine();
var number = int.Parse(input);
var division = 5 / number;
}
catch (NullReferenceException)
{
// input was null!
}
catch (FormatException)
{
// number failed to parse (int.Parse failed)
}
catch (DivideByZeroException)
{
// input was 0
}
31 replies
CC#
Created by Merineth 🇸🇪 on 9/28/2024 in #help
✅ Exception handling
You can name the variable whatever you like, most people I see name it ex or just e, or if you don't need to use the variable, you can omit it entirely like I do below
31 replies