What should one throw?, C/C++ Programming

Assignment Help:

A: C++, unlike only about every other language with exceptions, is extremely accomodating while it comes to what you can throw. Actually, you can throw anything you akin to. That begs the question then, what should you throw?

In general, it's best to throw objects, not built-ins. If achievable, you must throw instances of classes which derive (ultimately) from the std::exception class. Through making your exception class inherit (ultimately) from the standard exception base-class, you are making life simpler for users (they have the alternative of catching most things by std::exception), plus probably you are providing them with more information (as the fact that your particular exception might be a refinement of std::runtime_error or whatever).

The common practice is to throw temporary:

#include

class MyException : public std::runtime_error {

public:

MyException() : std::runtime_error("MyException") { }

};

void f()

{

// ...

throw MyException();

}

Here, a temporary of type MyException is created & thrown. Class MyException inherits from class std::runtime_error that (ultimately) inherits from class std::exception.


Related Discussions:- What should one throw?

Design a game in c, Design a game in c: const DODGERS = 0; const GI...

Design a game in c: const DODGERS = 0; const GIANTS = 1;   void main(void) {    int scoreboard [2][9];    // An array two rows by nine columns    int team, i

Pointer to function, Pointer to function: int mul(int a, int b, int c) ...

Pointer to function: int mul(int a, int b, int c) {   return a*b*c; } void main() {   int (*function_pointer)(int, int, int);   function_pointer = &mul;

Araay, create two array names and grade .in first array you have to store t...

create two array names and grade .in first array you have to store thease 10 names 1.a,2.3.r,4.s,5.l,6.k,7.n,8.b,9.z,10.d you have to enter grades for thease students.te program mu

FlipFlaps fltk project, The project is to design and write a C++11/FLTKgame...

The project is to design and write a C++11/FLTKgame program with a graphical user interface.The game is based on "pancake sorting," as described at http://en.wikipedia.org/wiki/P

Coding, Problem Statement: You have to write a C program to develop a Car P...

Problem Statement: You have to write a C program to develop a Car Parking Management System for a busy commercial area. The system will record the car plate number, date and time w

Define the data file in c programming, Define the Data File in C Programmin...

Define the Data File in C Programming? Many applications needs that information can written to or read from an auxiliary memory device. Such information is stock up on the memo

Area under curve, find the area of a curve y=f(x) between x=a and x=b integ...

find the area of a curve y=f(x) between x=a and x=b integrate f(x) between limits a and b   #include float start_point, /* GLOBAL VARIABLES */

Assigment, Hi is there any chance to get assignment for fresher tutor

Hi is there any chance to get assignment for fresher tutor

Structure, Write a function that calculates the number of elapsed days betw...

Write a function that calculates the number of elapsed days between two dates. For example the days between Feb 3, 1970 and June 21, 1980? Becareful for the Leap year

Can you overload a function depend only on whether parameter, Can you overl...

Can you overload a function depend only on whether a parameter is a value or a reference? A: No. Passing by value and reference looks identical to the caller.

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd