PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, August 4, 2022

[FIXED] How to include a integer while throwing a failure exception in OCaml

 August 04, 2022     exception, ocaml, pattern-matching     No comments   

Issue

I have this function

let f = function
| 1 -> "a"
| 2 -> "b"
| _ -> failwith "Argument should be less than 3 and more than 0 but it was found to be x"

How do I set the value of x here equal to the function's input?


Solution

You can use the standard library function sprintf present in the Printf module.

| x -> failwith (Printf.sprintf "Argument should be ... but it was %d" x)

Although, I would recommend you to use invalid_arg instead of failwith since you are throwing the exception due to an invalid argument.

Check out this page of the OCaml documentation.



Answered By - kodwx
Answer Checked By - Candace Johnson (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing