Take an exam unprepared vs Skipping it
Students usually decide this based on the consequences of each option. At some universities, you can retake failed exams but you cannot retake passed exams to improve your grade. In this case, it might be worth skipping the exam instead of taking it and "risking" obtaining a low passing grade that you cannot get rid of.

It might also be that you the rules give you only a couple of chances to pass this exam, so you do not want to miss this chance. Or that you do not care about the grade as long as you pass, so you might as well have a try. Or that past exams are not made available to students, so it might be worth for you to go and learn how exams in this subject look like.

If none of the above applies (i.e., if you know you will fail anyway and are only asking whether the time spent at the exam will be worth it in terms of how much you learn), I would say that those hours would be much better spent learning the subject or doing practice exercises on your own. Surely you will learn more by doing exercises in a comfortable environment (at home or at the library) than by doing the same exercises in exam conditions.