answersLogoWhite

0

It works the same way as the ones in System.Exception:

To make an exception happen, use the keyword throw, followed by an instance of an Exception (or an object from a derived Exception. A user defined exceptions of course is a derived exception)

Exception anException = new WowException(); //or new Exception("Wow");

throw anException;

The catcher can specifically catch the exception being thrown, or generally as catch-all:

try {

MakeMyDay();

} catch (MyOMyException e) {//do something about e}

catch (Exception e) { //do something about e}

void MakeMyDay() {

//some code, and encounter some special cases

throw new MyOMyException();

}

public class MyOMyException : SystemException //I prefer SystemException. ApplicationException will do as well, but not Exception

{ public MyOMyException() : base("My-Oh-My, what you've done?!"){}}

User Avatar

Wiki User

14y ago

What else can I help you with?

Related Questions

How many types of user defined exceptions are there and list them?

They are user-defined. In other words: You & Me (Users) define them (make them). There is an endless number of user-defined exceptions


What is user exceptions?

user defined exception is created by user such as arthmetic,number format exception ...


What are common types of user defined exceptions?

There are no common types of user-defined exceptions. If they were common, they'd already be provided as standard. The whole point of a user-defined exception is to differentiate between the common and the uncommon. For common exceptions such as range errors we can simply throw a std::range_error; we don't need a user-defined exception unless we need to throw additional information that cannot easily be provided by the standard library exception.


What keyword is used to invoke user-defined exceptions?

throw is the keyword used to invoke the exception.throw new NoBalanceException("No balance please");


What is the user defined object?

A user-defined object is an instance of a user-defined type, typically a class, or an enum.


What is the difference between the world coordinate system and a user-defined coordinate system?

A world coordinate system is generally used by gorrilas or pandas in the middle east. A user-defined coordinate system only works for gypsies or pikey scum.


In c programming is main predefined or user defined?

main is predeclared and user defined.


What is the advantage of user-defined copy constructor?

What is the advantage of user-defined copy constructor


What is use of user defined data in c?

to create user defined functions the user defined data is needed nd its useful to the programmer to create its own data.


What would be some examples of user-defined exceptions ie incorect password?

Well maybe if you are using a try/catch with the If ElseIf etc, put an "else" in the password code and put the exception there.


What is a user-defined region?

A user-defined region is one that is classified by use. eg. hydro-electricity region


How can user defined operator overloading harm the readability of the program?

Because the built in operator has the precision and compiler knows all the precision between the operators, and it works on that precision. User can also create its own operator but the compiler does not come to know thow to make precision of this operator. Therefore we dont use user defined operator