Exceptions: Difference between revisions

From Rosetta Code
Content added Content deleted
(Moved to Language Features category)
(This task was getting no love, squirreled away in the Language Features category. Moved back to the Programming Tasks section until most of the tasks get a better categorization system.)
Line 1: Line 1:
{{Task}}
{{Language Feature}}


This task is to give an example of an exception handling routine and to "throw" a new exception.
This task is to give an example of an exception handling routine and to "throw" a new exception.

Revision as of 02:34, 1 February 2007

Task
Exceptions
You are encouraged to solve this task according to the task description, using any language you may know.

This task is to give an example of an exception handling routine and to "throw" a new exception.

AppleScript

try

try
    set num to 1 / 0
    --do something that might throw an error
end try

try-on error

try
    set num to 1 / 0
    --do something that might throw an error
on error errMess number errNum
    --errMess and number errNum are optional
    display alert "Error # " & errNum & return & errMess
end try

throw

error "Error message." number 2000

C++

C++ has no finally construct. Instead you can do this in the destructor of an object on the stack, which will be called if an exception is thrown.

try-catch

 struct MyException
 {
   // data with info about exception
 };

 void foo()
 {
   throw MyException();
 }

 void call_foo()
 {
   try {
     foo();
   }
   catch (MyException &exc)
   {
     // handle exceptions of type MyException and derived
   }
   catch (...)
   {
     // handle any type of exception not handled by above catches
   }
 }

C#

Compiler: MSVS 2005

 public class MyException : Exception
 {
   // data with info about exception
 };

 void foo()
 {
   throw MyException();
 }

 void call_foo()
 {
   try {
     foo();
   }
   catch (MyException e)
   {
     // handle exceptions of type MyException and derived
   }
   catch
   {
     // handle any type of exception not handled by above catches
   }
 } 

Java

Defining exceptions

  //Checked exception
  public class MyException extends Exception {
     //Put specific info in here
  }
  //Unchecked exception
  public class MyRuntimeException extends RuntimeException {}

Throw exceptions

  public void fooChecked() throws MyException {
     throw new MyException();
  }
  public void fooUnchecked() {
     throw new MyRuntimeException();
  }

Catching exceptions

  try {
     fooChecked();
  }
  catch(MyException exc) {
     //Catch only your specified type of exception
  }
  catch(Exception exc) {
     //Catch any non-system error exception
  }
  catch(Throwable exc) {
     //Catch everything including system errors (not recommended)
  }
  finally {
     //This code is always executed after exiting the try block
  }

Perl

# throw an exception
die "Danger, danger, Will Robinson!";

# catch an exception and show it
eval {
    die "this could go wrong mightily";
};
print $@ if $@;

# rethrow
die $@;

See http://perldoc.perl.org/perlvar.html#%24EVAL_ERROR for the meaning of the special variable $@. See http://search.cpan.org/dist/Error for an advanced, object based exception handling.

Python

try-except-finally-else

Interpreter: Python 2.5

Before Python 2.5 it was not possible to use finally and except together.

try:
   foo()
except TypeError:
   bar()
finally:
   baz()
else:
   # no exception occurred
   quux()