Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are there any standard exceptions?

I've been using throw new Exception("...") in my code, since I couldn't find anything else to use. I'm looking for things like C++'s out_of_range and logic_error classes.

std.exception defines a handful of functions to help with handling exceptions, but no actual types.

Are we meant to define all our own exceptions, or is Exception just used for everything?

like image 395
Maxpm Avatar asked Nov 15 '11 23:11

Maxpm


People also ask

What are the standard exceptions?

Standard Exceptions — certain classes of employees in workers compensation insurance who are common to many types of business and are separately rated unless included specifically in the wording of the governing occupational classification.

What is a count of standard exception?

Explanation: There are nine standard exceptions in c++. They are bad_alloc, bad_cast, bad_exception, bad_function_call, bad_typeid, bad_weak_ptr, ios_base::failure, logic_error and runtime_error.

What is the schedule of exceptions quizlet?

What is a Schedule of Exceptions on a title policy? Almost no title insurance policy protects against all conceivable events. As the name suggests, the Schedule of Exceptions is a specific list of items not covered and can include things such as unrecorded mechanic's liens, assessments, water rights and mining claims.


1 Answers

For the most part in Phobos, each module has its own exception type named after the module - e.g. UTFException for std.utf and FileException for std.file. There are a few which don't (e.g. std.concurrency has several exception types - MessageMismatch, OwnerTerminated, etc.), but that's the overall trend. As such, there are no exception types created with the idea that programmers would be instantiating them themselves. There's nothing to stop you from using any of the existing exception types, but in most cases, reusing them doesn't make sense, since they're module-specific rather than use-case specific.

Now, the closest analogy to C++'s out_of_range and logic_error types would be Errors rather than Exceptions - specifically core.exception.RangeError and core.exception.AssertError. Errors are different and are not intended to be recovered from. core.exception defines several of them (including OutOfMemoryError). You wouldn't normally be using those yourself, but you can if you want to. AssertError is what is thrown by assert when it fails.

So, to be clear, Throwable is the base exception type. Error and Exception are derived from Throwable. Any exception type which is not derived from Exception skips destructors, scope statements, and finally blocks and is not intended to be recovered from. Several standard Errors exist - primarily found in core.exception, but you can define your own if you need them. You probably won't use or define Errors very often though - aside from using assert.

On the other hand, anything derived from Exception is intended to be recoverable and will trigger destructors, scope statements, and finally blocks. There are not generally standard Exception-derived types that you would instantiate in your own code. You catch the Exceptions that Phobos defines, but you don't generally throw them. Rather, you either instantiate Exception directly or you use your own Exception-derived type.

It's also frequently considered good practice to use enforce and enforceEx rather than throwing an exception directly (though there are obviously cases where it makes better sense to throw one directly).

like image 110
Jonathan M Davis Avatar answered Dec 01 '22 16:12

Jonathan M Davis