Jump to content

Exceptions: Difference between revisions

no edit summary
No edit summary
Line 57:
{
switch (setjmp(env)) {
case 0: /* try */
foo();
break;
case MY_EXCEPTION: /* catch MY_EXCEPTION */
/* handle exceptions of type MY_EXCEPTION */
break;
Line 73:
if an exception is thrown.
 
The exception can be of any type, this includes int's, other primitives, as well as objects.
===try-catch===
 
===Defining exceptions===
struct MyException
{
// data with info about exception
};
 
===Throw exceptions===
// this function can throw any type of exception
void foo()
{
throw MyException();
}
 
// this function can only throw the types of exceptions that are listed
void call_foo()
void foo2() catch throw(MyException e)
{
trythrow {MyException();
}
foo();
 
}
===Catching exceptions===
catch (MyException &exc)
try {
foo();
// handle exceptions of type MyException and derived
}
catch (...MyException &exc)
{
// handle any typeexceptions of exception not handledtype byMyException aboveand catchesderived
}
catch (...)
{
// handle any type of exception not handled by above catches
}
 
Line 103 ⟶ 110:
'''Compiler''': MSVS 2005
 
===Defining exceptions===
public class MyException : Exception
{
// data with info about exception
};
 
===Throw exceptions===
void foo()
{
throw MyException();
}
 
===Catching exceptions===
void call_foo()
try {
foo();
}
catch (MyException &exce)
{
// handle exceptions of type MyException and derived
try {
}
foo();
}catch
{
catch (MyException e)
// handle any type of exception not handled by above catches
{
}
// handle exceptions of type MyException and derived
 
}
catch
{
// handle any type of exception not handled by above catches
}
}
=={{header|Factor}}==
===Throw Exceptions===
Line 176 ⟶ 184:
 
=={{header|Java}}==
An exception needs to extend the Exception type.
 
===Defining exceptions===
//Checked exception
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.