8

If you were designing a standard library of a high level language (like Rust, C++, etc.) what would it do if while getting the current time of day (e.g. SystemTime::now()) it encountered a failed kernel system call (e.g. to clock_gettime) and why?

What do you think the existing implementations do?

  • Return error or raise exception
  • Return 0
  • Return undefined values (like stack garbage)
  • Panic/crash/segfault
you are viewing a single comment's thread
view the rest of the comments
[-] Colloidal@programming.dev 6 points 1 day ago

Raising an exception would be standard practice, IMO. A library should not dictate how an application behaves. Let the application handle it.

[-] sxan@midwest.social 1 points 1 day ago

If that's the only error mechanism, sure. Exceptions in most languages tend to be relatively expensive, though, and most have a cheaper idiomatic way of returning error codes; you'd want to use those if they're available, right?

Does Rust use exceptions a lot? I don't know. V has panic and catch, but you almost never see them. Idiomatic is Option (?) and Return (!) values, which I thought V borrowed from Rust. Go does the (val, error) tuple-ish return thing, and while it too has catchable panics, they're discouraged in favor of (error) return values.

Depends on the language. "Higher level" is a pretty broad field!

[-] bleistift2@sopuli.xyz 2 points 1 day ago

If that’s the only error mechanism, sure. Exceptions in most languages tend to be relatively expensive, though, and most have a cheaper idiomatic way of returning error codes; you’d want to use those if they’re available, right?

I think not being able to get the current time from the system is very exceptional. And I think exceptional circumstances should act that way and not “look like” normal executions. To me, that means letting hell break loose, and not “silently” return a 1 instead of a 0.

By similar reasoning, “Exceptions in most languages tend to be relatively expensive” is a very weak argument. We don’t expect this error-throwing code to execute a lot.

[-] sxan@midwest.social 1 points 1 day ago

I can think if plenty of situations where system time is

  • Optional
  • Unreliable
  • And even potentially disallowed by the user

In fact, if you don't set up your containers right, the system time is almost always wrong.

this post was submitted on 04 Aug 2025
8 points (100.0% liked)

Programming

21981 readers
119 users here now

Welcome to the main community in programming.dev! Feel free to post anything relating to programming here!

Cross posting is strongly encouraged in the instance. If you feel your post or another person's post makes sense in another community cross post into it.

Hope you enjoy the instance!

Rules

Rules

  • Follow the programming.dev instance rules
  • Keep content related to programming in some way
  • If you're posting long videos try to add in some form of tldr for those who don't want to watch videos

Wormhole

Follow the wormhole through a path of communities !webdev@programming.dev



founded 2 years ago
MODERATORS