Brief understanding of exception handling in C ++, Java, and C #

Source: Internet
Author: User
From: http://blog.sina.com.cn/s/blog_4daeca9c01000auq.html

 

C ++
Medium exception:

1. Any exception can be thrown. Although the standard C ++ defines the STD: exception class, the throw object can be any object. Including int and char. For example:

Throw 100;

Try {

// Do something

}

Catch (INT num)
{

// Do something

}

However, all exceptions thrown in the C ++ standard library are derived from the STD: exception class.
STD: exception is defined as follows:

Class exception
{
Public:

Exception ();

Exception (const char * const &);

Exception (const char * const &, INT );

Exception (const exception &);

Exception & operator = (const exception &);

Virtual ~ Exception ();

Virtual const char * What () const;

};

2. In Standard C ++, there is no finally clause. It may be strange to be familiar with Java and C! It is inconvenient to use without a finally clause.

3. In C ++, function declaration can throw any exceptions due to lack of time, or use exception specifications to constrain exceptions thrown by functions, such:

// You can throw any exceptions.
Void F (){
Throw 100;
}

// Description this function will not throw any exception
Void F () Throw ()
{
}

// Description this function will throw an int or char * type exception
Void F () Throw (INT, char *)
{
// Do something

}

However, the exception specification in C ++ is not very reliable. The following code can also be compiled, but the compiler will give a warning.

Void F () Throw (INT, char *)
{
Throw "100 ";

}

 

4. c ++ exceptions can be derived from multiple exception base classes. This feature makes it easy to handle unclassified exceptions, such as network file exceptions:

Class file_exception {}
Class network_exception {}
Class newwork_file_exception: Public file_exception, public network_exception
{}

5. Capture all abnormal syntaxes. In C ++, it is not a single inheritance, so you cannot catch all exceptions by capturing a base class, as in Java or C.

Try {
// Do something

} Catch ()
{// Catch all exceptions
// Do something

}


In conclusion, in C ++, namespaces and exception standards are introduced as features for large-scale program design. However, the absence of a garbage collection mechanism in C ++ makes resource management very complex due to exceptions. In C ++, writing exceptional security code is very difficult.



Java
Exceptions in:

1. You can throw any class derived from throwable. The throwable class is defined as follows:

Public class throwable implements serializable
{
Public throwable ();
Public throwable (string message );

Public throwable (string message, throwable cause); // @ since 1.4

Public throwable (throwable cause); // @ since 1.4

Public String getmessage ();

Public String getlocalizedmessage (); // @ since jdk1.1

Public throwable getcause (); /// @ since 1.4

Public synchronized throwable initcause (throwable cause); // @ since 1.4

Public String tostring ();

Public void printstacktrace ();

Public void printstacktrace (printstream S );

Public void printstacktrace (printwriter S); // @ since jdk1.1

Private void printstacktraceascause (printwriter s,

Stacktraceelement [] causedtrace );

Public synchronized native throwable fillinstacktrace ();

Public stacktraceelement [] getstacktrace (); // @ since 1.4

Public void setstacktrace (stacktraceelement [] stacktrace); /// @ since 1.4

}

Exception and error in throwable.

Public class exception extends throwable
{}
Public class error extends throwable
{}

In exception, there is a special derived class, runtimeexception, as follows:
Public class runtimeexception extends exception {}
The class diagram is as follows:

2. Exception specifications are also used in Java and are used in the entire base class library. Java runs based on JVM, but the JVM itself throws an exception, that is, all methods may throw an exception.
. Therefore, the Java compiler does not check error and runtimeexception during compilation. Unlike C ++, there is no exception specification and no non-
Runtimeexception and error. As follows:

Public void F ()
{
Throw new runtimeexception (); // correct

}

Public void F ()
{
Throw new error (); // correct

}

The following code compilation error occurs:

Public void F ()
{
Throw new exception ();

}

A correct example of exception specification:

Public wfexception extends exception
{}

Public void F () throws wfexception
{
Throw new wfexception ();

}

 

3. Some members of throwable, exception, and runtimeexception are added in different JDK versions. You must note when writing code. The following code cannot be used in JDK
Compilation in 1.3:

Public void F () throws wfexception
{
Try {
// Do something

} Catch (exception E)
{
Throw new wfexception ("", e );

}
}

4. Try syntax in Java, including finally clauses. As follows:

Try {
// Do something

} Catch (exception E)
{
// Do something

} Finally {
// Do something

}

 

C #
Exceptions in:

1. Limitations of C # exceptions. C # Runs Based on CLR. Due to the need to support multiple languages, the implementation of exceptions is greatly limited. One important feature is the absence of exception specifications, which many people are disappointed! Anders
Hejlsberg has published a special article to explain why C # does not support exception specifications, which makes sense. However, C # without exception specifications has become simple, but it is also easier to make mistakes. At this point, I prefer Java rather than C. Without exception specifications, the compiler will not help us check the Code and whether the try
... Catch processing.

2. Abnormal Structure. In. net
Framework, all exceptions are derived from the system. Exception base class. There are two subclasses: systemexception and
Applicationexception. Compile the exception class by yourself. We do not recommend that you directly derive from the exception class, but from
Applicationexception.

3. C # Is Like java and supports finally writing. For example:

Try
{
// Do something

}
Catch (exception E)

{
// Do something

}
Finally
{
// Do something

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.