Questions tagged [exception]

An exception is an unusual condition that requires deviation from the program's normal flow. Normally, an exception should not result in total failure, but instead be attended by an exception handler. Exception handling is a built-in construct in many programming languages. Usually, exceptions are handled by unwinding the stack, thus rolling back to a defined state outside the exception's scope, and then invoking a handler block or routine.

General

Exception handling is a programming language construct or computer hardware mechanism designed to handle the occurrence of exceptions, special conditions that change the normal flow of program execution. When such conditions occur the programmer can decide to "throw" or "raise" an exception. The thrown exception will propagate up the stack until "caught" by an appropriate language construct, which usually contains code that deals with the situation. Unhandled exceptions usually lead to abnormal termination.

Programming languages differ considerably in their support for exception handling (as distinct from error checking, which is normal program flow that checks for contingencies such as unsuccessful termination of invoked operations). In some programming languages, there are functions that cannot be safely called on invalid input data, or functions that return values which cannot be distinguished from exceptions. For example, in the atoi (ASCII to integer conversion) function may return 0 (zero) for any input that cannot be parsed into a valid value. In such languages, the programmer must either perform error checking (possibly through some auxiliary global variable such as C's errno) or input validation (perhaps using regular expressions) or both.

Exception handling is built upon three keywords: try, catch, and throw.

  • try: A try block identifies a block of code for which particular exceptions will be activated. It is followed by one or more catch blocks.
  • throw: A program throws an exception when a problem shows up. This is done using the throw keyword.
  • catch: A program catches an exception with an exception handler at the place in a program where you want to handle the problem. The catch keyword indicates the catching of an exception.

A method catches an exception using a combination of the try and catch keywords. A try/catch block is placed around the code that might generate an exception. Code within a try/catch block is referred to as protected code, and the syntax for using try/catch looks like the following:

try
{
   // protected code
}
catch( ExceptionName e1 )
{
   // catch block
}
catch( ExceptionName e2 )
{
   // catch block
}
catch( ExceptionName eN )
{
   // catch block
}

You can provide multiple catch statements to catch different types of exceptions in case your try block raises more than one type of exception.

Exception safety

Exception safety, as formalised by David Abrahams, guarantees a set of contract guidelines that an interface (or operation) offers w.r.t. the state of the program if an exception occurs.

  1. No-throw guarantee: the operation is guaranteed not to fail
  2. Strong exception safety: if the operation fails, the state will be as it was prior to the failure (rollback semantics)
  3. Basic exception safety: no leaks will occur, and data is left in a valid state (but possibly changed)
  4. No exception safety: no guarantees are made.

Automated exception handling

Automated exception handling is a computing term referring to the computerized handling of errors. Runtime engines such as those for the Java language or Microsoft .NET lend themselves to an automated mode of exception or error handling. In these environments, software errors do not "crash" the program or operating system but rather generate exceptions. Recent advances in these runtime engines enable specialized runtime-engine add-on products to provide automated exception handling that is independent of the source code and provides root-cause information for every exception of interest.

Tag Usage

Use this tag for

  • Questions about the technical process of how various languages, runtimes, or platforms handle (or do not handle) specific exceptions.
  • Questions about implementing custom automated exception handling capabilities.

Do not use this tag for

  • Debugging requests containing an exception as part of an MCVE, but are not otherwise about exceptions. These are questions that contain exceptions, but are not about them. One of our suggested edit rejections contains the text "Tags should help to describe what the question is about, not just what it contains."

References

Exception handling syntax

Further reading

Vexing exceptions blog post on MSDN by Eric Lippert, 2008
Cleaner, more elegant, and harder to recognize blog post on MSDN by Raymond Chen, 2005
Exception-Safe Coding in C++ web page by Jon Kalb

53429 questions
25
votes
2 answers

Green Exceptions?

When unhandled exceptions are encountered in VStudio usually the debugger highlights the line YELLOW as the line that threw the exception. However sometimes I encounter exceptions where the debugger highlights them green as shown: I've always…
Aren
  • 54,668
  • 9
  • 68
  • 101
25
votes
5 answers

What does "Error in namespaceExport(ns, exports) : undefined exports" mean?

When building a package, I got the error Error in namespaceExport(ns, exports) : undefined exports: FooBarBaz What does this mean, and how do I fix it?
Richie Cotton
  • 118,240
  • 47
  • 247
  • 360
25
votes
2 answers

Why is TargetInvocationException treated as uncaught by the IDE?

I have some code that is using reflection to pull property values from an object. In some cases the properties may throw exceptions, because they have null references, etc. object result; try { result = propertyInfo.GetValue(target,…
25
votes
6 answers

How are exceptions allocated on the stack caught beyond their scope?

In the following code, the stack-based variable 'ex' is thrown and caught in a function beyond the scope in which ex was declared. This seems a bit strange to me, since (AFAIK) stack-based variables cannot be used outside the scope in which they…
John Doe
  • 285
  • 3
  • 5
25
votes
9 answers

Optional long parameter is present but cannot be translated into a null value

Hi i'm developing on web so i have an ajax function which calling to a controller function which calling to a DAO function (to make changes on DB). I'm getting the exception above in the controller function.. controller…
Erez
  • 502
  • 3
  • 6
  • 17
25
votes
6 answers

boost::python Export Custom Exception

I am currently writing a C++ extension for Python using Boost.Python. A function in this extension may generate an exception containing information about the error (beyond just a human-readable string describing what happened). I was hoping I…
Jack Edmonds
  • 31,931
  • 18
  • 65
  • 77
25
votes
8 answers

What is the right way to pass on an exception? (C#)

I'm wondering what the correct way is to pass on an exception from one method to another. I'm working on a project that is divided into Presentation (web), Business and Logic layers, and errors (e.g. SqlExceptions) need to be passed down the chain…
avesse
  • 771
  • 1
  • 9
  • 20
25
votes
4 answers

How to handle DataIntegrityViolationException in Spring?

I need to show custom messages in my Spring 3.0 application. I have a database with Hibernate and there are several constraints. I have doubts in how DataIntegrityViolationException should be handled in a good way. I wonder if there is a way to map…
Javi
  • 19,387
  • 30
  • 102
  • 135
25
votes
13 answers

In C++ what are the benefits of using exceptions and try / catch instead of just returning an error code?

I've programmed C and C++ for a long time and so far I've never used exceptions and try / catch. What are the benefits of using that instead of just having functions return error codes?
KPexEA
  • 16,560
  • 16
  • 61
  • 78
25
votes
8 answers

code duplication in try catch block

Is there a better way to catch exceptions? I seem to be duplicating a lot of code. Basically in every controller I have a catch statement which does this: try { Do something that might throw exceptions. } catch (exception ex) { Open…
bren bree
  • 253
  • 3
  • 5
25
votes
3 answers

Which Exception to throw when a method try to use a field that can be null?

I am actually working on a Framework development, which means require a really strong coding methodology. I am facing a problem where I do not know which System.Exception derivated class I need to throw. Basically the case is when I have a class…
Ucodia
  • 7,410
  • 11
  • 47
  • 81
25
votes
7 answers

HTTPS hostname wrong: should be . What causes this?

I am getting this 'HTTPS hostname wrong:' error when trying to connect to a server using https. My url looks something like this https://sub.domain.com/tamnode/webapps/app/servlet. I connect using the following code // Create a URLConnection…
paul
  • 13,312
  • 23
  • 81
  • 144
25
votes
1 answer

Await with .NET 4.0: meaningful stack traces

I have a C# console application project using .NET 4.0, with the Microsoft.Bcl.Async package installed. I use this code: internal class Program { private static void Main(string[] args) { Foo().Wait(); } static void…
Paul Stovell
  • 32,377
  • 16
  • 80
  • 108
25
votes
2 answers

What is the C# equivalent to Java's Throwable?

What is the C# equivalent to Java's Throwable? In Java, the root of the exception class hierarchy is called Throwable, not Exception. The Throwable base class has two derived classes: Exception: for conditions that a reasonable application might…
1 2 3
99
100