예외 처리 구문

Jmnote (토론 | 기여)님의 2014년 5월 18일 (일) 06:16 판 (→‎C#)
exception handling syntax
예외 처리 구문

1 Bash

#!/usr/bin/env bash
#set -e provides another error mechanism
print_error(){
	echo "there was an error"
}
trap print_error exit #list signals to trap
tempfile=`mktemp`
trap "rm $tempfile" exit
./other.sh || echo warning: other failed
echo oops)
echo never printed

2 C

#include <setjmp.h>
#include <stdio.h>
#include <stdlib.h>

enum { SOME_EXCEPTION = 1 } exception;
jmp_buf state;

int main(void)
{
  if(!setjmp(state))                      // try
  {
    if(/* something happened */)
    {
      exception = SOME_EXCEPTION;
      longjmp(state, 0);                  // throw SOME_EXCEPTION
    }
  } 
  else switch(exception)
  {             
    case SOME_EXCEPTION:                  // catch SOME_EXCEPTION
      puts("SOME_EXCEPTION caught");
      break;
    default:                              // catch ...
      puts("Some strange exception");
  }
  return EXIT_SUCCESS;
}

3 C#

public static void Main()
{
   try
   {
      // Code that could throw an exception
   }
   catch(System.Net.WebException ex)
   {
      // Process a WebException
   }
   catch(System.Exception)
   {
      // Process a System level CLR exception, that is not a System.Net.WebException,
      // since the exception has not been given an identifier it cannot be referenced
   }
   catch
   {
      // Process a non-CLR exception
   }
   finally
   {
      // (optional) code that will *always* execute
   }
}

3.1 C++

#include <exception>
int main() {
   try {
       // do something (might throw an exception)
   }
   catch (const std::exception& e) {
        // handle exception e
   }
   catch (...) {
        // catches all exceptions, not already catched by a catch block before
        // can be used to catch exception of unknown or irrelevant type
   }
}

4 같이 보기

5 참고 자료

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}